如何在java项目中找到未使用/死代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:56:42  来源:igfitidea点击:

How to find unused/dead code in java projects

javarefactoringdead-code

提问by knatten

What tools do you use to find unused/dead code in large java projects? Our product has been in development for some years, and it is getting very hard to manually detect code that is no longer in use. We do however try to delete as much unused code as possible.

您使用什么工具在大型 Java 项目中查找未使用/死代码?我们的产品已经开发了几年,手动检测不再使用的代码变得非常困难。但是,我们会尝试删除尽可能多的未使用代码。

Suggestions for general strategies/techniques (other than specific tools) are also appreciated.

对一般策略/技术(特定工具除外)的建议也受到赞赏。

Edit:Note that we already use code coverage tools (Clover, IntelliJ), but these are of little help. Dead code still has unit tests, and shows up as covered. I guess an ideal tool would identify clusters of code which have very little other code depending on it, allowing for docues manual inspection.

编辑:请注意,我们已经使用了代码覆盖工具(Clover、IntelliJ),但这些工具帮助不大。死代码仍然有单元测试,并显示为覆盖。我想一个理想的工具可以识别几乎没有其他代码依赖的代码集群,从而允许手动检查文档。

采纳答案by akuhn

I would instrument the running system to keep logs of code usage, and then start inspecting code that is not used for months or years.

我会检测正在运行的系统以保留代码使用日志,然后开始检查数月或数年未使用的代码。

For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.

例如,如果您对未使用的类感兴趣,则可以在创建实例时检测所有类以记录日志。然后一个小脚本可以将这些日志与完整的类列表进行比较,以找到未使用的类。

Of course, if you go at the method level you should keep performance in mind. For example, the methods could only log their first use. I dont know how this is best done in Java. We have done this in Smalltalk, which is a dynamic language and thus allows for code modification at runtime. We instrument all methods with a logging call and uninstall the logging code after a method has been logged for the first time, thus after some time no more performance penalties occur. Maybe a similar thing can be done in Java with static boolean flags...

当然,如果您进入方法级别,您应该牢记性能。例如,这些方法只能记录它们的第一次使用。我不知道在 Java 中如何最好地做到这一点。我们已经在 Smalltalk 中做到了这一点,它是一种动态语言,因此允许在运行时修改代码。我们使用日志记录调用检测所有方法,并在第一次记录方法后卸载日志记录代码,因此一段时间后不会再发生性能损失。也许可以用静态布尔标志在 Java 中完成类似的事情......

回答by Matthias Winkelmann

Eclipse can show/highlight code that can't be reached. JUnit can show you code coverage, but you'd need some tests and have to decide if the relevant test is missing or the code is really unused.

Eclipse 可以显示/突出显示无法访问的代码。JUnit 可以向您显示代码覆盖率,但您需要进行一些测试,并且必须确定相关测试是否丢失或代码是否真的未使用。

回答by Vaibhav

There are tools which profile code and provide code coverage data. This lets you see (as code is run) how much of it is being called. You can get any of these tools to find out how much orphan code you have.

有一些工具可以分析代码并提供代码覆盖率数据。这让您可以看到(在代码运行时)有多少被调用。您可以使用这些工具中的任何一种来了解您拥有多少孤立代码。

回答by cletus

In theory, you can't deterministically find unused code. Theres a mathematical proof of this (well, this is a special case of a more general theorem). If you're curious, look up the Halting Problem.

理论上,您无法确定地找到未使用的代码。这有一个数学证明(好吧,这是更一般定理的一个特例)。如果您很好奇,请查看停机问题。

This can manifest itself in Java code in many ways:

这可以通过多种方式在 Java 代码中体现出来:

  • Loading classes based on user input, config files, database entries, etc;
  • Loading external code;
  • Passing object trees to third party libraries;
  • etc.
  • 根据用户输入、配置文件、数据库条目等加载类;
  • 加载外部代码;
  • 将对象树传递给第三方库;
  • 等等。

That being said, I use IDEA IntelliJ as my IDE of choice and it has extensive analysis tools for findign dependencies between modules, unused methods, unused members, unused classes, etc. Its quite intelligent too like a private method that isn't called is tagged unused but a public method requires more extensive analysis.

话虽如此,我使用 IDEA IntelliJ 作为我选择的 IDE,它具有广泛的分析工具,用于查找模块、未使用的方法、未使用的成员、未使用的类等之间的依赖关系。它也非常智能,就像一个不被调用的私有方法一样标记为未使用但公共方法需要更广泛的分析。

回答by Alan

