I have a web application in which there are classes and methods that are not used anymore.
Is there a feature of eclipse that can tell me all the classes and methods that are lo longer used, in order to 'clean' my code? Thank you
Give a try to a UCDetector
UCDetector (Unnecessary Code Detector - pronounced "You See Detector") is a eclipse PlugIn tool to find unnecessary (dead) public java code. For example public classes, methods or fields which have no references. UCDetector creates markers for the following problems, which appear in the eclipse problem view:
- Unnecessary (dead) code
- Code where the visibility could be changed to protected, default or private
- Methods of fields, which can be final
Eclipse will automatically highlight code that isn't being called and imports that aren't being used with a yellow squiggly. Hit Ctrl-shift-O
to remove unused imports. You still have to go through and manually remove methods and fields you don't want.
To quick-and-dirtily check if a class or method is being used, left-click on it to select it (grey background) then right-click on it, References -> Workspace
, to see if anything in your workspace uses it. This may not work with anything that specifies Java methods or classes in configuration, e.g. Spring XML configuration, so read the warning below :)
If you're feeling brave, you can get Eclipse to analyse a class and auto-remove private stuff it deems unnecessary (see warning below). Here's how:
Right-click inside your code, go to Source -> Clean Up...
. Go for a Custom
profile. Hit Configure
. In here, go to the Unnecessary Code
tab, and you can check Remove unused private members
, and which things you want to remove (e.g. methods will remove all private methods that are not being called). Don't forget to back up your code first in a nice repository :) Hit Next
and you'll see the results of the change. Hit Finish if you're happy!
To create a permanent cleanup profile, so you don't have to specify Custom each time, go to Window -> Preferences -> Java -> Code Style -> Clean Up -> Edit
. This will edit the existing, default Eclipse cleanup profile; you can also create a separate one here.
Finally, there are tools to do this. Googling got me to the UCDetector open source Eclipse plugin, which seems to be good, and reading about it on Stack Overflow again makes it seem to be a very useful tool, but one that comes with the following warning! :)
WARNING: be careful! Make sure that the code you remove is not being accessed (or preferably, CANNOT be accessed) from anywhere else. For example, a good way to do this is to use a version control system that keeps track of all your code (so you can roll back if necessary), then deploy a "cleaned" version in a test system to make sure it's all okay, before moving on to production. You don't want stuff breaking at 3am because of your cleaning :)
If your project is not too complex you can right click on the classes choose the references->project
menu option
We use Sonar code analysis to identify issues regarding unused classes or the usage of for example depreciated ones. Other Answers on Stackoverflow suggest a braod spectrum of tools available for exlipse:
How to find unused Java classes and JSP files from a project