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

发布于 2025-02-08 08:09:41 字数 1551 浏览 2 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(21

猫七 2025-02-15 08:09:42

我找到了三叶草的覆盖工具,该工具可以进行代码,并突出显示所使用的代码且未使用的代码。与Google CodePro Analytics不同,它也适用于WebApplications(根据我的经验,我对Google CodePro可能是不正确的)。

我注意到的唯一缺点是它不考虑Java接口。

I found Clover coverage tool which instruments code and highlights the code that is used and that is unused. Unlike Google CodePro Analytics, it also works for WebApplications (as per my experience and I may be incorrect about Google CodePro).

The only drawback that I noticed is that it does not takes Java interfaces into account.

救赎№ 2025-02-15 08:09:42

我使用doxygen开发方法调用映射来找到从未调用的方法。在图上,您会发现没有呼叫者的方法簇岛。这对库不起作用,因为您需要始终从某个主要入口点开始。

I use Doxygen to develop a method call map to locate methods that are never called. On the graph you will find islands of method clusters without callers. This doesn't work for libraries since you need always start from some main entry point.

风和你 2025-02-15 08:09:41

效果很好的Eclipse插件是未使用的代码检测器

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

An Eclipse plugin that works reasonably well is 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).

微凉 2025-02-15 08:09:41

codepro 最近由Google发布了Eclipse Project。它是免费的,非常有效的。该插件具有'查找死亡代码'具有一个/多个入口点的功能(S)。效果很好。

CodePro was recently released by Google with the Eclipse project. It is free and highly effective. The plugin has a 'Find Dead Code' feature with one/many entry point(s). Works pretty well.

罪#恶を代价 2025-02-15 08:09:41

我会启动运行系统以保留代码使用的日志,然后开始检查几个月或数年不使用的代码。

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

当然,如果您处于方法级别,则应牢记性能。例如,这些方法只能记录其首次使用。我不知道这是如何在Java完成的。我们已经在SmallTalk中进行了此操作,这是一种动态语言,因此可以在运行时进行代码修改。我们在第一次记录方法后,用记录调用来启动所有方法,并卸载记录代码,因此一段时间后不再发生绩效惩罚。也许在Java中使用静态布尔国旗可以做类似的事情...

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

凉城凉梦凉人心 2025-02-15 08:09:41

我很惊讶 proguard 在这里没有提及。这是周围最成熟的产品之一。

proguard 是一个免费的Java类文件缩小,优化器,obfuscator,obfuscator,
和preverifier。它检测并删除未使用的类,字段,
方法和属性。它优化字节码并删除未使用的
指示。它重命名了其余类,字段和方法
使用简短的无意义名称。最后,它可以预先处理处理
Java 6的代码或Java Micro Edition。

proguard的某些用途是:

  • 创建更紧凑的代码,用于较小的代码档案,跨网络更快地传输,更快的加载和较小的内存
    足迹。
  • 制作程序和库更难反向工程。
  • 列出死亡代码,因此可以从源代码中删除。
  • 重新定位和预验证Java 6或更高的现有类文件,以充分利用其更快的班级加载。

列表死亡代码的示例: https:// https://www.guardsquare.com/ en/products/proguard/手册/示例#死刑

I'm suprised ProGuard hasn't been mentioned here. It's one of the most mature products around.

ProGuard is a free Java class file shrinker, optimizer, obfuscator,
and preverifier. It detects and removes unused classes, fields,
methods, and attributes. It optimizes bytecode and removes unused
instructions. It renames the remaining classes, fields, and methods
using short meaningless names. Finally, it preverifies the processed
code for Java 6 or for Java Micro Edition.

Some uses of ProGuard are:

  • Creating more compact code, for smaller code archives, faster transfer across networks, faster loading, and smaller memory
    footprints.
  • Making programs and libraries harder to reverse-engineer.
  • Listing dead code, so it can be removed from the source code.
  • Retargeting and preverifying existing class files for Java 6 or higher, to take full advantage of their faster class loading.

Here example for list dead code: https://www.guardsquare.com/en/products/proguard/manual/examples#deadcode

冷默言语 2025-02-15 08:09:41

我已经知道在Eclipse上做的一件事是在一个班级上,将其所有方法更改为私人,然后看看我得到了什么投诉。对于所使用的方法,这将引起错误,我将它们返回到我可以的最低访问级别。对于未使用的方法,这将引起有关未使用方法的警告,然后可以删除这些方法。作为奖励,您经常发现一些可以并且应该私下的公共方法。

但这是非常手动的。

One thing I've been known to do in Eclipse, on a single class, is change all of its methods to private and then see what complaints I get. For methods that are used, this will provoke errors, and I return them to the lowest access level I can. For methods that are unused, this will provoke warnings about unused methods, and those can then be deleted. And as a bonus, you often find some public methods that can and should be made private.

But it's very manual.

黯然#的苍凉 2025-02-15 08:09:41

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

emma 将为您提供有关代码的任何给定运行的类别的百分比。

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

Emma and Eclemma will give you nice reports of what percentage of what classes are run for any given run of the code.

榆西 2025-02-15 08:09:41

我们已经开始使用查找错误来帮助识别我们代码库的富含目标环境中的某些funk,以进行重构。我还会考虑结构101 识别代码库体系结构中太复杂的斑点,所以您知道真正的沼泽在哪里。

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

风吹过旳痕迹 2025-02-15 08:09:41

从理论上讲,您无法确定地找到未使用的代码。有一个数学证明(嗯,这是一个更一般定理的特殊情况)。如果您很好奇,请查找停止问题。