We've started to use Find Bugsto help identify some of the funk in our codebase's target-rich environment for refactorings. I would also consider Structure 101to identify spots in your codebase's architecture that are too complicated, so you know where the real swamps are.

我们已经开始使用Find Bugs来帮助识别我们代码库的目标丰富的环境中的一些问题,以进行重构。我还会考虑使用Structure 101来识别代码库架构中过于复杂的点,以便您知道真正的沼泽在哪里。

回答by Mikezx6r

An Eclipse plugin that works reasonably well is Unused Code Detector.

一个运行良好的 Eclipse 插件是Unused Code Detector

It processes an entire project, or a specific file and shows various unused/dead code methods, as well as suggesting visibility changes (i.e. a public method that could be protected or private).

它处理整个项目或特定文件并显示各种未使用/死代码方法,以及建议可见性更改(即可以受保护或私有的公共方法)。

回答by Vladimir Dyuzhev

User coverage tools, such as EMMA. But it's not static tool (i.e. it requires to actually run the application through regression testing, and through all possible error cases, which is, well, impossible :) )

用户覆盖工具,例如 EMMA。但它不是静态工具(即它需要通过回归测试和所有可能的错误情况实际运行应用程序,这是不可能的:))

Still, EMMA is very useful.

尽管如此,EMMA 还是非常有用的。

回答by jamesh

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

使用测试覆盖工具来检测您的代码库,然后运行应用程序本身,而不是测试。

Emmaand Eclemmawill give you nice reports of what percentage of what classes are run for any given run of the code.

EmmaEclemma会给你很好的报告,说明对于任何给定的代码运行,运行的类的百分比。

回答by Vihung

Code coverage tools, such as Emma, Cobertura, and Clover, will instrument your code and record which parts of it gets invoked by running a suite of tests. This is very useful, and should be an integral part of your development process. It will help you identify how well your test suite covers your code.

代码覆盖工具,例如 Emma、Cobertura 和 Clover,将检测您的代码并通过运行一组测试记录它的哪些部分被调用。这非常有用,并且应该是您开发过程中不可或缺的一部分。它将帮助您确定测试套件对代码的覆盖程度。

However, this is not the same as identifying real dead code. It only identifies code that is covered (or not covered) by tests. This can give you false positives (if your tests do not cover all scenarios) as well as false negatives (if your tests access code that is actually never used in a real world scenario).

但是,这与识别真正的死代码不同。它只识别测试覆盖(或未覆盖)的代码。这可能会给您带来误报(如果您的测试未涵盖所有场景)以及误报(如果您的测试访问的代码实际上从未在现实世界中使用过)。

I imagine the best way to really identify dead code would be to instrument your code with a coverage tool in a live running environment and to analyse code coverage over an extended period of time.

我想真正识别死代码的最好方法是在实时运行的环境中使用覆盖工具检测您的代码,并在很长一段时间内分析代码覆盖率。

If you are runnning in a load balanced redundant environment (and if not, why not?) then I suppose it would make sense to only instrument one instance of your application and to configure your load balancer such that a random, but small, portion of your users run on your instrumented instance. If you do this over an extended period of time (to make sure that you have covered all real world usage scenarios - such seasonal variations), you should be able to see exactly which areas of your code are accessed under real world usage and which parts are really never accessed and hence dead code.

如果您在负载平衡的冗余环境中运行(如果没有,为什么不呢?)那么我认为只检测您的应用程序的一个实例并配置您的负载平衡器是有意义的,以便随机但很小的一部分您的用户在您的检测实例上运行。如果您在很长一段时间内这样做(以确保您已经涵盖了所有现实世界的使用场景 - 例如季节性变化),您应该能够准确地看到您的代码的哪些区域在现实世界的使用中被访问以及哪些部分真的永远不会被访问,因此是死代码。

I have never personally seen this done, and do not know how the aforementioned tools can be used to instrument and analyse code that is not being invoked through a test suite - but I am sure they can be.

我个人从未亲眼见过这样做,也不知道如何使用上述工具来检测和分析未通过测试套件调用的代码 - 但我相信它们可以。

回答by graveca

  • FindBugs is excellent for this sort of thing.
  • PMD (Project Mess Detector) is another tool that can be used.
  • FindBugs 非常适合这类事情。
  • PMD(Project Mess Detector)是另一种可以使用的工具。

However, neither can find public static methodsthat are unused in a workspace. If anyone knows of such a tool then please let me know.

但是,两者都找不到工作区中未使用的公共静态方法。如果有人知道这样的工具,请告诉我。