Пример #1
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     scope = self.scope_handler.getScope()[-1]
     self.scope_handler.enterMethod(ctx.IDENTIFIER().getText())
     if (ctx.IDENTIFIER().getText() == self.new_method_name
             and scope == self.class_identifier):
         self.abort = True
Пример #2
0
 def exitMethodDeclaration(self,
                           ctx: JavaParserLabeled.MethodDeclarationContext):
     last_scope = self.scope_handler.getScope()[-2]
     if (self.method_name == ctx.IDENTIFIER().getText()
             and last_scope == self.class_identifier and not self.abort):
         interval = ctx.IDENTIFIER().getSourceInterval()
         self.token_stream_rewriter.replaceRange(interval[0], interval[1],
                                                 self.new_method_name)
     self.scope_handler.exitMethod(ctx.IDENTIFIER().getText())
Пример #3
0
    def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
        if self.is_source_class or self.is_target_class:
            if ctx.formalParameters().formalParameterList():
                method_parameters = [ctx.formalParameters().formalParameterList().children[i] for i in
                                     range(len(ctx.formalParameters().formalParameterList().children)) if i % 2 == 0]
            else:
                method_parameters = []
            method_text = ''
            for modifier in ctx.parentCtx.parentCtx.modifier():
                method_text += modifier.getText() + ' '

            type_text = ctx.typeTypeOrVoid().getText()

            if type_text == self.source_class:
                type_text = self.target_class

                if self.is_target_class:
                    self.token_stream_rewriter.replace(
                        program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                        from_idx=ctx.typeTypeOrVoid().start.tokenIndex,
                        to_idx=ctx.typeTypeOrVoid().stop.tokenIndex,
                        text=type_text
                    )
            method_text += type_text + ' ' + ctx.IDENTIFIER().getText()
            method_text += ' ( '
            for parameter in method_parameters:
                method_text += parameter.typeType().getText() + ' '
                method_text += parameter.variableDeclaratorId().getText() + ', '
            if method_parameters:
                method_text = method_text[:len(method_text) - 2]
            method_text += ')\n\t{'
            method_text += self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=ctx.methodBody().start.tokenIndex + 1,
                stop=ctx.methodBody().stop.tokenIndex - 1
            )
            method_text += '}\n'
            if self.is_source_class:
                self.source_class_data['methods'].append(ConstructorOrMethod(
                    name=ctx.IDENTIFIER().getText(),
                    parameters=[Parameter(
                        parameter_type=p.typeType().getText(),
                        name=p.variableDeclaratorId().IDENTIFIER().getText())
                        for p in
                        method_parameters],
                    text=method_text))
            else:
                self.target_class_data['methods'].append(ConstructorOrMethod(
                    name=ctx.IDENTIFIER().getText(),
                    parameters=[Parameter(
                        parameter_type=p.typeType().getText(),
                        name=p.variableDeclaratorId().IDENTIFIER().getText())
                        for p in
                        method_parameters],
                    text=method_text))
Пример #4
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.source_method == ctx.IDENTIFIER().getText():
         self.token_stream_rewriter.replaceRange(
             from_idx=ctx.parentCtx.parentCtx.start.tokenIndex,
             to_idx=ctx.parentCtx.parentCtx.stop.tokenIndex,
             text="")
Пример #5
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.is_in_scope:
         if ctx.IDENTIFIER().getText() == self.method_identifier:
             self.token_stream_rewriter.replaceIndex(
                 index=ctx.start.tokenIndex + 2, text=self.method_new_name)
             print("method name changed !")
