166

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?

reevesy
  • 3,452
  • 1
  • 26
  • 23
Cherry
  • 31,309
  • 66
  • 224
  • 364
  • Just run inspection click right button and see this in the menu list. – Cherry Mar 20 '14 at 02:38
  • That's two questions -- they would be better as separate questions. Also serialVersionXXX is a bad idea for most projects. – Software Engineer Mar 20 '14 at 15:47
  • 2
    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. – Software Engineer Mar 23 '14 at 17:39

7 Answers7

261
  • 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. :)
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
BlondCode
  • 4,009
  • 1
  • 19
  • 18
  • 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. – Jaap Jul 07 '16 at 11:24
  • 5
    One of them is specially for IntelliJ Idea usage, another is for general Java. I wouldn't say these are duplications. – BlondCode Jul 07 '16 at 11:27
  • Which is irrelevant then? – BlondCode Jul 11 '16 at 14:16
  • 8
    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...). – thelawnmowerman Mar 05 '17 at 14:30
  • I have problem like @thelawnmowerman and seems filter does not work – Mahdi Aug 20 '17 at 13:50
  • 6
    There is a change in Studio 3.0 Only check classes option comes after step: Select "Unused declaration Java|Declaration redundancy" – Sourabh Saldi Dec 05 '17 at 11:55
  • 4
    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. – TWiStErRob Jul 05 '18 at 12:22
  • FTR: Ctrl+Shift+A is the default Studio keymap for "Help > Find Action...". It's Shift+Command+A on OSX. – Tbadams Mar 21 '19 at 16:57
  • 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. – kc2001 Mar 24 '19 at 12:33
  • fails for spring, ignores autowired annotation – tgkprog Feb 21 '22 at 18:05
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.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
  • 7
    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. – Rob Fletcher Nov 20 '15 at 17:19
  • 3
    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. – Dogs Mar 02 '17 at 18:29
7

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.

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
2

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

enter image description here

Vic
  • 21,473
  • 11
  • 76
  • 97
  • 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. :-( – Cherry Mar 20 '14 at 09:34
  • @Cherry, sorry to hear... Maybe you should file a defect to jetbrains – Vic Mar 20 '14 at 10:45
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.

sunny
  • 303
  • 2
  • 7
0

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

Thuan Tran
  • 351
  • 2
  • 7
0
  • Go to the code tab, analyze code, and run inspection by name.

navigation

  • Select class checkbox ✅

settings

Jorge Tovar
  • 1,374
  • 12
  • 17