How can I detect dead code in an enterprise Java project (Java + JSP + Javascript)?
Asked Answered
E

2

12

Does anyone know of a tool for detecting dead code in a Java EE project?

I've looked into lots of tools that do this well for pure Java projects, but nothing seems to really handle projects which include JSPs and Javascript files as well. For example, detecting that a Java method is in use because it's being called from a JSP, or detecting that some JSP file that previously was used as an AJAX request is no longer called from any Javascript.

Thanks.

Epigeous answered 10/8, 2011 at 19:58 Comment(2)
This is largely impossible since all possible Ajax requests that a piece of JavaScript will/might generate can't be easily determined.Covenantor
You could also have scenarios where a form in the HTML is submitting to one of you JSPs so you would have to inspect the HTML too... but +1 as this is a good question.Hekate
L
5

What about real-life sampling? Plug-in JaCoCo agent to your application, run it and use it for a while. If you have thorough functional tests, run them. If you have a team of testers, let them click on your application. JaCoCo will record all executed lines (it is used for code coverage) in the meantime.

If some lines were not executed during extensive period of time (remember you would have to touch every possible screen, call, path and branch in your application) you should examine whether they aren't actually dead (or maybe you simply forgot to run your application in this particular configuration).

Understandably this is very fragile, but I don't think you will find anything automatic.

Lope answered 10/8, 2011 at 20:16 Comment(1)
I don't know JaCoCo, but using a coverage tool semms the only logical solution to me. It will not give you any data for javascript files however.Schuh
H
0

I propose to use JVM flag -verbose:class (http://java.dzone.com/articles/how-use-verbose-options-java). It will enable logging information about classes loaded by JVM, eg.

[Loaded java.lang.Object from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
[Loaded java.io.Serializable from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]

If some class is not loaded during long period of time then with highly possibility it is unused.

Of course this solution will give you only information about unused classes, but you don't have to use any external library.

Hawks answered 15/10, 2014 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.