Пример #6
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.is_target_class:
         if ctx.IDENTIFIER().getText() in self.target_methods:
             grand_parent_ctx = ctx.parentCtx.parentCtx
             if grand_parent_ctx.modifier():
                 if len(grand_parent_ctx.modifier()) == 2:
                     self.token_stream_rewriter.delete(
                         program_name=self.token_stream_rewriter.
                         DEFAULT_PROGRAM_NAME,
                         from_idx=grand_parent_ctx.modifier(
                             1).start.tokenIndex - 1,
                         to_idx=grand_parent_ctx.modifier(
                             1).stop.tokenIndex)
                 else:
                     if grand_parent_ctx.modifier(0).getText() == 'static':
                         self.token_stream_rewriter.delete(
                             program_name=self.token_stream_rewriter.
                             DEFAULT_PROGRAM_NAME,
                             from_idx=grand_parent_ctx.modifier(
                                 0).start.tokenIndex - 1,
                             to_idx=grand_parent_ctx.modifier(
                                 0).stop.tokenIndex)
             else:
                 return None
Пример #7
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):
        self.methods_name.append(ctx.IDENTIFIER().getText())
        # checks if this is the method containing target lines
        if ctx.start.line <= self.first_line and ctx.stop.line >= self.last_line:
            print("Found method containing target lines.")
            self.is_in_target_method = True
            self.is_result_valid = True

            # checks if method is static
            for modifier in ctx.parentCtx.parentCtx.modifier():
                if modifier.getText() == 'static':
                    self.is_target_method_static = True
                    print("Target Method is static.")
                    break

            # checks if method throws any exception
            if ctx.qualifiedNameList():
                self.exception_thrown_in_target_method = ctx.qualifiedNameList(
                ).getText()
                print("Target Method throws exception.")
                # TODO : check extracted lines for exception occurrence instead ,
                #  as they may not throw exception even though their parent method does

            # save method's last line number
            self.method_stop_line = ctx.stop.line
Пример #8
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):

        if self.is_source_class:

            # method_identifier = ctx.variableDeclarators().variableDeclarator().variableDeclaratorId().IDENTIFIER.getText()
            method_identifier = ctx.IDENTIFIER().getText()
            print(method_identifier)
            print(self.moved_methods)
            if self.moved_methods == method_identifier:
                methodDefctx = ctx.parentCtx.parentCtx
                start_index = methodDefctx.start.tokenIndex
                stop_index = methodDefctx.stop.tokenIndex
                self.method_text = self.token_stream_rewriter.getText(
                    program_name=self.token_stream_rewriter.
                    DEFAULT_PROGRAM_NAME,
                    start=start_index,
                    stop=stop_index)

                self.token_stream_rewriter.delete(
                    program_name=self.token_stream_rewriter.
                    DEFAULT_PROGRAM_NAME,
                    from_idx=methodDefctx.start.tokenIndex,
                    to_idx=methodDefctx.stop.tokenIndex)

            print("method text: ", self.method_text)
        else:
            return None
Пример #9
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     # Extract Methods Name & Methods body of SuperClass
     if self.InSuperClass == True:
         newClassName = ctx.IDENTIFIER().getText()
         newMethodBody = ctx.methodBody().getText()
         self.Visitors[newClassName] = [newMethodBody]
Пример #10
0
    def exitMethodDeclaration(self,
                              ctx: JavaParserLabeled.MethodDeclarationContext):
        """
        It gets the method name, if the method is one of the moved methods, we move it to the subclass and delete it from the source program.
        """

        if not self.is_source_class:
            return None
        method_identifier = ctx.IDENTIFIER().getText()
        if self.detected_method == method_identifier:
            # method_modifier = ctx.parentCtx.parentCtx.modifier(0)
            # if(method_modifier!=)
            # method_modifier_text=method_modifier.getText()
            # print(method_modifier_text)

            # start_index = ctx.start.tokenIndex
            start_index = ctx.parentCtx.parentCtx.start.tokenIndex
            stop_index = ctx.stop.tokenIndex
            method_text = self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=start_index,
                stop=stop_index)
            self.code += (self.NEW_LINE + self.TAB + method_text +
                          self.NEW_LINE)
            # delete method from source class
            self.token_stream_rewriter.delete(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                from_idx=start_index,
                to_idx=stop_index)
            self.detected_method = None
