예제 #1
0
    def enterFieldDeclaration(self,
                              ctx: JavaParserLabeled.FieldDeclarationContext):
        if self.is_source_class or self.is_target_class:
            field_text = ''
            for child in ctx.children:
                if child.getText() == ';':
                    field_text = field_text[:len(field_text) - 1] + ';'
                    break
                field_text += child.getText() + ' '

            name = ctx.variableDeclarators().variableDeclarator(
                0).variableDeclaratorId().IDENTIFIER().getText()

            if ctx.typeType().classOrInterfaceType() != None and ctx.typeType(
            ).classOrInterfaceType().getText() == self.source_class:
                self.field_that_has_source.append(name)
                return

            modifier_text = ''
            for modifier in ctx.parentCtx.parentCtx.modifier():
                modifier_text += modifier.getText() + ' '
            field_text = modifier_text + field_text
            if self.is_source_class:
                self.source_class_data['fields'].append(
                    Field(name=name, text=field_text))
            else:
                self.target_class_data['fields'].append(
                    Field(name=name, text=field_text))
예제 #2
0
 def exitFieldDeclaration(self,
                          ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.is_source_class:
         return None
     grand_parent_ctx = ctx.parentCtx.parentCtx
     # field_identifier = ctx.variableDeclarators().getText().split(",")
     field_identifier = ctx.variableDeclarators().variableDeclarator(
         0).variableDeclaratorId().IDENTIFIER().getText()
     if self.field_name in field_identifier:
         if grand_parent_ctx.modifier() == []:
             self.token_stream_rewriter.replaceRange(
                 from_idx=ctx.typeType().start.tokenIndex,
                 to_idx=ctx.typeType().stop.tokenIndex,
                 text='static ' + ctx.typeType().getText())
         else:
             for i in range(0, len(grand_parent_ctx.modifier())):
                 if grand_parent_ctx.modifier(i).getText() == "static":
                     self.is_static = True
                     break
             if not self.is_static:
                 self.token_stream_rewriter.replaceRange(
                     from_idx=grand_parent_ctx.modifier(0).start.tokenIndex,
                     to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex,
                     text=grand_parent_ctx.modifier(0).getText() +
                     ' static')
예제 #3
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        if self.package_identifier is None and not self.in_some_package\
                or self.package_identifier is not None and self.in_selected_package:
            if self.in_selected_class:
                if ctx.variableDeclarators().variableDeclarator(0)\
                        .variableDeclaratorId().getText() == self.field_identifier:

                    grand_parent_ctx = ctx.parentCtx.parentCtx
                    if len(grand_parent_ctx.modifier()) == 0:
                        self.token_stream_rewriter.insertBeforeIndex(
                            index=ctx.parentCtx.start.tokenIndex,
                            text=' static ')
                    else:
                        is_static = False
                        for modifier in grand_parent_ctx.modifier():
                            if modifier.getText() == "static":
                                is_static = True
                                break
                        if not is_static:
                            self.token_stream_rewriter.insertAfter(
                                index=grand_parent_ctx.start.tokenIndex +
                                len(grand_parent_ctx.modifier()),
                                text=' static ')

        if self.package_identifier is None or self.package_identifier is not None and self.is_package_imported:
            if ctx.typeType().classOrInterfaceType() is not None:
                if ctx.typeType().classOrInterfaceType().getText(
                ) == self.class_identifier:
                    self.declared_objects_names.append(
                        ctx.variableDeclarators().variableDeclarator(
                            0).variableDeclaratorId().getText())
                    print("Object " + ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().getText()\
                          + " of type " + self.class_identifier + " found.")
예제 #4
0
    def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
        if ctx.typeType() is None:
            return

        field_type = ctx.typeType().getText()
        if field_type in self.file_classes:
            log_error("Forbidden", "This class has fields that dependent on other classes")
            exit(0)
예제 #5
0
 def exitFieldDeclaration(self,
                          ctx: JavaParserLabeled.FieldDeclarationContext):
     if self.package_identifier is None \
             or self.package_identifier is not None \
             and self.is_package_imported:
         if ctx.typeType().getText() == self.class_identifier:
             # change the name class; (we find right class then change the name class)
             self.token_stream_rewriter.replaceIndex(
                 index=ctx.typeType().start.tokenIndex,
                 text=self.class_new_name)
             print("class name has change to new_class_name")
예제 #6
0
 def exitFieldDeclaration(self,
                          ctx: JavaParserLabeled.FieldDeclarationContext):
     if ctx.typeType().getText() == self.source_class:
         self.aul.add_identifier(
             (ctx.variableDeclarators().variableDeclarator(
                 0).variableDeclaratorId().IDENTIFIER().getText(),
              self.scope))
예제 #7
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        """
        It gets the field name, if the field is one of the moved fields, we move it and delete it from the source program. ​
        """
        if not self.is_source_class:
            return None
        field_identifier = ctx.variableDeclarators().variableDeclarator(
            0).variableDeclaratorId().IDENTIFIER().getText()
        field_names = list()
        field_names.append(field_identifier)
        # print("field_names=", field_names)
        grand_parent_ctx = ctx.parentCtx.parentCtx
        if self.detected_field in field_names:
            if (not grand_parent_ctx.modifier()):
                modifier = ""
            else:
                modifier = grand_parent_ctx.modifier(0).getText()
            field_type = ctx.typeType().getText()
            self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}"

            # delete field from source class ==>new
            start_index = ctx.parentCtx.parentCtx.start.tokenIndex
            stop_index = ctx.parentCtx.parentCtx.stop.tokenIndex
            self.token_stream_rewriter.delete(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                from_idx=start_index,
                to_idx=stop_index)

            self.detected_field = None
