This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: How do I find deprecated code that is still being called in Java?

What tools do you use to find unused/deprecated code on large Java projects? Our product has been in development for several years and manual testing has become very difficult. However, we remove as much unused code as possible.

Suggestions for general strategies/techniques (except for specific tools) are also appreciated.

Edit: Please note that we have used code coverage tools (Clover, IntelliJ), but they are not helpful. Deprecated code still has unit tests and shows up as covered. I think an ideal tool would identify clusters of code that have little dependence on its other code, allowing the document to be checked manually.

Answer a

I typically instruct a running system to keep track of code usage, and then start examining code that hasn’t been used in months or years.

For example, if you are interested in unused classes, you can check all classes to record when creating an instance. A small script can then compare these logs to the full list of classes to find unused classes.

Of course, at the method level, you should consider performance. For example, only the first invocation of these methods is recorded. I don’t know how to do it best in Java. We’ve already done this in Smalltalk, which is a dynamic language and therefore allows code to be modified at run time. We use a log to detect all methods and unload the logging code after the method is first logged, so there is no further performance penalty after a while. Perhaps something similar could be marked with static booleans in Java.

Answer two

There’s a handy plugin called Unused Code Detector

It covers the entire project or specific files and shows various unused/deprecated methods and suggests visibility changes (that is, public methods that can be gaicprotected or private).

The article translated from Stack Overflow:stackoverflow.com/questions/1…