Пример #11
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class:
         return None
     method_identifier = ctx.IDENTIFIER().getText()
     if method_identifier in self.moved_methods:
         self.detected_method = method_identifier
Пример #12
0
    def exitMethodDeclaration(self,
                              ctx: JavaParserLabeled.MethodDeclarationContext):
        if not self.is_source_class:
            return None
        method_identifier = ctx.IDENTIFIER().getText()
        if self.detected_method == method_identifier:
            start_index = ctx.start.tokenIndex
            stop_index = ctx.stop.tokenIndex
            method_text = self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=start_index,
                stop=stop_index)
            self.code += self.NEW_LINE + ("public " + method_text +
                                          self.NEW_LINE)
            # delegate method body in source class
            if self.method_map.get(method_identifier):
                self.parameters.append("this")

            self.token_stream_rewriter.replaceRange(
                from_idx=ctx.methodBody().start.tokenIndex,
                to_idx=stop_index,
                text="{" +
                f"\nreturn this.{self.object_name}.{self.detected_method}(" +
                ",".join(self.parameters) + ");\n" + "}")
            self.parameters = []
            self.detected_method = None
Пример #13
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.in_class:
         name = ctx.IDENTIFIER()
         if name.getText() == self.method_name:
             node = name.getSymbol()
             self.token_stream_rewriter.replaceIndex(
                 node.tokenIndex, self.new_method_name)
             self.changed = True
Пример #14
0
 def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.enter_class:
         return
     m = []
     m_name = ctx.IDENTIFIER().getText()
     self.method_no = self.method_no + 1
     m.append(m_name)
     m.append(self.method_no)
     self.method_name.append(m)
Пример #15
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     """
     It sets the detected field to the method if it is one of the moved methods. ​
     """
     if not self.is_source_class:
         return None
     method_identifier = ctx.IDENTIFIER().getText()
     if method_identifier in self.moved_methods:
         self.detected_method = method_identifier
Пример #16
0
 def exitMethodDeclaration(self,
                           ctx: JavaParserLabeled.MethodDeclarationContext):
     grandParentCtx = ctx.parentCtx.parentCtx
     methodIdentifier = ctx.IDENTIFIER().getText()
     if self.Method and self.MethodIndex < len(
             self.Methods) and self.Methods[self.MethodIndex].split(
                 '/')[1] == methodIdentifier:
         if self.IsSourceClassForMethods[self.MethodIndex]:
             self.CodeRewrite.delete(self.CodeRewrite.DEFAULT_PROGRAM_NAME,
                                     grandParentCtx.start.tokenIndex,
                                     grandParentCtx.stop.tokenIndex)
             self.MethodIndex += 1
Пример #17
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     # checking if current method is the target method
     if self.is_in_target_package and self.is_in_target_class and is_equivalent(
             ctx.IDENTIFIER(), self.target_method):
         self.is_in_target_method = True
         self.method_start_line = ctx.start.line
         new_lines = []
         for line in self.lines:
             new_lines.append(line + self.method_start_line)
         self.lines = new_lines
         self.method_stop_line = ctx.stop.line
Пример #18
0
 def exitMethodDeclaration(self,
                           ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class or self.inner_class_count != 0:
         return None
     grand_parent_ctx = ctx.parentCtx.parentCtx
     method_identifier = ctx.IDENTIFIER().getText()
     if self.method_name == method_identifier:
         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_method = None
Пример #19
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     grand_parent_ctx = ctx.parentCtx.parentCtx
     self.method_selected = False
     if ctx.IDENTIFIER().getText() == self.method_identifier:
         self.enter_method = True
         self.method_selected = True
         self.old_method_Declaration = self.token_stream_rewriter.getText(
             "",
             grand_parent_ctx.modifier(0).start.tokenIndex,
             ctx.formalParameters().stop.tokenIndex)
         self.old_method_Declaration = self.old_method_Declaration.replace(
             self.method_identifier, "doOperation")
Пример #20
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):
        self.fields = self.method_map.get(ctx.IDENTIFIER().getText())
        if self.fields:
            if ctx.formalParameters().getText() == "()":
                text = f"{self.source_class} ref"
            else:
                text = f", {self.source_class} ref"

            self.token_stream_rewriter.insertBeforeToken(
                token=ctx.formalParameters().stop,
                text=text,
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME)
Пример #21
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):
        methodIdentifier = ctx.IDENTIFIER().getText()
        if self.Variable:
            for i in range(len(self.Variables)):
                if self.IsSourceClassForVariables[i] and self.Variables[
                        i].split('/')[1] == methodIdentifier:
                    self.IsSourceMethodForVariables[i] = True

        if self.Parameter:
            for i in range(len(self.Parameters)):
                if self.IsSourceClassForParameters[i] and self.Parameters[
                        i].split('/')[1] == methodIdentifier:
                    self.IsSourceMethodForParameters[i] = True
        pass
