How to Find Unused/Dead Code in Java Projects

How to find unused/dead code in java projects

I would instrument the running system to keep logs of code usage, and then start inspecting code that is not used for months or years.

For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.

Of course, if you go at the method level you should keep performance in mind. For example, the methods could only log their first use. I dont know how this is best done in Java. We have done this in Smalltalk, which is a dynamic language and thus allows for code modification at runtime. We instrument all methods with a logging call and uninstall the logging code after a method has been logged for the first time, thus after some time no more performance penalties occur. Maybe a similar thing can be done in Java with static boolean flags...

Java - find methods used only in test (unused in source code)

In my opinion, the simplest solution is to first delete or exclude test package from your project, then either utilize some tool that finds all the unused methods, or update the Java Compiler Error/Warning settings for unused/unnecessary code to get some errors/warnings as a result for you after a build.

I couldn't find any unused method finder tool where you can exclude some usages from certain packages. If there is any, I'd still recommend above steps with compiler, for I'd rather depend on less tools on my IDE, if tool adds very little to the productivity.

Find unused public members in Eclipse

There exists a Eclipse plugin Unused Code Detector (http://www.ucdetector.org/).
It can process a specific file or the entire project, searching for unused members/methods.

How to use IntelliJ IDEA to find all unused code?

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"

How can I find all unused methods of my project in the Android Studio IDE?

In the android studio(or more generally in the Intellij IDEA) you can specify inspection code that you want to analyze from Analyze->Inspect Code... and then define the scope of your code.
You can see the result for my code in the below picture:

Sample Image

In Declaration redundancy you can see that "someMethod" is declared unused. Also, in Probable bugs you can find variable i is never used.

Find unused code in a Maven modularised project

when I run it on one of the modules it ignores completely the fact that other modules might be using some of the public members...

Yes, that's the problem, and that's why there is no real deterministic way to do find unused code as reminded by @cletus in this previous answer.

Having that said, tools like PMD (and its unusedcode rule), Findbugs may help anyway. IDEs like IntelliJ (Java code inspections are fully available in the Community Edition) and Eclipse also have good support for this.

For IntelliJ, have a look at Global unused declaration inspection:

Thanks to improvements in the internal indexes behind the Intellij IDEA code insight engine, Maia will be able to instantly highlight some java classes, methods and fields which are unused across the entire project.

For Eclipse, there is the UCDetector plugin:

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

alt text

But I confess that I'm not sure if any of these solution will really work across modules. In that case, my suggestion would be to put all the code in one "janitor" project (yeah, this is ugly but well...) and to run the tools on it (and to clean modules based on the obtained results).



Related Topics



Leave a reply



Submit