Java Reference
In-Depth Information
operations; they're full-fledged refactoring operations that make all the necessary
adjustments to your project's source code.
Deleting unused classes and symbols safely
You don't want to delete a method or other symbol that is being used by another
part of your program. Although you could certainly use the
Find Usages
com-
mand as a way to ensure that something is as useless as you think,
IDEA
makes it
easy with the
Safe Delete
command.
Safe Delete
automatically performs a usage
check for you, proceeding with the deletion only if the symbol really is unused (or
you accept the warning and delete it anyhow).
You execute a
Safe Delete
refactoring by placing your cursor on the doomed
symbol inside the editor and pressing
Alt+Delete
, executing the
Refactor | Safe
Delete
command from the main menu, or right-clicking the symbol and selecting
the
Refactor | Safe Delete
option. You can also delete packages and classes
through the
Project
,
Structure
, and
Commander
windows.
Once you request the deletion,
IDEA
checks for
existing usages (see figure 9.15); if any are found, it
presents a warning dialog and gives you a chance to
see the usages. The usages view works the same as
the
Find
tool window; this includes giving you the
ability to exclude individual references from the
deletion operation. Generally, you use the results to
track down all the references, examine or modify
them, and then make the decision to proceed with
the deletion. If you click the
Ignore
button under
the list of detected usages,
IDEA
performs the deletion regardless of usages, leav-
ing you to clean up your own mess. Alternatively, you can choose to cancel the
entire operation. If you perform a delete on a class or file and are using a version
control system,
IDEA
performs the necessary operations to record the deletion.
(See chapter 8.)
Figure 9.15 Safe Delete
makes sure you don't delete
something you shouldn't by
finding all its usages.
When you're deleting a field variable with corresponding access meth-
ods,
IDEA
gives you the option of deleting the access methods as well.
TIP
Creating new classes by copying existing ones
You can use the
Copy
refactoring to copy a class (or an entire package). This is
often a good way to create a new class using an old one as a starting point. You can
only perform the
Copy
refactoring on a class, file, package, or directory. (It