예제 #8
0
 def exitFieldDeclaration(self,
                          ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.is_source_class:
         return None
     # field_names = ctx.variableDeclarators().getText().split(",")
     field_identifier = ctx.variableDeclarators().variableDeclarator(
         0).variableDeclaratorId().IDENTIFIER().getText()
     field_names = list()
     field_names.append(field_identifier)
     print("field_names=", field_names)
     print("Here")
     grand_parent_ctx = ctx.parentCtx.parentCtx
     if self.detected_field in field_names:
         if (not grand_parent_ctx.modifier()):
             # print("******************************************")
             modifier = ""
         else:
             modifier = grand_parent_ctx.modifier(0).getText()
         field_type = ctx.typeType().getText()
         self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}"
         # delete field from source class
         # field_names.remove(self.detected_field)
         # if field_names:
         #     self.token_stream_rewriter.replaceRange(
         #         from_idx=grand_parent_ctx.start.tokenIndex,
         #         to_idx=grand_parent_ctx.stop.tokenIndex,
         #         text=f"{modifier} {field_type} {','.join(field_names)};"
         #     )
         # else:
         # self.token_stream_rewriter.delete(
         #     program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
         #     from_idx=grand_parent_ctx.start.tokenIndex,
         #     to_idx=grand_parent_ctx.stop.tokenIndex
         # )
         self.detected_field = None
예제 #9
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        if not self.is_source_class:
            return None
        if not self.detected_field:
            return None

        field_names = ctx.variableDeclarators().getText()
        field_names = field_names.split(',')
        grand_parent_ctx = ctx.parentCtx.parentCtx
        if any([self.detected_field in i for i in field_names]):
            field_type = ctx.typeType().getText()

            if len(field_names) == 1:
                self.code += f"public {field_type} {field_names[0]};{self.NEW_LINE}"
            else:
                self.code += f"public {field_type} {self.detected_field};{self.NEW_LINE}"
            # delete field from source class
            for i in field_names:
                if self.detected_field in i:
                    field_names.remove(i)

            if field_names:
                self.token_stream_rewriter.replaceRange(
                    from_idx=grand_parent_ctx.start.tokenIndex,
                    to_idx=grand_parent_ctx.stop.tokenIndex,
                    text=f"public {field_type} {','.join(field_names)};\n")
            else:
                self.token_stream_rewriter.delete(
                    program_name=self.token_stream_rewriter.
                    DEFAULT_PROGRAM_NAME,
                    from_idx=grand_parent_ctx.start.tokenIndex,
                    to_idx=grand_parent_ctx.stop.tokenIndex)
            self.detected_field = None
