Exemple #1
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)
Exemple #2
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)
Exemple #3
0
 def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext):
     if self.inProducts:
         method_modifier = ctx.parentCtx.parentCtx.start.text
         if method_modifier == 'public':
             method_text = self.token_stream_rewriter.getText(
                 program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                 start=ctx.parentCtx.parentCtx.start.tokenIndex,
                 stop=ctx.formalParameters().RPAREN().symbol.tokenIndex) + ";"
             if method_text not in self.productsMethod:
                 self.productsMethod[method_text] = [self.currentClass]
             else:
                 self.productsMethod[method_text].append(self.currentClass)
    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
Exemple #5
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")
Exemple #6
0
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     self.Methods.append(str(ctx.IDENTIFIER()))
     self.MethodParameters.append(str(ctx.formalParameters()))
Exemple #7
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))