Skip to content

Rename method

Introduction

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

Pre and Post Conditions

Pre Conditions:

  1. User must enter the existing method's name, The source class's name for the refactoring, and the new method name in order to rename.

  2. Check if the method exist, then rename it.

Post Conditions:

  1. After refactoring, all the old method names in the project should be changed.

See whether the method is defined in a superclass or subclass. If so, you must repeat all steps in these classes too.

The next method is important for maintaining the functionality of the program during the refactoring process. Create a new method with a new name. Copy the code of the old method to it. Delete all the code in the old method and, instead of it, insert a call for the new method.

Find all references to the old method and replace them with references to the new one.

Delete the old method. If the old method is part of a public interface, don’t perform this step. Instead, mark the old method as deprecated.

__init__(self, java_file_path, common_token_stream, scope_class_name, target_method_name, new_name, reference=None) special

The Main listener which parses the file based on the provided information, using ANTLR parser generator and tokenization methods

Parameters:

Name Type Description Default
java_file_path(str)

Address path to the test/source file

required
scope_class_name(str)

Name of the class in which the refactoring has to be done

required
target_method_name(str)

Name of the method in which the refactoring has to be done

required
new_name(str)

The new name of the refactored method

required

Returns:

Type Description

No returns

Source code in refactorings\rename_method.py
def __init__(self, java_file_path, common_token_stream, scope_class_name, target_method_name, new_name,
             reference=None):
    """The Main listener which parses the file based on the provided information,
        using ANTLR parser generator and tokenization methods

        Args:
            java_file_path(str): Address path to the test/source file

            scope_class_name(str): Name of the class in which the refactoring has to be done

            target_method_name(str): Name of the method in which the refactoring has to be done

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

        Returns:
            No returns
    """
    self.file_path = java_file_path
    self.token_stream = common_token_stream
    self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)
    self.class_name = scope_class_name
    self.method_name = target_method_name
    self.new_method_name = new_name
    self.in_class = False
    self.changed = False
    self.reference = reference