Finding code that can be thrown away is a highlight of my day. Deleting code means it no longer has to be compiled, maintained or tested. No one has to see the code and wonder "hmm, what's this doing here?".
Especially on big projects with shifting insights in features or architecture and design, old code tends to be left behind. To determine if code is really unused is hard and may take a lot of manual inspection. Fortunately, the Eclipse Core Tools contains a utility called "Find unused members". It tries to determine if code is no longer used.
Download and install the Core Tools plug-in. As an example, consider this code:
package com.infosupport.demo.unused;
import java.io.Serializable;
public class SimpleTalker implements Talker, Serializable {
private static final long serialVersionUID = 1L;
public String sayHello(String message) {
return "Hello " + message;
}
public String sayGoodBye(String message) {
return "Goodbye " + message;
}
public String say(String message) {
return message;
}
public static void main(String[] args) {
System.out.println(new SimpleTalker().sayHello(args[0]));
}
}
package com.infosupport.demo.unused;
public interface Talker {
public String say(String message);
}
(Note that you can copy paste this code to Eclipse and it will generate an example project for you). If you right click on the Java Project you will get a new option:
When you run it, you get the following results:
Note that not all the unused elements are actually unused. The main method and serialVersionUID are special in Java and may be invoked by the VM. Code that is invoked through reflection is not detected. If code that calls this code is not in your workspace (or does not use a project reference to link to the code), it is not detected. Conversely, if you call code only from your unit tests, this tool will not report it because it is still used in the tests. Remember after removing code to re-run the tool, as now more code may have become unused because it was only called from unused code.
Still a lot of gotchas, but nevertheless a great first scanning tool to get rid of that old junk!
6 comments
Can you use it en eclipse europa or it is only in version 3.2?
Can it be installed using the update manager? It would be great if you post the url to use with the manager, It is difficult to find something in the eclipse site.
unregistered
The screen shots were made on Europa, so yes, it works. 🙂 The update site is over here: http://eclipse.org/eclipse/platform-core/updates
peterhe
Really a lot of lots of gotchas. What about configuration? This would be the first step towards an useful tool.
fritzthecat
Has anyone got Core Tools working on Ganymede? The new software updates installer doesn’t seem to like the update site. I can see the site in my list, however selecting it doesn’t enable to ‘Install…’ button.
japher
There is a eclipse plugin, which finds unused dead java code. It also suggests to make code protected, private or final and it finds cyclic dependencies between classes.
Try this: UCDetector
jCoder
Here is the correct URL for UCDetector: http://www.ucdetector.org
jCoder