How to find all unused classes in IntelliJ IDEA?
Here's a quick drill-down to detect unused classes in IntelliJ IDEA employing the Code Inspection utility:
- Head to
Analyze
->Inspect Code
. - Choose the appropriate scope (whole project / specific modules).
- Once the analysis is done, scroll down to "Declaration redundancy" -> "Unused declaration".
This will unveil classes presumed to be unused. But verify each one manually, as they might be overlooked by the inspection if they are referenced using reflection, dependency injection, or in external modules.
Make inspections your laser targeting system
Inspecting classes only
You can tune the inspection to narrow down its focus only on classes. Ctrl+Shift+A
(Command+Shift+A
on macOS), type in "unused declar", in the popping window, affirm that classes are the only subjects for inspections.
Using 'inspect by name' for precision
When in need of a specific inspection, using Analyze/Run Inspection by Name
can be a timesaver. Enter "Unused decla" and select "Unused declaration Java|Declaration redundancy" from the dropdown.
Counter-verification: Your safety net
Avoid mishaps by employing "Find Usages" (Alt+F7) before removing a class. This will ensure the class isn't used in a dark corner of your codebase.
Beware of the invisible usages
Remember that dynamic usages, where classes are referenced through strings or configuration files, might not be visible to the IntelliJ IDEA scans.
Play safe while removing classes
Removing classes might sound fun until you eliminate the one used in an external API. Be cautious, as unintended behavior changes could crash your siesta.
Deep dive into code cleanup
Stay updated, stay sharp
Update your IntelliJ IDEA periodically. The more recent your IDE, the smarter and more precise its inspections get.
Inspections at your fingertips
Make your life easier with keyboard shortcuts like Ctrl+Alt+Shift+I
for an instant initiation of inspections.
Trust, but verify
Inspecting is not enough. Manually review the inspection results to avoid false red flags.
Constructor calls matter
The inspection looks for explicit constructor calls in your code. Still, some crafty usage cases from mighty frameworks may sneak by unnoticed.
Fortify for the win
Advanced tools like Fortify can help provide deeper code analysis but remember, quality often calls for a top-up on your credit card.
Free doesn't mean inefficient
If shelling out bucks isn't your thing, you can opt for free plugins and tools, offering competitive features for identifying unused classes.
Let's visualise
Your project is like a room full of boxes (classes)?
IntelliJ IDEA (Dr. Inspector) will turn each one inside out for you:
That's one clean and clutter-free room for you now.
Was this article helpful?