How find all unused classes in Intellij Idea?
Asked Answered
A

7

168

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?

Accentuate answered 20/3, 2014 at 2:3 Comment(3)
Just run inspection click right button and see this in the menu list.Accentuate
That's two questions -- they would be better as separate questions. Also serialVersionXXX is a bad idea for most projects.Ryanryann
It's a bad idea because very few developers know what it's for, and it is almost always misused. It is extremely rare in java to use the serialization mechanism to read and write objects at the byte level, and if you do the default serial version is usually sufficient to provide safety. A hard coded serialVersion must be updated manually every time the interface of a class changes, and every time the field list changes -- invariably, developers do not do this because they simply don't understand what the serialVersion is for. The main reason you see it in code is because of eclipse.Ryanryann
C
267
  • 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. :)
Carpology answered 7/7, 2016 at 11:13 Comment(10)
Posting nearly the exact same answer within a couple of minutes to several questions, suggest that one of them ios a duplicate of the other. please mark them as such.Maraud
One of them is specially for IntelliJ Idea usage, another is for general Java. I wouldn't say these are duplications.Carpology
Which is irrelevant then?Carpology
It ignores the "Only Check Classes" configuration, so I get lots and lots of unwanted results (unused methods, fields, etc., and, what is worse, many of them being false positives, for several reasons...).Tapetum
I have problem like @Tapetum and seems filter does not workBoggers
There is a change in Studio 3.0 Only check classes option comes after step: Select "Unused declaration Java|Declaration redundancy"Renner
We don't need to change global settings to run an inspection with different parameters. The Run inspection by name action will pop up the settings for the inspection.Broida
FTR: Ctrl+Shift+A is the default Studio keymap for "Help > Find Action...". It's Shift+Command+A on OSX.Letsou
A warning... Depending on the size of your project, this can take a long time - hours to days. You also may want to increase the amount of memory you allocate.Leucomaine
fails for spring, ignores autowired annotationEdna
L
14

Update 2023:

ctrl + alt + shift + I

enter image description here

Click on highlighted "Unused declaration"

enter image description here

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.

Leyes answered 9/11, 2021 at 14:49 Comment(0)
K
11

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.

Kerekes answered 29/4, 2015 at 4:43 Comment(2)
Well, it does grey out the class name when actually viewing the file and give you an intention action to "remove unused class", all that's missing is to be able to automatically find them. Certainly there are ways that could break things but that's true of many refactorings.Chondrule
Class.forName() is arguably not any different, from the point of analysis safety, than using reflection to read/set private fields on a class. So if IntelliJ will do this for fields, that is not an acceptable answer for why it won't do it for classes as well. In either case, the analysis result isn't 100% guaranteed to be safe.Numerology
D
3
  • Go to the code tab, analyze code, and run inspection by name.

navigation

  • Select class checkbox ✅

settings

Dactylo answered 26/12, 2022 at 20:26 Comment(0)
P
2

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

enter image description here

Poussette answered 20/3, 2014 at 8:39 Comment(2)
Ha! Seems not :-) I turned off everythings in "unused declaration" group and leave only "Unused symbol" with "check classes" only. But results do not change. :-(Accentuate
@Cherry, sorry to hear... Maybe you should file a defect to jetbrainsPoussette
O
1

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.

Oenomel answered 23/4, 2015 at 7:19 Comment(0)
O
0

Press Ctrl + Shift + A, in action tab choose Analyze -> Run Inspection by Name...-> Unused declaration -> Show popup -> Click OK enter image description here

Outsail answered 9/12, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.