简体   繁体   中英

How find all unused classes in Intellij Idea?

There is an inspection "Unused declaration" which can find all unused code in Intellij Idea. (see How to use IntelliJ IDEA to find all unused code? ) But I want to find all unused classes, not methods, variables etc. Only classes. (it is difficult to find only classes in 3000 result list). How I can do that?

  • Press Ctrl + Shift + A (in Mac Command + Shift + A )
  • Enter "unused declar"
  • Double-click on "Unused declaration"

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

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"

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

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.

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.

For classes, even if they don't have public access, 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.

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.

Maybe you should look into the Unused Symbol inspection with the following settings:

在此处输入图片说明

我不确定这是否能回答您的问题,但是我过去曾使用过一个工具作为Fortify在项目上运行代码审查规则,该规则精确地指向未使用的导入,无效代码,未使用的类等。这是一个付费软件,但我确保会有一些免费的插件/软件可以使用。

I have found that intellij will declared classes that are not instantiated as unused even if they are actually used in declarations. So what I typically do is Analyze and then in Unused declaration I right click each one and click find usages. If a popup shows up saying "no usages" then can safe deleted from the problems menu directly as shown below. This is the quickest way I've found - otherwise IntelliJ autodelte will remove classes that are used, just not instantiated.

在此处输入图片说明

Note in gif you cannot see the popup that informed me that there were no usages of this class that I chose to delete.

Press Ctrl + Shift + A, in action tab choose Analyze -> Run Inspection by Name...-> Unused declaration -> Show popup -> Click OK 在此处输入图像描述

  • Go to the code tab, analyze code , and run inspection by name .

导航

  • Select class checkbox ✅

设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM