Manual Refactoring

Manual Refactoring

While the analysis and refactoring tools described in the previous section are focused on batch-automated tasks, you’ll often need to do more fine-grained refactoring work. Typical examples of these tasks include moving classes from one package to another and changing method parameters. These tasks need to be done on a case-by-case basis, for which the IDE has a range of small, subtle, and powerful features to help you get started.

You can start using the manual refactoring features from the Refactor menu in the main menubar or by right-clicking in the Source Editor or on a class node in the Projects window and choosing from the Refactor submenu.

Table 6-3 provides a summary of the refactoring features that are available in this context. These features are explained more thoroughly in task-specific topics throughout the remainder of this chapter, starting right after Table 6-3 .

Table 6-3. Refactoring Features

Feature Description Shortcut

Find Usages

Displays all occurrences of the name of a given class, method,

Alt+F7 or field. See “Finding Occurrences of the Currently Selected Class, Method, or Field Name” in the previous chapter.

Rename Renames all occurrences of the selected class, interface, Ctrl+R method, or field name. See “Renaming All Occurrences of the Currently Selected Class, Method, or Field Name” in the previous chapter.

Safely Delete Deletes a code element after making sure that no other code Alt+Delete

references that element. See “Deleting Code Safely” earlier in this chapter.

Change Method

Ctrl+Alt+Shift+C Parameters

Enables you to change the parameters and the access modifier

for the given method. See “Changing a Method’s Signature” later in this chapter.

Encapsulate Fields Generates accessor methods (getters and setters) for a field and Ctrl+Alt+Shift+E changes code that accesses the field directly so that it uses those new accessor methods instead. See “Encapsulating a Field” later in this chapter.

Replace Constructor Allows you to hide a constructor and replace it with a static Ctrl+Alt+Shift+F with Factory

method that returns a new instance of a class. Replace Constructor Helps hide a constructor, replacing its usage with the references Ctrl+Alt+Shift+B

with Builder

to a newly generated builder class or to an existing builder class.

Invert Boolean Allows you to change the sense of a Boolean method or Ctrl+Alt+Shift+I variable to its opposite.

Copy

Copies a class to a different package and enables you to update

Alt+C all references to that class with the new package name.

Move Class

Moves a class to a different package and updates all references

Ctrl+M

to that class with the new package name. See “Moving a Class to

a Different Package” later in this chapter. Pull Up

Moves a method, inner class, or field to a class’s superclass. You Ctrl+Alt+Shift+U can also use this feature to declare the method in the superclass and keep the method definition in the current class. See “Moving Class Members to Other Classes” later in this chapter.

(continued)

Table 6-3. (continued)

Feature Description Shortcut

Push Down Moves a method, inner class, or field to a class’s direct Ctrl+Alt+Shift+D

subclasses. You can also use this feature to keep the method declaration in the current class and move the method definition to the subclasses. See “Moving Class Members to Other Classes” later in this chapter.

Introduce Method Creates a new method based on a selection of code in the Alt+Shift+M selected class and replaces the extracted statements with a call to the new method. See “Creating a Method from Existing Statements” later in this chapter. Similarly, you can use existing statements to create a variable (Alt+Shift+V), Constant (Alt+Shift+C), Field (Alt+Shift+E), Parameter (Alt+Shift+P), and local extension (Alt+Shift+X).

Extract Interface Creates a new interface based on a selection of methods in Ctrl+Alt+Shift+T the selected class and adds the new interface to the class’s implements class. See “Creating an Interface from Existing Methods” later in this chapter.

Extract Superclass Creates a new superclass based on a selection of methods in the Ctrl+Alt+Shift+S selected class. You can have the class created with just method declarations, or you can have whole method definitions moved into the new class. See “Extracting a Superclass to Consolidate Common Methods” later in this chapter.

Use Supertype Where Changes code to reference objects of a superclass (or other type) Ctrl+Alt+Shift+W Possible

instead of objects of a superclass. See “Changing References to Use a Supertype” later in this chapter.

Ctrl+Alt+Shift+L Outer Level

Move Inner Class to

Moves a class up one level. If the class is a top-level inner class,

it is made into an outer class and moved into its own source file. If the class is nested within the scope of an inner class, method, or variable, it is moved up to the same level as that scope. See “Unnesting Classes” later in this chapter.

Convert Anonymous Converts an anonymous inner class to a named inner class. Ctrl+Alt+Shift+A Class to Member

See “Unnesting Classes” later in this chapter.