Find unused classes in a Java Eclipse project
Asked Answered
T

3

75

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?

Turner answered 20/3, 2009 at 10:9 Comment(1)
possible duplicate of How to find unused/dead code in java projectsWhitfield
L
88

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.

Loci answered 20/3, 2009 at 11:38 Comment(6)
Does this work for android projects? I downloaded, installed, and the UCDetector menu is no where to be found :(Gazette
@Loci - UCDetector shall not work if there are interface implementations which will be known only at runtime. It incorrectly marks the implementation classes as unused :(Knitted
@Knitted interesting. I have included your comment in the answer for more visibility.Loci
It will be greatfull if a maven plugin would contain this functionality!!!Footworn
@Footworn I agree. Only maven.apache.org/plugins-archives/maven-shade-plugin-3.1.1/… is coming close, to my knowledge.Loci
@Loci 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 jrichardsz.github.io/java/…Footworn
C
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[]);
}
Cerium answered 18/3, 2014 at 11:33 Comment(2)
I had the same idea! Have you found a nice way to specify all the dependencies for a Maven project?Shelton
@Shelton 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.Ferdinand
C
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"

Centiare answered 26/3, 2017 at 1:40 Comment(1)
The question asks for an Eclipse solution. PS, I didn't neg rep you, just adding a comment.Kingwood

© 2022 - 2024 — McMap. All rights reserved.