Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   finding dead code... (http://www.velocityreviews.com/forums/t127292-finding-dead-code.html)

secret 10-04-2003 06:45 PM

finding dead code...
 
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



VisionSet 10-04-2003 07:06 PM

Re: finding dead code...
 

"secret" <secret@secret.com> wrote in message
news:3zEfb.7345$o_.762@newssvr29.news.prodigy.com. ..
> 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.

--
Mike W



Robert Olofsson 10-04-2003 07:57 PM

Re: finding dead code...
 
secret (secret@secret.com) 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

Roedy Green 10-04-2003 08:20 PM

Re: finding dead code...
 
On Sat, 04 Oct 2003 18:45:19 GMT, "secret" <secret@secret.com> wrote
or quoted :

>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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green 10-04-2003 08:21 PM

Re: finding dead code...
 
On 4 Oct 2003 19:57:02 GMT, d94-rol@tjatte.nada.kth.se (Robert
Olofsson) wrote or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Andrew 10-04-2003 09:14 PM

Re: finding dead code...
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

VisionSet wrote:
| "secret" <secret@secret.com> wrote in message
| news:3zEfb.7345$o_.762@newssvr29.news.prodigy.com. ..
|
|>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-----


Paul Tomblin 10-04-2003 09:18 PM

Re: finding dead code...
 
In a previous article, see mindprod.com said:
>On 4 Oct 2003 19:57:02 GMT, d94-rol@tjatte.nada.kth.se (Robert
>Olofsson) wrote or quoted :
>>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.


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.


--
Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
"Towards thee I roll, thou all-destroying but unconquering wanker; to the
last I grapple with thee; from hell's heart I killfile thee; for hate's
sake I spit my last post at thee! All your base are belong to us!"

evernat 09-09-2009 08:02 AM

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.


All times are GMT. The time now is 03:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.