Ejemplo n.º 1
0
    def enterConstructorDeclaration(
            self, ctx: JavaParserLabeled.ConstructorDeclarationContext):

        # checks if this Constructor contains target lines
        if ctx.start.line <= self.first_line and ctx.stop.line >= self.last_line:
            print("Found Constructor containing target lines.")
            self.is_in_target_method = True
            self.is_result_valid = True

            # checks if Constructor 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 Constructor 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
Ejemplo n.º 2
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.º 3
0
    def exitConstructorDeclaration(
            self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
        if self.is_source_class and self.is_in_constructor:
            move_constructor_flag = False
            for field in self.fields_in_constructor:
                if field in self.moved_fields:
                    move_constructor_flag = True

            for method in self.methods_in_constructor:
                if method in self.moved_methods:
                    move_constructor_flag = True

            if move_constructor_flag:
                # start_index = ctx.parentCtx.parentCtx.start.tokenIndex
                # stop_index = ctx.parentCtx.parentCtx.stop.tokenIndex

                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() + ' '
                # constructor_text += ctx.IDENTIFIER().getText()#======
                constructor_text += self.new_class
                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'

                self.code += constructor_text

                start_index = ctx.parentCtx.parentCtx.start.tokenIndex
                stop_index = ctx.parentCtx.parentCtx.stop.tokenIndex
                self.token_stream_rewriter.delete(
                    program_name=self.token_stream_rewriter.
                    DEFAULT_PROGRAM_NAME,
                    from_idx=start_index,
                    to_idx=stop_index)

        self.is_in_constructor = False
Ejemplo n.º 4
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.º 5
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.º 6
0
 def enterConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     if self.is_source_class:
         self.is_in_constructor = True
         self.fields_in_constructor = []
         self.methods_in_constructor = []
         self.constructor_body = ctx.block()
         children = self.constructor_body.children
Ejemplo n.º 7
0
 def exitConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     is_valid = False
     for i in self.common_sets:
         if i in ctx.getText():
             is_valid = True
             break
     if self.is_father and self.has_father_con and is_valid:
         code = ""
         for var in self.common_sets:
             code += f"this.{var} = {var};\n"
         self.rewriter.insertBeforeToken(token=ctx.stop, text=code)
     self.in_con = False
Ejemplo n.º 8
0
    def enterConstructorDeclaration(
            self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
        if self.inProducts == True:
            Parameter = ""
            if ctx.formalParameters().children.__len__() > 0:
                ParamChild = ctx.formalParameters().children[1]
                for i in range(0, ParamChild.children.__len__(), 2):
                    Parameter += ParamChild.children[i].stop.text + ","
                Parameter = Parameter[:-1]

            self.productConstructorParam[self.currentClass] = Parameter

            ParamList = self.token_stream_rewriter.getText(
                program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME,
                start=ctx.formalParameters().LPAREN().symbol.tokenIndex,
                stop=ctx.formalParameters().RPAREN().symbol.tokenIndex)

            Method = "\t" + self.interfaceName + " create" + \
                     self.currentClass + ParamList + \
                     "{\n\t\t" + "return new " + self.currentClass + "(" + Parameter + ");\n\t}\n"

            self.productConstructorMethod.append(Method)
Ejemplo n.º 9
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.º 11
0
 def enterConstructorDeclaration(self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     params = ctx.formalParameters().getText()
     if params == "()":
         self.has_empty_cons = True
Ejemplo n.º 12
0
 def exitConstructorDeclaration(
         self, ctx: JavaParserLabeled.ConstructorDeclarationContext):
     self.scope_handler.exitMethod(ctx.IDENTIFIER().getText())