2.4 Refactoring
One of the major advantages of using a good Java IDE like Eclipse is that it can let you rename and move Java elements around, and it will update all references to those items throughout your code automatically.
2.4.1 Renaming Elements
For example, take a look at the code in Example 2-6. Here, we've used code assist to create a new method to display a simple message, but we
forgot
to change the default
name
for the method that code assist supplied.
Example 2-6. The Ch02_06.java example
package org.eclipse.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch0206 {
public static void main(String[] args) {
name( );
}
public static void name( ) {
System.out.println("No worries.");
}
}
This default name for the new method,
name
, is called in the
main
method, and it could be called from other locations in your code as well. How can you change the name of this method and automatically update all calls to it? Select
name
in the editor and then select the Refactor
Rename menu item, opening the Rename Method dialog you see in Figure 2-20.
Figure 2-20. Refactoring a method
Enter the new name for the method,
printer
in this case, and click OK. When you do, the name of this method and all references to it will be updated throughout your code, including all code in the project, as you see here:
package org.eclipse.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch0206 {
public static void main(String[] args) {
printer( );
}
public static void printer( ) {
System.out.println("No worries.");
}
}
We've also misnamed the class in this example
Ch0206
, instead of
Ch02_06
. To rename the class, select
Ch0206
in the editor and select the Refactor
Rename menu item, opening the Rename Type dialog you see in Figure 2-21. Enter the new name,
Ch02_06
, and click OK to rename the class.
Figure 2-21. Refactoring a class
Clicking OK not only changes the name of the class in the code, it even changes the name of the class's file from
Ch0206.java
to
Ch02_06.java
, as you can see by checking the Package Explorer. Here's the new code:
package org.eclipse.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch02_06 {
public static void main(String[] args) {
printer( );
}
public static void printer( ) {
System.out.println("No worries.");
}
}
In fact, we've unaccountably managed to misname the package as well when creating this example
org.eclipse.ch02
instead of
org.eclipsebook.ch02
. When you refactor it, the name is changed both in the Package Explorer and throughout your code:
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch02_06 {
public static void main(String[] args) {
printer( );
}
public static void printer( ) {
System.out.println("No worries.");
}
}
As you can see, it's easy to rename Java elements in your codeEclipse will handle the details, making the changes throughout your code automatically.
|
If you simply type over a Java element in your code, no refactoring happens. You've got to explicitly refactor if you want those changes to echo throughout your code.
|
|
2.4.2 Moving Elements
Refactoring works automatically across files as well. Say, for example, that you want to move the
printer
method to another class,
Ch02_06Helper
. To see how this works, create that new class now, which Eclipse will put in its own file,
Ch02_06Helper.java
. Then select the method you want to move,
printer
, by selecting the word "printer" in the declaration of this method. Next, select the Refactor
Move to
open
the dialog you see in Figure 2-22. To move this method to the
Ch02_06Helper
class, enter the fully qualified name of that class,
org.eclipsebook.ch02.Ch02_06Helper
, in the dialog and click OK. This moves the
printer
method to the
Ch02_06Helper
class like this:
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch02_06Helper {
public static void printer( ) {
System.out.println("No worries.");
}
}
Figure 2-22. Moving a method between classes
And the call to the
printer
method is automatically qualified as
Ch02_06Helper.printer
back in the
Ch02_06
class in the
main
method:
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch02_06 {
public static void main(String[] args) {
Ch02_06Helper.printer( );
}
}
2.4.3 Extracting Interfaces
You can also extract interfaces using refactoring. To see how this works, we'll create an interface for the
Ch02_06Helper
class (this class has the
printer
method in it). Convert
printer
from a static to a standard method by deleting the keyword
static
in the method declaration. Then select the name of the class,
Ch02_06Helper
, in the editor and select Refactor
Extract Interface to open the Extract Interface dialog you see in Figure 2-23. Select the
printer
method to add that method to the interface, and then enter the name of the new interface
Ch02_06HelperInterface
and click OK.
Figure 2-23. Extracting an interface
Clicking OK creates a new file,
Ch02_06HelperInterface.java
, where the interface is declared:
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public interface Ch02_06HelperInterface {
public abstract void printer( );
}
The original class is now declared to implement this new interface,
Ch02_06HelperInterface
:
package org.eclipsebook.ch02;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Ch02_06Helper implements Ch02_06HelperInterface {
public void printer( ) {
System.out.println("No worries.");
}
}
Besides renaming and moving elements and extracting interfaces, there are other operations you can perform with refactoring, such as converting anonymous classes to nested classes, changing a method's signature, and converting a local variable to a class field. For these and other options, take a look at the items available in the Refactor menu.
|