天天看点

如何在Java项目中查找未使用/无效的代码

本文翻译自:How to find unused/dead code in java projects

What tools do you use to find unused/dead code in large java projects?

您使用什么工具在大型Java项目中查找未使用/无效的代码?

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.

但是,我们确实尝试删除尽可能多的未使用的代码。

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.

编辑:请注意,我们已经使用了代码覆盖率工具(Clover,IntelliJ),但是这些帮助不大。

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.

我猜一个理想的工具可以识别出几乎没有其他代码依赖的代码集群,从而可以对文档进行手动检查。

#1楼

参考:https://stackoom.com/question/gHn/如何在Java项目中查找未使用-无效的代码

#2楼

DCD is not a plugin for some IDE but can be run from ant or standalone.

DCD不是某些IDE的插件,但可以从ant或独立运行。

It looks like a static tool and it can do what PMD and FindBugs can't .

它看起来像一个静态工具, 可以完成PMD和FindBugs无法完成的工作 。

I will try it.

我会试试。

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

PS如下面的评论中所述,该项目现在位于GitHub中 。

#3楼

Eclipse can show/highlight code that can't be reached.

Eclipse可以显示/突出显示无法访问的代码。

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.

JUnit可以向您显示代码覆盖率,但是您需要进行一些测试,并且必须确定是否缺少相关测试或代码是否确实未使用。

#4楼

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.

您可以使用这些工具中的任何一种来查明您有多少孤立代码。

#5楼

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,它具有用于模块,未使用的方法,未使用的成员,未使用的类等之间的依赖关系的广泛分析工具。它非常智能,就像未调用的私有方法是标记为未使用,但公共方法需要更广泛的分析。

#6楼

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.

我还将考虑使用结构101来识别代码库体系结构中过于复杂的位置,以便您知道真正的沼泽在哪里。