findbugs 可以检测未使用的公共方法
是否可以使用 FindBugs 检测源树中未使用的方法?我在 SO 上看到一些帖子,其中用户声称可以这样做,其他一些帖子询问如何在 FB 中执行此操作,还有一些帖子则声称 FB 不能这样做。
有谁确切知道这是如何做到的?我只对那些没有从其他地方显式调用的方法感兴趣,我不关心反射。
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, some 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
作为 FindBugs 团队的成员,我可以告诉您,不幸的是 FindBugs 并不这样做。如果您在我们的网站上搜索错误模式,则唯一提到的“未使用”检测器是未使用的字段。
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.
我目前正在做的一个项目就是这样做的......现在还很早,所以可能还剩下一堆错误:
https://github.com/mebigfatguy/deadmethods
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
我认为 Findbugs 很有可能报告公共方法,这些方法的使用方式与报告私有方法的方式不同(要么是这样,要么我正在考虑编译器标志:-)。
真正的问题是你为什么也想要?如果您正在编写一个封闭且永远不会扩展的程序,那么找到未使用的方法可以让您有机会删除它们。但是,如果您正在编写 API,您无法预测谁将需要这些方法,因此报告它们没有多大意义。
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.
好吧,从 findbugs-1.3.9 开始,它似乎没有捕获未使用的方法。
当我在这个小样本上运行 findbugs 时:
它没有发现 foo 和 bar 都没有被使用。它确实发现 TestJava.j 是一个未使用的字段。
findbugs 远非完美,但它仍然是一个非常有用的工具。
Well, as of findbugs-1.3.9, it appears it does not catch unused methods.
When I ran findbugs on this small sample:
It did not catch that neither foo nor bar are unused. It did catch that TestJava.j is an unused field.
findbugs is far from perfect, but it's still a pretty useful tool.
好吧,既然您想走这条路线,尽管其他人已经回复了警告:),您可以复制并修改 UPM 检测器 来完成您需要的操作。
为 FindBugs 编写一个检测器非常简单(特别是当您有一个很好的起点时)。请阅读此内容开始使用
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
(对我来说)寻找未使用方法的候选者的最佳方法是使用覆盖工具,例如 emma。
检测您的应用程序,过度使用它并检查 emma 日志 - 会话期间未使用的方法可能未使用,您可以使用您最喜欢的 IDE(Eclipse,...)来检查未访问的方法方法调用层次结构。
我怀疑 find bugs 或任何其他代码分析器是否真的可以检测到未使用的方法,因为方法可能会被
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
删除未使用的代码(包括未使用的公共方法)是混淆器所做的一件事。问题是,仅通过查看包含公共方法的类,您无法真正判断是否使用了公共方法。您需要查看将要运行的整个系统,因为公共方法可能会从任何地方调用。
针对整个系统(即您的代码和用于运行系统的所有库)运行混淆器可以帮助找到从未被调用的公共方法(警告:当然,反射可能会扰乱该结果!)。
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!).
也许 crap4j 就是您所需要的。它删除单元测试未到达的所有代码。这当然是最小化应用程序的困难方法。
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.