在 Java Eclipse 项目中查找未使用的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/665563/
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 17:38:31  来源:igfitidea点击:

Find unused classes in a Java Eclipse project

javaeclipserefactoring

提问by jjujuma

I have a large Eclipse project in which there exist several classes which, although they ceased to be used anywhere, were never marked @Deprecated.

我有一个大型 Eclipse 项目,其中存在几个类,尽管它们不再在任何地方使用,但从未被标记为 @Deprecated。

How can I easily find all of these?

我怎样才能轻松找到所有这些?

回答by VonC

I also like to use UCDetector:

我也喜欢使用UCDetector

screenshot

截屏

UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private.

UCDetector (Unecessary Code Detector) 是一个开源 eclipse 插件工具,用于查找不必要的(死的)公共 Java 代码。它还尝试使代码成为最终的、受保护的或私有的。

Bonus: it can also find cyclic dependencies between classes

奖励:它还可以找到类之间的循环依赖

(also a number of other tools-- including Findbugs -- knows how do do that too)

(还有许多其他工具——包括 Findbugs——也知道如何做到这一点)



Caveat: Cidmentions in the comments:

警告Cid在评论中提到:

UCDetector shall not work if there are interface implementations which will be known only at runtime.
It incorrectly marks the implementation classes as unused.

如果存在仅在运行时才知道的接口实现,则 UCDetector 将无法工作。
它错误地将实现类标记为未使用。



Update 2017: static code analysis has evolved quite a bit in 8 years.
Using SonarLint for Eclipse, you can use the the latest SonarJava 4.6 pluginto analyze your code.
It will find dead code.

2017 年更新:静态代码分析在 8 年里有了很大的发展。
使用SonarLint for Eclipse,您可以使用最新的SonarJava 4.6 插件来分析您的代码。
它会找到死代码

回答by rustyx

ProGuardcan be used to print a report of unused classes/methods. It's a pain to supply all the dependent jars to it, though.

ProGuard可用于打印未使用的类/方法的报告。但是,为它提供所有依赖的 jar 是很痛苦的。

These options list unused classes, fields, and methods in the application mypackage.MyApplication:

这些选项列出了应用程序 mypackage.MyApplication 中未使用的类、字段和方法:

-injars      in.jar
-libraryjars <java.home>/lib/rt.jar

-dontoptimize
-dontobfuscate
-dontpreverify
-printusage

-keep public class mypackage.MyApplication {
    public static void main(java.lang.String[]);
}

回答by Hasan Jamshaid

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

只需使用分析 | 在启用适当检查的情况下检查代码(声明冗余组下的未使用声明)。

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"

使用 IntelliJ 11 CE,您现在可以“分析 | 按名称运行检查 ... | 未使用的声明”