Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused classes in a Java Eclipse project

I have a large Eclipse project in which there exist several classes which, although they ceased to be used anywhere, were never marked @Deprecated.

How can I easily find all of these?

like image 771
jjujuma Avatar asked Mar 20 '09 10:03

jjujuma


People also ask

How do I find unused classes in eclipse?

UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private. Caveat: Cid mentions in the comments: UCDetector shall not work if there are interface implementations which will be known only at runtime.

How can I see all classes in eclipse?

Use Ctrl + Shift + H keys to search for classes.

Where are eclipse classes stored?

The initial value is the bin directory directly under your project. Similarly, the Source tab provides access to the location of your source input folder(s). The Java perspective filters out the output folder from the Package Explorer. If you like, you can use the Navigator view to browse the output folder.

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.


1 Answers

I also like to use UCDetector:

screenshot

UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private.

Bonus: it can also find cyclic dependencies between classes

(also a number of other tools -- including Findbugs -- knows how do do that too)


Caveat: Cid mentions in the comments:

UCDetector shall not work if there are interface implementations which will be known only at runtime.
It incorrectly marks the implementation classes as unused.


Update 2017: static code analysis has evolved quite a bit in 8 years.
Using SonarLint for Eclipse, you can use the the latest SonarJava 4.6 plugin to analyze your code.
It will find dead code.

like image 103
VonC Avatar answered Sep 23 '22 06:09

VonC