finding dead code...

S

secret

I've recently taken responsibility for a large java application and I'd love
to be able to use a tool to help me discover what classes and methods are no
longer being used so I could refactor them out.

Basically there are a small and finite number of places for users to enter
the code. What I would ideally like to have is a tool that I could enter in
those class/method entry points and the root of the project directory. The
tool would then parse my code and make a graph of the classes and methods
that could ever be reached from those starting points and would then return
to me a list of the rest of the classes/methods that are never reached.

Is there such a tool?

alan
 
V

VisionSet

secret said:
I've recently taken responsibility for a large java application and I'd love
to be able to use a tool to help me discover what classes and methods are no
longer being used so I could refactor them out.

Basically there are a small and finite number of places for users to enter
the code. What I would ideally like to have is a tool that I could enter in
those class/method entry points and the root of the project directory. The
tool would then parse my code and make a graph of the classes and methods
that could ever be reached from those starting points and would then return
to me a list of the rest of the classes/methods that are never reached.

Have a look at http://jgrasp.org & http://www.quest.com/jprobe/
Or a good ide such as eclipse can help.
 
R

Robert Olofsson

secret ([email protected]) wrote:
: tool would then parse my code and make a graph of the classes and methods
: that could ever be reached from those starting points and would then return
: to me a list of the rest of the classes/methods that are never reached.
: Is there such a tool?

That depends on what you really want. There are a few tools that can
do code coverage. Basically this means that they tell you which
classes/methods/lines are used during tests (you have automated tests
dont you?). I havent used them but I know of them:
jprobe, jcoverage...

Profilers can most probably dump a list of methods/classes that have
been used during tests.

Ok, do you have tests for 100% of your application? if so
congratulations, I dont find that too common so Ill assume that you
dont. Then the coverage lists can not be used directly to tell you
what classes are not used.

So how do you do then? grep for "new <classname>" in the code?
what about dynamic class loading? what about islands of unused code?

For work I wrote a perl program that can read our java code and our
config files and db and figure out which classes are used.
This tool is highly specialized and will not do you any good
(and since its work I cant share without asking my boss).

So I would say that no, no such tool exists for you, but some tool
that tries to tell you that actually do exist.

Someone please tell me Im wrong.

/robo
 
R

Roedy Green

Basically there are a small and finite number of places for users to enter
the code. What I would ideally like to have is a tool that I could enter in
those class/method entry points and the root of the project directory. The
tool would then parse my code and make a graph of the classes and methods
that could ever be reached from those starting points and would then return
to me a list of the rest of the classes/methods that are never reached.

Optimising compilers do this, but they don't modify the original
source. Perhaps the Jet folk would be interested in exporting that
information in a usable way. You might be able to figure out enough
as it is from the verbose Jet compile listings. See
http://mindprod.com/jet.html
 
R

Roedy Green

Profilers can most probably dump a list of methods/classes that have
been used during tests.

Of course just because it was not exercised in a given test does not
mean it is safe to remove the code. It is just indicative it MIGHT be
truly dead code.


On the other hand there can be code that in theory can be reached, but
in practice will never be.
 
A

Andrew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

VisionSet wrote:
| |
|>I've recently taken responsibility for a large java application and I'd
|
| love
|
|>to be able to use a tool to help me discover what classes and methods are
|
| no
|
|>longer being used so I could refactor them out.
|>
|>Basically there are a small and finite number of places for users to enter
|>the code. What I would ideally like to have is a tool that I could enter
|
| in
|
|>those class/method entry points and the root of the project directory.
|
| The
|
|>tool would then parse my code and make a graph of the classes and methods
|>that could ever be reached from those starting points and would then
|
| return
|
|>to me a list of the rest of the classes/methods that are never reached.
|>
|
|
| Have a look at http://jgrasp.org & http://www.quest.com/jprobe/
| Or a good ide such as eclipse can help.
|

Eclipse can easily do what you have specified. You can turn these
preferences on and off, as well as many many more that I'm sure you'd
find handy.

- --
[A n d r e w]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/fzhOx4nopx2ZsVURArS9AKDT/Kx3kW6/NPDoyl32xPaGtNepPACdHhO1
LHkTd3pSMJP/9RwEeNfCqoE=
=MxSB
-----END PGP SIGNATURE-----
 
P

Paul Tomblin

In a previous article, see mindprod.com said:
Of course just because it was not exercised in a given test does not
mean it is safe to remove the code. It is just indicative it MIGHT be
truly dead code.

Unless you're using Class and ClassLoader to load classes by name, You can
tell by code parsing if a method is never called.

In one job, we used a code obfuscator that as well as obfuscating code,
also did that analysis and removed unused methods. It made some pretty
small jar files because of that. I think there was a way to tell it to
just tell you which methods and classes were unused. I know there was a
way to tell it what methods and classes you needed to access by name so it
wouldn't obfuscate those.
 
Joined
Sep 9, 2009
Messages
1
Reaction score
0
Dead Code Detector (dcd)

Dead Code Detector (dcd.dev.java.net) is exactly such a tool to find dead code without code coverage or unit tests.

The tool does static analysis of compiled classes to search unused code, including private and package private methods and/or protected and public methods. But it does not start with entry points: so these entry points could be detected as suspects, which you can filter.

If you have any problem with DCD you can reach me with evernat at free.fr

JGrasp noted above has recently included a plugin for DCD.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top