views:

225

answers:

4

I'm a NetBeans 6.5 user and am searching for a way of generating a list of methods/classes that are not used within a set project group.

I know I can right click on each method and select "Find Usages" but I'm looking for an automation to that process.

I'm willing to look at something that runs outside of netbeans, as long as it generates a list of cruft methods/classes.

Thanks.

+4  A: 

You can switch on the unused code rules in PMD (there's a NetBeans plugin), then run PMD over a whole project.

Bill the Lizard
Seems to be a partial solution, but it only seems to allow for the detection of private methods/fields.
Allain Lalonde
I was about to post the same after looking at PMD. I've also had the same issue, in the past dealing with legacy codebases I've only detected unused public methods (unused within the codebase) through an evil mixture of grep, python, etc etc. Please post if you know of any other tools.
Steve B.
Public methods are assumed to be used outside your code (they're your API), so PMD doesn't flag them.
Bill the Lizard
You don't really want to get rid of public methods, do you? That could (and probably will) break other people's code.
Bill the Lizard
I do indeed. All the code is my own. Declaring a method/class public is the only way of accessing it from another package within the same project. I'd like to have an "internal" keyword in java, but alas...
Allain Lalonde
There are times where you're dealing with a legacy codebase, all the useage is internal to the codebase, but there are public methods that have long since been forgotten.
Steve B.
This is a bit farfetched, but could you write a script to change all the public methods to private, just to run those PMD rules? You'd have to rename all your currently private methods so you can undo the damage.
Bill the Lizard
That's a good suggestion, although it can actually be a bit more complicated,as with a convoluted enough codebase you can actually have entirely dead method chains - more like unused objects in a running JVM, it needs to be dealt with as a graph.
Steve B.
Wouldn't you at least get the start of the dead chain? Then run PMD again and repeat until you've removed all the links in the chain. I can imagine that recursive calls might not get picked up, though.
Bill the Lizard
+4  A: 

Obfuscators like proguard can shrink your jars by removing unused methods/classes. Maybe it is possible to get a verbose output which contains the list of removed (hence unused) classes/methods.

Here you can find more information about finding dead code with proguard.

asalamon74
+1  A: 

There are several tools you can use to help find these and other problems:

Joe Attardi
I'm actively using these. Great tools. They don't address the problem in the case of public methods/classes though.
Allain Lalonde
+1  A: 

Use a test coverage tool to instrument your codebase, then run the application itself, not the tests.

Emma and Eclemma will give you nice reports of what percentage of what classes are run for any given run of the code.

jamesh