Skip to content

Make field non-static

Introduction

Make static field non-static refactoring operation

Pre and post-conditions

Pre-conditions:

Todo: Add pre-conditions

Post-conditions:

Todo: Add post-conditions

MakeFieldNonStaticRefactoringListener (JavaParserLabeledListener)

To implement Make static field non-static refactoring operation based on its actors.

Source code in codart\refactorings\make_field_non_static.py
class MakeFieldNonStaticRefactoringListener(JavaParserLabeledListener):
    """

    To implement Make static field non-static refactoring operation based on its actors.

    """

    def __init__(self, common_token_stream: CommonTokenStream = None, source_class=None, field_name: str = None):
        """


        """

        if field_name is None:
            self.field_name = ""
        else:
            self.field_name = field_name

        if source_class is None:
            self.source_class = ""
        else:
            self.source_class = source_class
        if common_token_stream is None:
            raise ValueError('common_token_stream is None')
        else:
            self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)

        self.is_source_class = False
        self.is_static = False

    def enterClassDeclaration(self, ctx: JavaParserLabeled.ClassDeclarationContext):

        class_identifier = ctx.IDENTIFIER().getText()
        if class_identifier == self.source_class:
            self.is_source_class = True
        else:
            self.is_source_class = False

    def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
        if not self.is_source_class:
            return None
        grand_parent_ctx = ctx.parentCtx.parentCtx
        # field_identifier = ctx.variableDeclarators().getText().split(",")
        field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText()
        if self.field_name in field_identifier:
            i = 0
            if not (grand_parent_ctx.modifier() == []):
                for i in range(0, len(grand_parent_ctx.modifier())):
                    if grand_parent_ctx.modifier(i).getText() == "static":
                        self.is_static = True
                        break
                if self.is_static:
                    self.token_stream_rewriter.replaceRange(
                        from_idx=grand_parent_ctx.modifier(i).start.tokenIndex,
                        to_idx=grand_parent_ctx.modifier(i).stop.tokenIndex,
                        text=''
                    )

__init__(self, common_token_stream=None, source_class=None, field_name=None) special

Source code in codart\refactorings\make_field_non_static.py
def __init__(self, common_token_stream: CommonTokenStream = None, source_class=None, field_name: str = None):
    """


    """

    if field_name is None:
        self.field_name = ""
    else:
        self.field_name = field_name

    if source_class is None:
        self.source_class = ""
    else:
        self.source_class = source_class
    if common_token_stream is None:
        raise ValueError('common_token_stream is None')
    else:
        self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)

    self.is_source_class = False
    self.is_static = False

main(udb_path, source_class, field_name, *args, **kwargs)

Main API for make field non-static

Source code in codart\refactorings\make_field_non_static.py
def main(udb_path, source_class, field_name, *args, **kwargs):
    """

    Main API for make field non-static

    """

    main_file = None
    db = und.open(udb_path)
    classes = db.ents("Class")
    for cls in classes:
        if cls.simplename() == source_class:
            if cls.parent() is not None:
                temp_file = str(cls.parent().longname(True))
                if os.path.isfile(temp_file):
                    main_file = temp_file
                    break

    if main_file is None:
        db.close()
        return False

    db.close()
    stream = FileStream(main_file, encoding='utf8', errors='ignore')
    lexer = JavaLexer(stream)
    token_stream = CommonTokenStream(lexer)
    parser = JavaParserLabeled(token_stream)
    parser.getTokenStream()
    parse_tree = parser.compilationUnit()
    my_listener = MakeFieldNonStaticRefactoringListener(
        common_token_stream=token_stream,
        source_class=source_class,
        field_name=field_name
    )
    walker = ParseTreeWalker()
    walker.walk(t=parse_tree, listener=my_listener)

    with open(main_file, mode='w', encoding='utf8', errors='ignore', newline='') as f:
        f.write(my_listener.token_stream_rewriter.getDefaultText())

    return True