Example #1
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))
Example #2
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]
Example #3
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
    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