예제 #10
0
 def exitFieldDeclaration(self,
                          ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.is_source_class:
         return None
     field_names = ctx.variableDeclarators().getText().split(",")
     print("Here")
     grand_parent_ctx = ctx.parentCtx.parentCtx
     if self.detected_field in field_names:
         modifier = grand_parent_ctx.modifier(0).getText()
         field_type = ctx.typeType().getText()
         self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}"
         # delete field from source class
         field_names.remove(self.detected_field)
         if field_names:
             self.token_stream_rewriter.replaceRange(
                 from_idx=grand_parent_ctx.start.tokenIndex,
                 to_idx=grand_parent_ctx.stop.tokenIndex,
                 text=f"{modifier} {field_type} {','.join(field_names)};")
         else:
             self.token_stream_rewriter.delete(
                 program_name=self.token_stream_rewriter.
                 DEFAULT_PROGRAM_NAME,
                 from_idx=grand_parent_ctx.start.tokenIndex,
                 to_idx=grand_parent_ctx.stop.tokenIndex)
         self.detected_field = None
예제 #11
0
 def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
     if self.is_target_class:
         if ctx.typeType().classOrInterfaceType().getText() == self.source_class:
             grand_parent_ctx = ctx.parentCtx.parentCtx
             self.token_stream_rewriter.delete(
                 program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                 from_idx=grand_parent_ctx.start.tokenIndex,
                 to_idx=grand_parent_ctx.stop.tokenIndex)
예제 #12
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        if ctx.typeType().getText() == self.source_class:
            flag = False
            for child in ctx.variableDeclarators().children:
                if child.getText() != ',':
                    id = child.variableDeclaratorId().IDENTIFIER().getText()
                    fields_used = self.aul.get_identifier_fields(
                        (id, self.scope))
                    methods_used = self.aul.get_identifier_methods(
                        (id, self.scope))

                    if len(self.intersection(
                            fields_used, self.moved_fields)) > 0 or len(
                                self.intersection(methods_used,
                                                  self.moved_methods)) > 0:
                        flag = True

            if flag == True:
                self.token_stream_rewriter.replaceRange(
                    from_idx=ctx.typeType().start.tokenIndex,
                    to_idx=ctx.typeType().stop.tokenIndex,
                    text=f"{self.new_class}")

                for child in ctx.variableDeclarators().children:
                    if child.getText() != ',':
                        if type(child.children[-1]) == JavaParserLabeled.VariableInitializer1Context and \
                                type(child.children[-1].children[0]) == JavaParserLabeled.Expression4Context and \
                                child.children[-1].children[0].children[0].getText() == 'new' and \
                                len(child.children[-1].children[0].children) > 1 and \
                                type(child.children[-1].children[0].children[1]) == JavaParserLabeled.Creator1Context:
                            if child.variableInitializer().expression(
                            ).creator().createdName().getText(
                            ) == self.source_class:
                                self.token_stream_rewriter.replaceRange(
                                    from_idx=child.
                                    variableInitializer().expression().creator(
                                    ).createdName().start.tokenIndex,
                                    to_idx=child.variableInitializer(
                                    ).expression().creator().createdName(
                                    ).stop.tokenIndex,
                                    text=f"{self.new_class}")
예제 #13
0
 def enterFieldDeclaration(self,
                           ctx: JavaParserLabeled.FieldDeclarationContext):
     if self.inCreator == True:
         variableType = ctx.typeType().classOrInterfaceType().IDENTIFIER(0)
         if variableType.symbol.text in self.products_identifier:
             self.productVarTypeIndex.append(variableType.symbol.tokenIndex)
             self.productVarValueIndex.append([
                 variableType.symbol.text,
                 ctx.variableDeclarators().variableDeclarator(
                     0).ASSIGN().symbol.tokenIndex, ctx.stop.tokenIndex
             ])
예제 #14
0
 def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.is_source_class:
         return None
     self.detected_field = ctx.variableDeclarators().getText().split(',')
     print("Here it is a field")
     grand_parent_ctx = ctx.parentCtx.parentCtx
     modifier = ""
     for i in range(0, len(grand_parent_ctx.modifier())):
         modifier += grand_parent_ctx.modifier(i).getText()
         modifier += " "
     field_type = ctx.typeType().getText()
     self.fieldcode += f"{self.TAB}{modifier} {field_type} {self.detected_field[0]};{self.NEW_LINE}"
