- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
Deleting Code Safely
Over time, your code might gather elements that have limited or no usefulness. To make the code easier to maintain, it is desirable to remove as much of this code as possible. However, it might be hard to immediately determine whether you can delete such code without causing errors elsewhere.
Table 5-10. Refactoring Commands
Command |
Description |
Find Usages |
Displays all occurrences of the name of a given class, method, or field. See Finding Occurrences of the Currently Selected Class, Method, or Field Name earlier in this chapter. |
Rename |
Renames all occurrences of the selected class, interface, method, or field name. See Renaming All Occurrences of the Currently Selected Class, Method, or Field Name earlier in this chapter. |
Safely Delete |
Deletes a code element after making sure that no other code references that element. See Deleting Code Safely earlier in this chapter. |
Change Method 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 changes code that accesses the field directly so that it uses those new accessor methods instead. See Encapsulating a Field later in this chapter. |
Move Class |
Moves a class to a different package and updates all references 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' superclass. You can also use this command 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. |
Push Down |
Moves a method, inner class, or field to a class' direct subclasses. You can also use this command 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. |
Extract Method |
Creates a new method based on a selection of code in the 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. |
Extract Interface |
Creates a new interface based on a selection of methods in the selected class and adds the new interface to the class' implements clause. 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 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 Possible |
Change code to reference objects of a superclass (or other type) instead of objects of a subclass. See Changing References to Use a Supertype later in this chapter. |
Move Inner to Outer Level |
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 Class to Inner |
Converts an anonymous inner class to a named inner class. See Unnesting Classes later in this chapter. |
The IDE's Safely Delete command can help you with the process of removing unused code, saving you cycles of manual searches and compilation attempts. When you use this command, the IDE checks to see if the selected code element is referenced elsewhere. If the code element is not used, the IDE deletes it. If the code element is used, the IDE displays where the code is used. You can then resolve references to the code you want to delete and then try the Safely Delete operation again.
You can use the Safely Delete command on any type (such as a class, interface, and or enumeration), method, field, or local variable.
To safely delete a code element:
- In the Source Editor or Projects window, right-click the code element that you want to delete and choose Refactor | Safely Delete.
- In the Safe Delete dialog box, make sure that the item to be deleted is listed.
-
If you want the IDE to look inside comments for mentions of the code element, select the Search in Comments checkbox.
If this checkbox is not selected, comments referring to the code element are not affected if you delete the code element.
If this checkbox is selected, any found references to the code element in comments will prevent the code element from being immediately deleted, even if the code element is not used.
-
Click Next.
If no references to the code element are found, the Safe Delete dialog box closes and the code element is deleted.
If references to the code element are found, no code is deleted and the Safely Delete dialog box remains open. If you click the Show Usages button, the Usages window opens and displays the references to that code element. Double-click an item in the list to jump to the line of code that it represents. If you remove the cited references, you can click the Rerun Safe Delete button to repeat the attempt to safely delete the code element.