IntelliJ Community & Ultimate
Manually you would do:
- go to IntelliJ and open your project
- Press Analyze on the menu bar
- Run Inspection by Name...
- type "Unused declaration"
- select only classes
- press OK
If you want to batch delete, you can select multiple entries then R-Click and Safe Delete.
This is explained also here.
In order to run the same as the above from command line and generate a report you would need to follow the steps described here. This last page might not provide enough information. Therefore I will summarize the steps required to make this work.
In order to run the inspections offline you can:
- go to IntelliJ and open your project
- Settings
- Editor
- Inspections
- Select a profile from the profile drop down and clone it then give it a name (eg: UnusedDeclaration)
- next deselect everything except Java > Declaration redundancy > Unused declaration
- select Java > Declaration redundancy > Unused declaration, in order to customize this inspection, then uncheck everything and select only Classes so that only classes are checked
- OK
The result of these steps is that you obtained in your project a new XML with all your settings. For example, if you named your profile "UnusedDeclaration" then you will get <proj>/.idea/inspectionProfiles/UnusedDeclaration.xml
.
Now is important that you close your IntelliJ instances else will not work.
Go to command prompt and type similar to this (for a Windows machine):
D:\>C:\Users\andre\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\171.4249.39\bin\inspect.bat D:\MyProjects\MyProject D:\MyProjects\MyProject\.idea\inspectionProfiles\UnusedDeclaration.xml D:\results
Note: this is for a Windows machine, but for a Linux or Mac you would use inspect.sh instead of inspect.bat
.
Note: I used Toolbox to install IntelliJ. This is useful as I can manage the entire suite installation and updates. Therefore my IntelliJ installation is located at C:\Users\andre\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\171.4249.39\
.
By running the previous command you will get the following behavior:
- inspect.bat is run with the associated command line arguments
- Argument 1: project directory (eg:
D:\MyProjects\MyProject
)
- Argument 2: profile XML (eg:
D:\MyProjects\MyProject\.idea\inspectionProfiles\UnusedDeclaration.xml
)
- Argument 3: output path (eg:
D:\results
)
Therefore you get your report with all unused classes under D:\results\unused.xml
.
IntelliJ Ultimate
The following can be done manually but I found it useful for scenarios where you need to analyze how your code is being used, including determining if a class/namespace/module is not used at all.
You can:
- go to IntelliJ Ultimate and open your project
- Analyze
- Analyze Dependency Matrix
- OK
A matrix with dependencies will appear.
If a row is empty (no numbers) then that part is not used.
You can R-click and Safe delete the corresponding selection.
You can R-click > Find usages - in order to see where the specific part is used.
A brief description of this feature can be found here.
JArchitect
JArchitect offers many features and seems to be the complete solution for analyzing large projects and ensure code quality.
It can provide the Dependency Matrix similar to the one from IntelliJ Ultimate.
It provides also a language CQLinq that you can use to query your code base. For example, determining the classes which are not used. And more you can generate reports and also create thresholds and integrate in your normal continuous integration process. Therefore you can fail a build if, for example, some classes are not used.
NOTE: there are some answers like this one which discusses more tools for analyzing dependencies.