Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused code in a Maven modularised project

I have to clean up an old project and general knowledge here is that the project contains lots of unused code that we could remove. That would save some headaches and make maintenance easier.

I found the Eclipse Core Tools plugin which looks like a great tool, but in our case we have a Maven2 project that is split in 3 modules. I can't seem to be able to run the "find unreferenced members" on the parent project, and 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...

Anyone has overcome that issue? Or found another way to go about this?

Thanks.

like image 511
Lancelot Avatar asked Feb 11 '10 16:02

Lancelot


2 Answers

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).

like image 181
Pascal Thivent Avatar answered Oct 27 '22 09:10

Pascal Thivent


I've used the Core Tools plugin to find unused code across many different maven modules. The build path for each of the projects has to be set up properly so that Eclipse knows that there are dependencies between projects. If you import the projects using the m2eclipse plugin, it will set these up automatically.

I think the way that the Core Tools plugin works is that it automates the process that the "Call Hierarchy" view performs. Select a method and run "Call Hierarchy" (ctrl-alt-H) if there are no callers then Core Tools should mark the method as uncalled.

like image 28
Ken Liu Avatar answered Oct 27 '22 09:10

Ken Liu