예제 #15
0
    def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
        if not self.is_source_class:
            return None
        grand_parent_ctx = ctx.parentCtx.parentCtx
        # field_identifier = ctx.variableDeclarators().getText().split(",")
        field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText()
        if self.field_name in field_identifier:
            if grand_parent_ctx.modifier() == []:
                self.token_stream_rewriter.replaceRange(
                    from_idx=ctx.typeType().start.tokenIndex,
                    to_idx=ctx.typeType().stop.tokenIndex,
                    text='private ' + ctx.typeType().getText()
                )
            elif grand_parent_ctx.modifier(0).getText() == 'public':
                self.token_stream_rewriter.replaceRange(
                    from_idx=grand_parent_ctx.modifier(0).start.tokenIndex,
                    to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex,
                    text='private')
            elif grand_parent_ctx.modifier(0).getText() != 'private':
                self.token_stream_rewriter.replaceRange(
                    from_idx=grand_parent_ctx.modifier(0).start.tokenIndex,
                    to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex,
                    text='private ' + grand_parent_ctx.modifier(0).getText())
            # generate accessor and mutator methods
            # Accessor body
            new_code = '\n\t'
            new_code += 'public ' + ctx.typeType().getText() + ' get' + str.capitalize(self.field_name)
            new_code += '() { \n\t\t return this.' + self.field_name + ';' + '\n\t}'

            # Mutator body
            new_code += '\n\t'
            new_code += 'public void set' + str.capitalize(self.field_name)
            new_code += '(' + ctx.typeType().getText() + ' ' + self.field_name + ') { \n\t\t'
            new_code += 'this.' + self.field_name + ' = ' + self.field_name + ';' + '\n\t}\n'

            self.token_stream_rewriter.insertAfter(ctx.stop.tokenIndex, new_code)
예제 #16
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        if self.in_source_class and self.in_selected_package:
            if ctx.variableDeclarators().variableDeclarator(
                    0).variableDeclaratorId().getText(
                    ) == self.field_identifier:
                if not ctx.parentCtx.parentCtx.modifier(0):
                    self.token_stream_rewriter.insertBeforeIndex(
                        index=ctx.typeType().stop.tokenIndex, text='private ')

                elif ctx.parentCtx.parentCtx.modifier(0).getText() == 'public':
                    self.token_stream_rewriter.replaceRange(
                        from_idx=ctx.parentCtx.parentCtx.modifier(
                            0).start.tokenIndex,
                        to_idx=ctx.parentCtx.parentCtx.modifier(
                            0).stop.tokenIndex,
                        text='private')
                else:
                    return

                for c in ctx.parentCtx.parentCtx.parentCtx.classBodyDeclaration(
                ):
                    try:
                        print('method name: ' + c.memberDeclaration().
                              methodDeclaration().IDENTIFIER().getText())

                        if c.memberDeclaration().methodDeclaration().IDENTIFIER() \
                                .getText() == 'get' + str.capitalize(
                            self.field_identifier):
                            self.getter_exist = True

                        if c.memberDeclaration().methodDeclaration().IDENTIFIER() \
                                .getText() == 'set' + str.capitalize(
                            self.field_identifier):
                            self.setter_exist = True

                    except:
                        logger.error("not method !!!")

                logger.debug("setter find: " + str(self.setter_exist))
                logger.debug("getter find: " + str(self.getter_exist))

                # generate accessor and mutator methods
                # Accessor body
                new_code = ''
                if not self.getter_exist:
                    new_code = '\n\t// new getter method\n\t'
                    new_code += 'public ' + ctx.typeType().getText() + \
                                ' get' + str.capitalize(self.field_identifier)
                    new_code += '() { \n\t\treturn this.' + self.field_identifier \
                                + ';' + '\n\t}\n'

                # Mutator body
                if not self.setter_exist:
                    new_code += '\n\t// new setter method\n\t'
                    new_code += 'public void set' + str.capitalize(
                        self.field_identifier)
                    new_code += '(' + ctx.typeType().getText() + ' ' \
                                + self.field_identifier + ') { \n\t\t'
                    new_code += 'this.' + self.field_identifier + ' = ' \
                                + self.field_identifier + ';' + '\n\t}\n'
                self.token_stream_rewriter.insertAfter(ctx.stop.tokenIndex,
                                                       new_code)

                hidden = self.token_stream.getHiddenTokensToRight(
                    ctx.stop.tokenIndex)