Renaming Java elements

In this section, you will rename a Java element using refactoring.  Refactoring actions change the structure of your code without changing its semantics (behavior).

  1. In the Package Explorer view, select junit.framework/TestCase.java.
  2. From its context menu, select Refactor > Rename.
  3. In the New Name field on the Rename Compilation Unit page, type "TestCase2".

    Rename refactoring wizard

  4. To preview the changes that will be made as a result of renaming the class, press Next.
  5. The workbench analyzes the proposed change and presents you with a preview of the changes that would take place if you rename this resource. 

    Since renaming a compilation unit will affect the import statements in other compilation units, there are other compilation units affected by the change.  These are shown in a list of changes in the preview pane.

    Refactoring wizard preview page

  6. On the Refactoring preview page, you can scroll through the proposed changes and select or deselect changes, if necessary.  You will typically accept all of the proposed changes.
  7. Click Finish to accept all proposed changes.

You have seen that a refactoring action can cause many changes in different compilation units.  These changes can be undone as a group.

  1. In the menu bar, select Edit > Undo Rename Compilation Unit.

    Refactor menu

  2. The refactoring changes are undone, and the workbench returns to its previous state. You can undo refactoring actions right up until you change and save a compilation unit, at which time the refactoring undo buffer is cleared.
Refactoring support Refactoring actions
Refactoring wizard
Java preferences