示例#1
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
示例#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 ctx.typeType().getText() == self.source_class:
         self.aul.add_identifier(
             (ctx.variableDeclarators().variableDeclarator(
                 0).variableDeclaratorId().IDENTIFIER().getText(),
              self.scope))
示例#4
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() is not 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))
示例#5
0
 def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
     if self.inCreator:
         variable_type = ctx.typeType().classOrInterfaceType().IDENTIFIER(0)
         if variable_type.symbol.text in self.products_identifier:
             self.productVarTypeIndex.append(variable_type.symbol.tokenIndex)
             self.productVarValueIndex.append([variable_type.symbol.text,
                                               ctx.variableDeclarators().variableDeclarator(
                                                   0).ASSIGN().symbol.tokenIndex, ctx.stop.tokenIndex])
示例#6
0
 def enterFieldDeclaration(self,
                           ctx: JavaParserLabeled.FieldDeclarationContext):
     if self.in_class:
         if ctx.variableDeclarators().variableDeclarator(
                 0).variableDeclaratorId().getText(
                 ) == self.field_identifier:
             self.token_stream_rewriter.replaceIndex(
                 index=ctx.variableDeclarators().variableDeclarator(
                     0).variableDeclaratorId().start.tokenIndex,
                 text=self.field_new_name)
             print("field name changed !")
示例#7
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)
             self.changed = True
             print("class name has change to new_class_name")
示例#8
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}"
示例#9
0
 def enterFieldDeclaration(self,
                           ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.enter_class:
         return
     field_id = ctx.variableDeclarators().variableDeclarator(
         i=0).variableDeclaratorId().IDENTIFIER().getText()
     self.field_dict[field_id] = []
示例#10
0
 def enterFieldDeclaration(self,
                           ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.enter_class:
         if ctx.start.text == self.class_identifier:
             start = ctx.variableDeclarators().variableDeclarator(
                 0).ASSIGN().symbol.tokenIndex + 1
             end = ctx.stop.tokenIndex
             self.ObjectIndex.append([start, end])
示例#11
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:
                # Todo: Requires better handling
                st = f"public {field_type} {field_names[0]};{self.NEW_LINE}"
                if '=new' in st and '()' in st:
                    st = st.replace('new', 'new ')
                self.code += st
            else:
                # Todo: Requires better handling
                st = f"public {field_type} {self.detected_field};{self.NEW_LINE}"
                if '=new' in st and '()' in st:
                    st = st.replace('new', 'new ')
                self.code += st

            # delete field from source class
            for fi in field_names:
                if self.detected_field in fi:
                    field_names.remove(fi)
                # Todo: Requires better handling
                if fi == '1))' or fi == ' 1))':
                    field_names.remove(fi)

            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
示例#12
0
 def enterFieldDeclaration(self,
                           ctx: JavaParserLabeled.FieldDeclarationContext):
     field_names = ctx.variableDeclarators().getText().split(",")
     for field in field_names:
         if field in self.fields_to_increase_visibility:
             for modifier in ctx.parentCtx.parentCtx.modifier():
                 if modifier.getText() == "private":
                     self.token_stream_rewriter.replaceSingleToken(
                         token=modifier.start, text="public ")
示例#13
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:
                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}")
示例#14
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)
示例#15
0
 def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
     if not self.is_source_class or self.inner_class_count != 0:
         return None
     field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText()
     if self.field_name == field_identifier:
         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
         )
         self.detected_field = None
示例#16
0
    def exitFieldDeclaration(self,
                             ctx: JavaParserLabeled.FieldDeclarationContext):
        fieldIdentifier = ctx.variableDeclarators().variableDeclarator(
            0).variableDeclaratorId().IDENTIFIER().getText()
        grandParentCtx = ctx.parentCtx.parentCtx

        if self.Field and self.FieldIndex < len(self.Fields) and self.Fields[
                self.FieldIndex].split('/')[1] == fieldIdentifier:
            if self.IsSourceClassForFields[self.FieldIndex]:
                self.CodeRewrite.delete(self.CodeRewrite.DEFAULT_PROGRAM_NAME,
                                        grandParentCtx.start.tokenIndex,
                                        grandParentCtx.stop.tokenIndex)
                self.FieldIndex += 1
示例#17
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().variableDeclarator(
            0).variableDeclaratorId().IDENTIFIER().getText()
        print("field_identifier :", field_identifier)
        if self.field_name in field_identifier:

            if not (grand_parent_ctx.modifier() == []):
                for i in range(0, len(grand_parent_ctx.modifier())):
                    if grand_parent_ctx.modifier(i).getText() == "final":
                        self.is_final = True
                        break
                print("-----------------------", self.is_final)
                if self.is_final:
                    self.token_stream_rewriter.replaceRange(
                        from_idx=grand_parent_ctx.modifier(i).start.tokenIndex,
                        to_idx=grand_parent_ctx.modifier(i).stop.tokenIndex,
                        text='')
示例#18
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)