Skip to content

Rename package

When the name of a package does not explain what the class does (package's functionality), it needs to be changed.

Pre-conditions:

Todo: Add pre-conditions

Post-conditions:

Todo: Add post-conditions

FindPackages (JavaParserLabeledListener)

The class find packages

Source code in codart\refactorings\rename_package.py
class FindPackages(JavaParserLabeledListener):
    """
    The class find packages

    """

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

        """

        self.token_stream = common_token_stream

        # Move all the tokens in the source code in a buffer, token_stream_rewriter.
        if common_token_stream is not None:
            self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)
        else:
            raise TypeError('common_token_stream is None')

    def enterPackageDeclaration(self, ctx: JavaParserLabeled.PackageDeclarationContext):
        if ctx.qualifiedName().IDENTIFIER()[-1].getText() not in packages:
            packages.append(ctx.qualifiedName().IDENTIFIER()[-1].getText())
            print("package", ctx.qualifiedName().IDENTIFIER()[-1].getText(), "added to list")

__init__(self, common_token_stream=None) special

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

    """

    self.token_stream = common_token_stream

    # Move all the tokens in the source code in a buffer, token_stream_rewriter.
    if common_token_stream is not None:
        self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)
    else:
        raise TypeError('common_token_stream is None')

RenamePackageRefactoringListener (JavaParserLabeledListener)

The class implements Rename Package refactoring.

Source code in codart\refactorings\rename_package.py
class RenamePackageRefactoringListener(JavaParserLabeledListener):
    """
        The class implements Rename Package refactoring.
    """
    def __init__(self,
                 common_token_stream: CommonTokenStream = None,
                 package_identifier: str = None,
                 package_new_name: str = None,
                 packages_name: list = []):
        """
         Args:

             common_token_stream (CommonTokenStream): An instance of ANTLR4 CommonTokenStream class

             package_identifier(str): Name of the package in which the refactoring has to be done

             package_new_name(str): The new name of the refactored method

             packages_name(str): Name of the packages in which the refactoring has to be done

        Returns:

            RenamePackageRefactoringListener: An instance of RenamePackageRefactoringListener class

        """

        self.token_stream = common_token_stream
        self.package_identifier = package_identifier
        self.package_new_name = package_new_name
        self.packages_name = packages_name
        self.is_in_scope = False
        # Move all the tokens in the source code in a buffer, token_stream_rewriter.
        if common_token_stream is not None:
            self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)
        else:
            raise TypeError('common_token_stream is None')

    def enterPackageDeclaration(self, ctx: JavaParserLabeled.PackageDeclarationContext):
        if self.package_identifier == ctx.qualifiedName().IDENTIFIER()[-1].getText():
            if self.package_new_name not in self.packages_name:
                self.token_stream_rewriter.replaceIndex(
                    index=ctx.qualifiedName().start.tokenIndex + (2 * len(ctx.qualifiedName().IDENTIFIER()) - 2),
                    text=self.package_new_name)
                print("package changed")

    def enterImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext):
        if ctx.qualifiedName().IDENTIFIER()[-1].getText() == self.package_identifier:
            if self.package_new_name not in self.packages_name:
                self.token_stream_rewriter.replaceIndex(
                    index=ctx.qualifiedName().start.tokenIndex + (2 * len(ctx.qualifiedName().IDENTIFIER()) - 2),
                    text=self.package_new_name)
                print("package name in import changed")

__init__(self, common_token_stream=None, package_identifier=None, package_new_name=None, packages_name=[]) special

Args:

 common_token_stream (CommonTokenStream): An instance of ANTLR4 CommonTokenStream class

 package_identifier(str): Name of the package in which the refactoring has to be done

 package_new_name(str): The new name of the refactored method

 packages_name(str): Name of the packages in which the refactoring has to be done

Returns:

Type Description
RenamePackageRefactoringListener

An instance of RenamePackageRefactoringListener class

Source code in codart\refactorings\rename_package.py
def __init__(self,
             common_token_stream: CommonTokenStream = None,
             package_identifier: str = None,
             package_new_name: str = None,
             packages_name: list = []):
    """
     Args:

         common_token_stream (CommonTokenStream): An instance of ANTLR4 CommonTokenStream class

         package_identifier(str): Name of the package in which the refactoring has to be done

         package_new_name(str): The new name of the refactored method

         packages_name(str): Name of the packages in which the refactoring has to be done

    Returns:

        RenamePackageRefactoringListener: An instance of RenamePackageRefactoringListener class

    """

    self.token_stream = common_token_stream
    self.package_identifier = package_identifier
    self.package_new_name = package_new_name
    self.packages_name = packages_name
    self.is_in_scope = False
    # Move all the tokens in the source code in a buffer, token_stream_rewriter.
    if common_token_stream is not None:
        self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)
    else:
        raise TypeError('common_token_stream is None')