I need to cleanup a some legacy code. Removing unused code is an important step.
Is there a tool that finds all deprecated code, removes all items which are still used somewhere and gives me a list of unused deprecated code?
For bonus points: Is there a tool which can find unused code non-deprecated code?
I’m aware that this is never perfect but I know for which cases I need special handling (as in DB drivers or classes that are referenced via DI).
I’m not completely certain that I understand your question. Do you want a tool that un-deprecates code that is still referenced? Any IDE will help you with that. Not automatically but removing an @Deprecated annotation is easily done with a global query-and-replace. After you have removed unused code, of course:
If all you want is to remove unused code, I have used the eclipse plugin ucdetector for this purpose in a previous project. While it does not actually remove the unused code it does give you a list of the methods, classes and constants that have no references so you can remove them yourself. This is a good thing.
As you point out yourself, there are some classes/methods that may seem to be unused using static analysis. In my opinion this makes it impossible to automate this task. You the coder will have to analyze every block of code that is reported to be unused.
If you are lucky enough to have excellent test coverage another option is to use a code coverage analysis tool, like cobertura, clover or emma.