Ejemplo n.º 1
0
 def exitConstructorDeclaration(self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     """
     Create a factory method. Make its body a call to the current constructor.
     """
     if self.is_target_class:
         grandParentCtx = ctx.parentCtx.parentCtx
         self.codeRewrite.insertAfter(
             index=grandParentCtx.stop.tokenIndex,
             text="\n    public static " + ctx.IDENTIFIER().getText() + " Create( " + ", ".join(self.new_parameters) +
                  "){\n       return new " + ctx.IDENTIFIER().getText() + "(" +  ", ".join(self.new_parameters_names) + ")}"
         )
     self.new_parameters = []
     self.new_parameters_names = []
Ejemplo n.º 2
0
 def enterConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     if self.is_package_imported or self.in_selected_package:
         if ctx.IDENTIFIER().getText() == self.class_identifier:
             self.token_stream_rewriter.replaceIndex(
                 index=ctx.start.tokenIndex, text=self.class_new_name)
             print("constructor name changed !")
Ejemplo n.º 3
0
    def enterConstructorDeclaration(self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
        if self.is_source_class or self.is_target_class:
            if ctx.formalParameters().formalParameterList():
                constructor_parameters = [ctx.formalParameters().formalParameterList().children[i] for i in
                                          range(len(ctx.formalParameters().formalParameterList().children)) if
                                          i % 2 == 0]
            else:
                constructor_parameters = []
            constructor_text = ''
            for modifier in ctx.parentCtx.parentCtx.modifier():
                constructor_text += modifier.getText() + ' '

            if self.is_source_class:
                constructor_text += self.target_class
            else:
                constructor_text += ctx.IDENTIFIER().getText()
            constructor_text += ' ( '
            for parameter in constructor_parameters:
                constructor_text += parameter.typeType().getText() + ' '
                constructor_text += parameter.variableDeclaratorId().getText() + ', '
            if constructor_parameters:
                constructor_text = constructor_text[:len(constructor_text) - 2]
            constructor_text += ')\n\t{'
            constructor_text += self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=ctx.block().start.tokenIndex + 1,
                stop=ctx.block().stop.tokenIndex - 1
            )
            constructor_text += '}\n'
            if self.is_source_class:
                self.source_class_data['constructors'].append(ConstructorOrMethod(
                    name=self.target_class, parameters=[Parameter(parameter_type=p.typeType().getText(),
                                                                  name=p.variableDeclaratorId().IDENTIFIER().getText())
                                                        for p in constructor_parameters],
                    text=constructor_text, constructor_body=self.token_stream_rewriter.getText(
                        program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                        start=ctx.block().start.tokenIndex + 1,
                        stop=ctx.block().stop.tokenIndex - 1
                    )))
            else:
                self.target_class_data['constructors'].append(ConstructorOrMethod(
                    name=self.target_class, parameters=[Parameter(parameter_type=p.typeType().getText(),
                                                                  name=p.variableDeclaratorId().IDENTIFIER().getText())
                                                        for p in constructor_parameters],
                    text=constructor_text, constructor_body=self.token_stream_rewriter.getText(
                        program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                        start=ctx.block().start.tokenIndex + 1,
                        stop=ctx.block().stop.tokenIndex - 1
                    )))
                proper_constructor = get_proper_constructor(self.target_class_data['constructors'][-1],
                                                            self.source_class_data['constructors'])

                if proper_constructor is None:
                    return

                self.token_stream_rewriter.insertBeforeIndex(
                    index=ctx.stop.tokenIndex,
                    text=proper_constructor.constructorBody
                )
Ejemplo n.º 4
0
 def enterConstructorDeclaration(self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     if self.is_target_class:
         # print("constructor name " + ctx.IDENTIFIER().getText())
         # parameters = ctx.formalParameters().getText()
         # print(len(ctx.formalParameters().formalParameterList().formalParameter()))
         grandParentCtx = ctx.parentCtx.parentCtx
         if ctx.IDENTIFIER().getText() == self.target_class:
             self.have_constructor = True
             # do refactor
             """
             Declare the constructor private.
             """
             if grandParentCtx.modifier():
                 if 'public' == grandParentCtx.modifier(0).getText():
                     self.codeRewrite.replaceRange(
                         from_idx=grandParentCtx.modifier(0).start.tokenIndex,
                         to_idx=grandParentCtx.modifier(0).stop.tokenIndex,
                         text='private')
             else:
                 self.codeRewrite.insertBeforeIndex(
                     index=ctx.start.tokenIndex,
                     text="private "
                 )
 def enterConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     self.current_method = ctx.IDENTIFIER().getText()
     if self.current_method == self.target_method and self.current_class == self.target_class:
         self.target_method_ctx = ctx
Ejemplo n.º 6
0
 def exitConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     self.scope_handler.exitMethod(ctx.IDENTIFIER().getText())