Contact TLCC

Refactor your Java Code in WebSphere Studio Click here to see all the Developer/Admin Tips

Date tip published:02/23/2004
Description:Learn how to quickly change a Java class name or the name of a variable or method in WebSphere Studio. Refactoring will search for all references to the old name and update the reference to the new name!


To learn more about how to use WebSphere Studio use the following links:

Introduction to WebSphere Studio 5
Servlet and JSP Programming using WebSphere Studio 5
Introduction to WebSphere 5 for Domino Developers
Servlet and JSP Programming using WebSphere 5 for Domino Developers



Refactoring Java Code

When a Java source file is moved or renamed, all code which references the corresponding class must also be updated to reference the new location or name. This can be tedious and time consuming. Fortunately, WebSphere Studio provides a "refactoring" tool which can be used to automatically update all references when a Java source file is moved or renamed. As the following image shows, the refactoring tool is available in the Java Perspective. To move or rename a Java source file, select the file in the Package Explorer view and the select the appropriate option on the Refactor menu.





Procedure: Use the Refactor Tool to Rename a Java Class

Follow these steps to rename a Java class:

  1. In the Package Explorer view of the Java Perspective, select the Java source file to rename.
  2. Select Refactor | Rename... from the menu or right click the source file and select Refactor | Rename... from its context menu.
  3. In the Rename Compilation Unit dialog, provide a new name and select the option to 'Update references to the renamed element'. Click the [Preview] button to preview the effect of the change.

  4. In the preview dialog, the top frame displays an expandable list of the resources where changes will be performed. The two bottom show the original and refactored source for the resource that is selected in the top frame. After reviewing the proposed changes, click the [OK] button to make the changes.




Procedure: Use the Refactor Tool to Move a Java Class

Follow these steps to rename a Java class:

  1. In the Package Explorer view of the Java Perspective, select the Java source files to move.
  2. Select Refactor | Move... from the menu or right click the source file and select Refactor | Move... from its context menu.
  3. In the Move dialog, select the new location for the class and select the option to 'Update references to the moved element(s)'. Click the [Preview] button to preview the effect of the change. Click the [OK] button to make the changes.





Procedure: Changing Individual Java Components

Individual Java components like methods and variable can also be refactored.

Follow these steps to rename a Java component:

  1. Open the Java source with the component to change in the Java Editor.
  2. Click the method or variable to change in the Java Editor or in the Outline View.
  3. Select Refactor | Rename... from the menu or right click the component and select Refactor | Rename... from its context menu.
  4. In the rename dialog, provide a new name and select the option to 'Update references to the renamed element'. Click the [Preview] button to preview the effect of the change.

  5. In the preview dialog, the top pane displays an expandable list of the resources where changes will be performed. The two bottom panes show the original and refactored source for the resource that is selected in the top frame. After reviewing the proposed changes, click the [OK] button to make the changes.




Procedure: Undo a Refactored Change

The Refactor menu include an Undo option to undo the last refactor change. There is also an option to Redo the last undone change.