这可以在许多方面以Java代码表现出来:

  • 基于用户输入,配置文件,数据库条目等加载类;
  • 加载外部代码;
  • 将物体树传递到第三方库;
  • 也就是说

,我使用Idea Intellij作为我的选择IDE,它具有广泛的分析工具,用于在模块,未使用的方法,未使用的成员,未使用的类等之间进行findign依赖性。称为未使用的标记,但公共方法需要更广泛的分析。

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:

  • 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.

独﹏钓一江月 2025-02-15 08:09:41

在Eclipse goto Windows>偏好> Java>编译器>错误/警告
并将所有这些都更改为错误。修复所有错误。这是最简单的方法。美好是,这将使您在编写时清理代码。

ScreenShot Eclipse代码:

alt

In Eclipse Goto Windows > Preferences > Java > Compiler > Errors/Warnings
and change all of them to errors. Fix all the errors. This is the simplest way. The beauty is that this will allow you to clean up the code as you write.

Screenshot Eclipse Code :

enter image description here

一梦等七年七年为一梦 2025-02-15 08:09:41

结构101 slice perspective 将提供一个列表(和依赖图)(和依赖图)(和依赖图)任何“孤儿”或“孤儿

The Structure101 slice perspective will give a list (and dependency graph) of any "orphans" or "orphan groups" of classes or packages that have no dependencies to or from the "main" cluster.

场罚期间 2025-02-15 08:09:41

Intellij具有用于检测未使用的代码的代码分析工具。您应该尝试使尽可能多的字段/方法/类尽可能多地进行非公开,这将显示更多未使用的方法/字段/类,

我还将尝试找到重复的代码作为减少代码卷的一种方式。

我的最后建议是尝试找到开源代码,如果使用将使您的代码变得更简单。

IntelliJ has code analysis tools for detecting code which is unused. You should try making as many fields/methods/classes as non-public as possible and that will show up more unused methods/fields/classes

I would also try to locate duplicate code as a way of reducing code volume.

My last suggestion is try to find open source code which if used would make your code simpler.

倾城°AllureLove 2025-02-15 08:09:41

DCD不是某些IDE的插件,但可以从ANT或独立运行。它看起来像静态工具,它可以做PMD和FindBugs无法做的事情。我会尝试的。

PS如下所述,该项目现在生活在 github

DCD is not a plugin for some IDE but can be run from ant or standalone. It looks like a static tool and it can do what PMD and FindBugs can't. I will try it.

P.S. As mentioned in a comment below, the Project lives now in GitHub.

烟花易冷人易散 2025-02-15 08:09:41

有一些介绍代码并提供代码覆盖数据的工具。这使您可以看到(随着代码运行)被调用多少。您可以获取这些工具中的任何一种,以找出您拥有多少孤儿代码。

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.

蓝眼睛不忧郁 2025-02-15 08:09:41
  • Findbugs非常适合这种事情。
  • PMD(项目杂物检测器)是可以使用的另一种工具。

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

  • FindBugs is excellent for this sort of thing.
  • PMD (Project Mess Detector) is another tool that can be used.

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

嘦怹 2025-02-15 08:09:41

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

仍然,Emma非常有用。

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 :) )

Still, EMMA is very useful.

乜一 2025-02-15 08:09:41

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

但是,这与确定真实的死亡代码不同。它仅标识测试涵盖(或未涵盖)的代码。这可以为您提供误报(如果您的测试不涵盖所有方案)以及假否定性(如果您的测试访问代码实际上在现实世界中从未使用过)。

我想真正识别死亡代码的最佳方法是在实时运行环境中使用覆盖范围工具来启动您的代码,并在长时间内分析代码覆盖范围。

如果您在负载均衡的冗余环境中运行(如果不是,为什么不呢?),那么我想只能仪器的一个实例一个实例并配置负载均衡器,以便随机但很小的部分您的用户在您的仪器实例上运行。如果您在很长一段时间内执行此操作(以确保您涵盖了所有现实世界的使用情况 - 这种季节性变化),您应该能够准确查看在现实世界中使用哪些区域以及哪些部分访问了代码的哪些区域以及哪些部分实际上从未访问过,因此死了。

我从未亲自看到这项工作,也不知道上述工具如何用于仪器和分析未通过测试套件调用的代码 - 但我相信它们可以。

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.

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.

痴骨ら 2025-02-15 08:09:41

有一个Java项目 - 死去代码检测器(dcd)。对于源代码,它似乎不能很好地工作,但是对于.jar文件 - 真的很好。另外,您可以按课堂和方法过滤。

There is a Java project - Dead Code Detector (DCD). For source code it doesn't seem to work well, but for .jar file - it's really good. Plus you can filter by class and by method.

一桥轻雨一伞开 2025-02-15 08:09:41

netbeans 这是NetBeans 死亡代码检测器

如果它可以链接到并突出未使用的代码,那就更好了。您可以在此处投票并评论: bug 181458-查找未使用的公共类,方法,fields

Netbeans here is a plugin for Netbeans dead code detector.

It would be better if it could link to and highlight the unused code. You can vote and comment here: Bug 181458 - Find unused public classes, methods, fields

天煞孤星 2025-02-15 08:09:41

Eclipse可以显示无法达到的代码/突出显示代码。 Junit可以显示您的代码覆盖范围,但是您需要一些测试,并且必须确定是否丢失了相关测试,或者代码真的未使用。

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.

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