Java 如何在 Intellij Idea 中找到所有未使用的类?

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

How find all unused classes in Intellij Idea?

javaintellij-ideadead-code

提问by Cherry

There is an inspection "Unused declaration" which can find all unused code in Intellij Idea. (see this question) But I want to find all unused classes, notmethods, variables etc. Onlyclasses. (it is difficult to find only classes in 3000 result list). How I can do that?

有一个检查“未使用的声明”,可以在 Intellij Idea 中找到所有未使用的代码。(见这个问题)但我想找到所有未使用的类,而不是方法、变量等。只有类。(很难在 3000 个结果列表中只找到类)。我怎么能做到这一点?

回答by Vic

Maybe you should look into the Unused Symbolinspection with the following settings:

也许您应该Unused Symbol使用以下设置查看检查:

enter image description here

在此处输入图片说明

回答by sunny

I am not sure if this will answer your question but I used a tool in past as Fortify to run code review rules on the project, that precisely points to the unused imports, dead code, unused classes etc. It is a paid software but I am sure there will be some free plugins/software will be available for same.

我不确定这是否能回答你的问题,但我过去使用了一个工具作为 Fortify 来运行项目的代码规则,它精确地指向未使用的导入、死代码、未使用的类等。这是一个付费软件,但我我相信会有一些免费的插件/软件可用。

回答by Andrew Janke

I don't think this is doable. I suspect this feature is intentionally left out of IDEs because it can't be used safely the way that other "remove unused XXX" refactorings can.

我不认为这是可行的。我怀疑这个特性是有意被排除在 IDE 之外的,因为它不能像其他“删除未使用的 XXX”重构那样安全地使用。

The unused declarations IDEA (and AFAIK, NetBeans) looks for are for private members and local variables: things that are not accessible, even dynamically, from outside that class or scope. (Well, at least without doing things with Reflection or JVM hacking that you're not supposed to.) No matter what outside code does with your library, it won't cause those things to be used, because their scope is limited and the IDE can see all of it. The compiler can determine this by looking at just your code.

IDEA(和 AFAIK、NetBeans)寻找的未使用声明是针对私有成员和局部变量的:那些不能从该类或作用域外部访问,甚至不能动态访问的东西。(好吧,至少没有用反射或 JVM hacking 做你不应该做的事情。)无论外部代码对你的库做了什么,它都不会导致这些东西被使用,因为它们的范围是有限的,而且IDE 可以看到所有这些。编译器可以通过查看您的代码来确定这一点。

For classes, even if they don't have publicaccess, they can be referenced dynamically with Class.forName(), and this actually happens in live code. So even if they're not apparently used within the code of your project, they might be used depending on what you or external code using your library runs. So the IDE can't guarantee that removing those classes won't change externally observable behavior.

对于类,即使它们没有public访问权限,也可以使用 动态引用它们Class.forName(),而这实际上发生在实时代码中。因此,即使它们显然没有在您的项目代码中使用,它们也可能会被使用,具体取决于您或使用您的库的外部代码运行的内容。因此,IDE 不能保证删除这些类不会改变外部可观察到的行为。

Which is why I think IDEA just doesn't provide this behavior: it might give users false expectations of safety, and removing them is not a safe refactoring.

这就是为什么我认为 IDEA 不提供这种行为的原因:它可能会给用户错误的安全期望,而删除它们并不是安全的重构。

回答by BlondCode

  • Press Ctrl+Shift+A(in Mac Command+Shift+A)
  • Enter "unused declar"
  • Double-click on "Unused declaration"
  • Ctrl+ Shift+ A(在 Mac Command+ Shift+ 中A
  • 输入“未使用的声明”
  • 双击“未使用的声明”

Settings will pop up

会弹出设置

  • Click on Java/Declaration redundancy/Unused declaration
  • on the right bottom select "On the fly editor settings"
  • untick check fields, ..., check parameters. Only Check Classes should be ticked.
  • Press OK
  • 点击 Java/Declaration 冗余/未使用的声明
  • 在右下角选择“即时编辑器设置”
  • 取消勾选检查字段,...,检查参数。只应勾选检查类。
  • 按确定

Settings closes

设置关闭

  • On the menu bar, click on Analyze / Run Inspection by Name (or Ctrl+Alt+Shift+I- in Mac Command+Option+Shift+I)
  • Insert text "Unused decla"
  • Select "Unused declaration Java|Declaration redundancy"
  • 在菜单栏上,单击按名称分析/运行检查(或Ctrl+ Alt+ Shift+ I- 在 Mac Command+ Option+ Shift+ 中I
  • 插入文本“未使用的声明”
  • 选择“未使用的声明Java|声明冗余”

Search starts

搜索开始

  • Check the job state on bottom of Idea, when finished: enjoy the results and the great feeling of cleaning up the messed code. :)
  • 完成后检查 Idea 底部的工作状态:享受结果和清理混乱代码的美妙感觉。:)