Пример #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))
Пример #2
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())
Пример #3
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='final ' + ctx.typeTypeOrVoid().getText())
         else:
             for i in range(0, len(grand_parent_ctx.modifier())):
                 if grand_parent_ctx.modifier(i).getText() == "final":
                     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() + ' final')
Пример #4
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)
 def enterMethodDeclaration(
         self, ctx: JavaParserLabeled.MethodDeclarationContext):
     methods.append(f"{ctx.IDENTIFIER()} ")
     returntype.append(ctx.typeTypeOrVoid().getText())
     if ctx.IDENTIFIER().getText() == self.method_name:
         found_func.append(ctx.IDENTIFIER())