Java 如何使用 IntelliJ IDEA 查找所有未使用的代码?

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

How to use IntelliJ IDEA to find all unused code?

javarefactoringintellij-ideacode-metricscode-inspection

提问by pathikrit

When I am in a .java file the unused code is usually grayed out or has a green underline saying this code will probably (probably because of some weird JNI/Reflection corner cases) be unused. But I have this project with thousands of Java files and I want to find ALL INSTANCES of such probable-unused codes. How can I do that in IntelliJ IDEA?

当我在 .java 文件中时,未使用的代码通常变灰或带有绿色下划线,表示此代码可能(可能是因为一些奇怪的 JNI/反射极端情况)未被使用。但是我有这个包含数千个 Java 文件的项目,我想找到这种可能未使用的代码的所有实例。我怎样才能在 IntelliJ IDEA 中做到这一点?

采纳答案by CrazyCoder

Just use Analyze | Inspect Codewith appropriate inspection enabled (Unused declarationunder Declaration redundancygroup).

只需Analyze | Inspect Code启用适当的检查即可使用(声明冗余组下的未使用声明)。

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

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

回答by Johnny

In latest IntelliJ versions, you should run it from Analyze->Run Inspection By Name:

在最新的 IntelliJ 版本中,您应该从Analyze->Run Inspection By Name运行它:

enter image description here

在此处输入图片说明

Than, pick Unused declaration:

然后,选择Unused 声明

enter image description here

在此处输入图片说明

And finally, uncheckthe Include test sources:

最后,取消包括测试来源

enter image description here

在此处输入图片说明