Пример #22
0
    def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):

        if self.is_children_class:

            method_identifier = ctx.IDENTIFIER().getText()
            if self.moved_methods == method_identifier:
                methodDefctx = ctx.parentCtx.parentCtx
                start_index = methodDefctx.start.tokenIndex
                stop_index = methodDefctx.stop.tokenIndex
                self.method_text = self.token_stream_rewriter.getText(
                    program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                    start=start_index,
                    stop=stop_index)

        else:
            return None
Пример #23
0
 def exitMethodDeclaration(self,
                           ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class:
         return None
     grand_parent_ctx = ctx.parentCtx.parentCtx
     method_identifier = ctx.IDENTIFIER().getText()
     if self.method_name in method_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
             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='')
Пример #24
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.is_target_class:
         if ctx.IDENTIFIER().getText() in self.target_methods:
             if 'this.' in ctx.getText():
                 raise ValueError("this method can not refactor")
             grand_parent_ctx = ctx.parentCtx.parentCtx
             if grand_parent_ctx.modifier():
                 if len(grand_parent_ctx.modifier()) == 2:
                     return None
                 else:
                     self.token_stream_rewriter.insertAfter(
                         index=grand_parent_ctx.modifier(0).stop.tokenIndex,
                         program_name=self.token_stream_rewriter.
                         DEFAULT_PROGRAM_NAME,
                         text=" static")
             else:
                 self.token_stream_rewriter.insertBeforeIndex(
                     index=ctx.start.tokenIndex, text="static ")
Пример #25
0
 def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class:
         return None
     method_identifier = ctx.IDENTIFIER().getText()
     if self.detected_method == method_identifier:
         start_index = ctx.start.tokenIndex
         stop_index = ctx.stop.tokenIndex
         method_text = self.token_stream_rewriter.getText(
             program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
             start=start_index,
             stop=stop_index
         )
         self.code += (self.NEW_LINE + self.TAB + method_text + self.NEW_LINE)
         # delete method from source class
         self.token_stream_rewriter.delete(
             program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
             from_idx=start_index,
             to_idx=stop_index
         )
         self.detected_method = None
Пример #26
0
    def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
        if not self.is_source_class:
            return None
        method_identifier = ctx.IDENTIFIER().getText()
        print("Here it is a method")

        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 += " "

        if self.detected_method == method_identifier:
            start_index = ctx.start.tokenIndex
            stop_index = ctx.stop.tokenIndex
            method_text = self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=start_index,
                stop=stop_index
            )
            self.methodcode += (self.NEW_LINE + self.TAB + modifier + method_text + self.NEW_LINE)
