开发者

Can findbugs detect unused public methods

Is it possible to detect unused methods in a source tree using FindBugs? I see some posts on SO where users are claiming to do that, s开发者_如何转开发ome others asking how to do this in FB and others where they claim FB cannot do this.

Does anyone know for sure how this is done? I am only interested in methods that are not explicitly called from elsewhere, I don't care about reflection.


as a member of the FindBugs team I can tell you that unfortunately FindBugs does not do this. If you search through the bug patterns on our website, the only mentions of "unused" detectors is for unused fields.


I have a project i'm currently working on that does just this.... It's very early on tho, so probably a bunch of bugs left:

https://github.com/mebigfatguy/deadmethods


I suppose it would be quite possible for Findbugs to report on public methods which are not used the same way it reports on privates (either that or I'm thinking of a compiler flag :-).

The real question is why would you want too? If you are writing a program which is closed and will never be extended, then locating unused methods gives you an opportunity to remove them. But if you are writing an API you cannot predict who will need those methods so there is not much point in reporting on them.


Well, as of findbugs-1.3.9, it appears it does not catch unused methods.

When I ran findbugs on this small sample:

public class TestJava
{
 int j;
 public static void main(String[] args)
 { 
   System.out.println("Nothing.");
 }
 public void foo()
 {
 }
 public static void bar()
 {
 }
}

It did not catch that neither foo nor bar are unused. It did catch that TestJava.j is an unused field.

Unused field
This field is never used.  Consider removing it from the class.

findbugs is far from perfect, but it's still a pretty useful tool.


Well, since you want to go down this route despite warnings from the others who've responded :), you can copy and modify the UPM detector to do what you need.

Its really simple to write a detector for FindBugs (especially when you've got a great starting point). Read this to get you started


The best approach (to me) to find candidates for unused methods is to use coverage tools, like emma.

Instrument you application, use it excessivly and examine the emma logs - methods that not have been used during the session may be unused and you can use your favourite IDE (eclipse, ...) to examine the unvisited methods call hierarchies.

I doubt, that find bugs or any other code analyser can really detect unused methods, because methods may be

  • called by other libraries (for all non-private methods)
  • called remotely
  • invoked through reflection API (even private methods, technically spoken)


Removing unused code (including unused public methods) is one thing obfuscators do. The problem is that you can't really tell if a public method is used by just looking at the class that contains it. You need to look at the whole system that is going to run, because a public method might be called from everywhere.

Running the obfuscator against the whole system (i.e. your code and all libraries used to run the system) can help find public methods that are never called (caveat: reflection can mess with that result, of course!).


Maybe crap4j is what you need. It removes all code that is not reached by unit tests. This is of course the hard way to minimize your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