findbugs 可以检测未使用的公共方法

发布于 2024-10-12 06:00:28 字数 155 浏览 1 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

沩ん囻菔务 2024-10-19 06:00:28

作为 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.

妖妓 2024-10-19 06:00:28

我目前正在做的一个项目就是这样做的......现在还很早,所以可能还剩下一堆错误:

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

依 靠 2024-10-19 06:00:28

我认为 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.

呆头 2024-10-19 06:00:28

好吧,从 findbugs-1.3.9 开始,它似乎没有捕获未使用的方法。

当我在这个小样本上运行 findbugs 时:

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

它没有发现 foo 和 bar 都没有被使用。它确实发现 TestJava.j 是一个未使用的字段

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

findbugs 远非完美,但它仍然是一个非常有用的工具。

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.

撩心不撩汉 2024-10-19 06:00:28

好吧,既然您想走这条路线,尽管其他人已经回复了警告:),您可以复制并修改 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

半葬歌 2024-10-19 06:00:28

(对我来说)寻找未使用方法的候选者的最佳方法是使用覆盖工具,例如 emma。

检测您的应用程序,过度使用它并检查 emma 日志 - 会话期间未使用的方法可能未使用,您可以使用您最喜欢的 IDE(Eclipse,...)来检查未访问的方法方法调用层次结构。

我怀疑 find bugs 或任何其他代码分析器是否真的可以检测到未使用的方法,因为方法可能会被

  • 其他库调用(对于所有非私有方法)
  • ,通过反射 API远程
  • 调用(甚至是私有方法,从技术上讲)

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)
烟沫凡尘 2024-10-19 06:00:28

删除未使用的代码(包括未使用的公共方法)是混淆器所做的一件事。问题是,仅通过查看包含公共方法的类,您无法真正判断是否使用了公共方法。您需要查看将要运行的整个系统,因为公共方法可能会从任何地方调用。

针对整个系统(即您的代码和用于运行系统的所有库)运行混淆器可以帮助找到从未被调用的公共方法(警告:当然,反射可能会扰乱该结果!)。

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!).

错爱 2024-10-19 06:00:28

也许 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文