Пример #27
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):
        """check if this is the intended method if so capture signature and remove boolean argument
            
        Args:
            ctx (JavaParserLabeled.MethodDeclarationContext): 
        """
        self.is_source_method = (
            ctx.IDENTIFIER().getText() == self.source_method)
        if self.is_source_method:

            nextParam = None

            for idx, formalParameter in enumerate(ctx.formalParameters(
            ).formalParameterList().formalParameter()):
                if formalParameter.variableDeclaratorId().IDENTIFIER().getText(
                ) == self.argument_name:
                    self.argument_token = formalParameter
                    nextParam = ctx.formalParameters().formalParameterList().formalParameter()[idx + 1] \
                        if idx != len(ctx.formalParameters().formalParameterList().formalParameter()) - 1 else None
                    break

            if nextParam:
                self.token_stream_rewriter.replaceRange(
                    self.argument_token.start.tokenIndex,
                    nextParam.start.tokenIndex - 1, '')
            else:
                self.token_stream_rewriter.replaceRange(
                    self.argument_token.start.tokenIndex,
                    self.argument_token.stop.tokenIndex, '')

            self.signature = self.token_stream_rewriter.getText(
                self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                ctx.start.tokenIndex,
                ctx.methodBody().start.tokenIndex)

            if self.token_stream_rewriter_changed == False:
                self.token_stream_rewriter = TokenStreamRewriter(
                    self.common_token_stream)
                self.token_stream_rewriter_changed = True
Пример #28
0
    def enterMethodDeclaration(
            self, ctx: JavaParserLabeled.MethodDeclarationContext):
        if len(self.enter_method_body_stack) > 0:
            return
        if hasattr(ctx.parentCtx.parentCtx, 'modifier'):
            modifiers = ctx.parentCtx.parentCtx.modifier()
        else:
            modifiers = ctx.parentCtx.parentCtx.parentCtx.modifier()

        do_extract = True
        for modifier in modifiers:
            if modifier.classOrInterfaceModifier() is not None:
                # print('modifier.classOrInterfaceModifier().getText()', modifier.classOrInterfaceModifier().getText())

                # Todo: Requires better handling
                if "private" in modifier.classOrInterfaceModifier().getText() or \
                        "static" in modifier.classOrInterfaceModifier().getText() or \
                        '?' in modifier.classOrInterfaceModifier().getText():
                    do_extract = False
                    break

        # Todo: Requires better handling
        if '?' in ctx.getChild(0).getText():
            do_extract = False

        if do_extract:
            method = {
                'name': ctx.IDENTIFIER().getText(),
                'return_type': ctx.typeTypeOrVoid().getText(),
                'formal_parameters': []
            }
            if ctx.formalParameters().formalParameterList() is not None:
                if hasattr(ctx.formalParameters().formalParameterList(),
                           'formalParameter'):
                    for f in ctx.formalParameters().formalParameterList(
                    ).formalParameter():
                        _type = f.typeType().getText()
                        identifier = f.variableDeclaratorId().getText()
                        method['formal_parameters'].append([_type, identifier])
            self.interface_info['methods'].append(method)
Пример #29
0
 def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class:
         return None
     grand_parent_ctx = ctx.parentCtx.parentCtx
     method_identifier = ctx.IDENTIFIER().getText()
     if self.method_name in method_identifier:
         if grand_parent_ctx.modifier() == []:
             self.token_stream_rewriter.replaceRange(
                 from_idx=ctx.typeTypeOrVoid().start.tokenIndex,
                 to_idx=ctx.typeTypeOrVoid().stop.tokenIndex,
                 text='public ' + ctx.typeTypeOrVoid().getText()
             )
         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='public')
         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='public ' + grand_parent_ctx.modifier(0).getText())
Пример #30
0
 def exitMethodDeclaration(self,
                           ctx: JavaParserLabeled.MethodDeclarationContext):
     if not self.is_source_class:
         return None
     grand_parent_ctx = ctx.parentCtx.parentCtx
     method_identifier = ctx.IDENTIFIER().getText()
     if self.method_name in method_identifier:
         if grand_parent_ctx.modifier() is None or len(
                 grand_parent_ctx.modifier()) == 0:
             self.token_stream_rewriter.replaceRange(
                 from_idx=ctx.typeTypeOrVoid().start.tokenIndex,
                 to_idx=ctx.typeTypeOrVoid().stop.tokenIndex,
                 text='static ' + ctx.typeTypeOrVoid().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')