Reputation: 33618
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?
Upvotes: 176
Views: 103928
Reputation: 16319
Update 2023:
ctrl + alt + shift + I
Click on highlighted "Unused declaration"
Select the options you care about. In this case, I chose only "Classes" to find unused classes.
BE CAREFUL WITH THIS THOUGH I've found that classes that are used inherently by reference, for example through hibernate, will say they're unused but right clicking class and doing Find Usages shows otherwise. This only checks if constructor is called explicitly.
Upvotes: 23
Reputation: 1879
Upvotes: 4
Reputation: 447
Press Ctrl + Shift + A, in action tab choose Analyze -> Run Inspection by Name...-> Unused declaration -> Show popup -> Click OK
Upvotes: 1
Reputation: 4179
Settings will pop up
Settings closes
Search starts
Upvotes: 270
Reputation: 23898
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.
Upvotes: 11
Reputation: 303
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.
Upvotes: 1
Reputation: 22051
Maybe you should look into the Unused Symbol
inspection with the following settings:
Upvotes: 2