Do you want to find out if you have dead code in your project? Than this tool will help you.
It will tell you which classes and methods were never called. But it will only tell you the method name. So it’s not possible to differ between overloaded methods. For me it’s not an issue and this way the code is very simple. I hope that it isn’t an issue for you, too.
Note: if some method were never called within the monitoring phase doesn’t mean that this methods aren’t needed. Only while the monitoring phase nobody needed that methods. To decide how long the monitoring phase should be you can look into the output file - the date time is logged. Use all the information wisely.
To track the method calls the lcmip-javaagent is used. A Java Agent can instrument the code. This means that the Java Agent will modify the original code to instrument it. In this case a static method call is added at the beginning of each method. The at.rseiler.lcmip.javaagent.MethodLogger#log() will be called with the full method name (classname#methodname). The MethodLogger stores all method names in an concurrent hash set and writes all new method names to the specified output file. Now you know the used methods.
But how to know the unused methods? You can find that out by using the other two lcmip tools.
The output of the summary report looks like this:
unused classes: 1/4 unused methods: 5/12 unused classes at.rseiler.lcmip.demo.UnusedClass unused methods at.rseiler.lcmip.demo.Main#<init> at.rseiler.lcmip.demo.Main$MainInnerClass#unusedMethod at.rseiler.lcmip.demo.UnusedClass#<init> at.rseiler.lcmip.demo.UnusedClass#unused at.rseiler.lcmip.demo.UsedClass#unused
How to find all unused classes and unused methods of the demo module:
> mvn clean package lcmip-demo\target> java -javaagent:..\..\lcmip-javaagent\target\lcmip-javaagent-1.0.jar=method-used.txt#at.rseiler.lcmip.demo -jar lcmip-demo-1.0.jar method-used.txt#at.rseiler.lcmip.demo lcmip-demo\target> java -jar ..\..\lcmip-jar-analyser\target\lcmip-jar-analyser-1.0-jar-with-dependencies.jar lcmip-demo-1.0.jar > method-all.txt lcmip-demo\target> java -jar ..\..\lcmip-summary\target\lcmip-summary-1.0.jar method-all.txt method-used.txt
For a typical Java application the overhead should not be noticeable. Nevertheless test it before you run it in production.
3.9 Specially Named Initialization Methods
At the level of the Java virtual machine, every constructor (§2.12) appears as an instance initialization method that has the special name <init>. This name is supplied by a compiler. Because the name <init> is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Instance initialization methods may be invoked only within the Java virtual machine by the invokespecial instruction, and they may be invoked only on uninitialized class instances. An instance initialization method takes on the access permissions (§2.7.4) of the constructor from which it was derived.
A class or interface has at most one class or interface initialization method and is initialized (§2.17.4) by invoking that method. The initialization method of a class or interface is static and takes no arguments. It has the special name <clinit>. This name is supplied by a compiler. Because the name <clinit> is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Class and interface initialization methods are invoked implicitly by the Java virtual machine; they are never invoked directly from any Java virtual machine instruction, but are invoked only indirectly as part of the class initialization process.
See Downloads