Can findbugs detect unused public methods

五迷三道 提交于 2019-11-29 10:02:50

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!