74

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?

jjujuma
  • 22,055
  • 12
  • 44
  • 46
  • 1
    possible duplicate of [How to find unused/dead code in java projects](http://stackoverflow.com/questions/162551/how-to-find-unused-dead-code-in-java-projects) – PhoneixS Feb 03 '15 at 09:26

3 Answers3

87

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.

joker
  • 3,416
  • 2
  • 34
  • 37
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    Does this work for android projects? I downloaded, installed, and the UCDetector menu is no where to be found :( – Jay Aug 26 '11 at 02:51
  • 3
    @VonC - UCDetector shall not work if there are interface implementations which will be known only at runtime. It incorrectly marks the implementation classes as unused :( – Cid Sep 03 '12 at 09:28
  • 2
    @Cid interesting. I have included your comment in the answer for more visibility. – VonC Sep 03 '12 at 10:17
  • It will be greatfull if a maven plugin would contain this functionality!!! – JRichardsz Nov 20 '18 at 14:40
  • @JRichardsz I agree. Only http://maven.apache.org/plugins-archives/maven-shade-plugin-3.1.1/project-summary.html is coming close, to my knowledge. – VonC Nov 20 '18 at 16:27
  • @VonC yeah!! In my case I need to detect developers bad practices at build stage. I think this project is a good alternative but with maven. Check my recent **TODO** https://jrichardsz.github.io/java/find-unused-classes-and-more-in-java-apps – JRichardsz Nov 20 '18 at 16:57
1

ProGuard can be used to print a report of unused classes/methods. It's a pain to supply all the dependent jars to it, though.

These options list unused classes, fields, and methods in the application mypackage.MyApplication:

-injars      in.jar
-libraryjars <java.home>/lib/rt.jar

-dontoptimize
-dontobfuscate
-dontpreverify
-printusage

-keep public class mypackage.MyApplication {
    public static void main(java.lang.String[]);
}
rustyx
  • 80,671
  • 25
  • 200
  • 267
  • I had the same idea! Have you found a nice way to specify all the dependencies for a Maven project? – Zero3 Nov 28 '15 at 13:51
  • @Zero3 as an option you can use maven dependency plugin to list all dependencies or extract them to some local directory then run windows or linux script which will list jars in directory and generate jar list parameter string and run proguard. – jmu Oct 13 '16 at 12:33
0

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"

Hasan Jamshaid
  • 1,659
  • 1
  • 11
  • 14