The Source for Java Technology Collaboration

Home » java.net Forums » Java Tools » Java Development Tools

Thread: Unused classes/packages tool

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 7 - Last Post: Sep 14, 2008 3:25 PM by: spj Threads: [ Previous | Next ]
echofloripa

Posts: 3
Unused classes/packages tool
Posted: Sep 14, 2005 7:01 AM
  Click to reply to this thread Reply

I'm working in a company which code evoluted too fast and among a lot of different developers. There are hundreds of packages and thousands of classes. We are migranting to subversion (from source safe, urghh :) ), and as a part of the migration plan, we want to get rid of any unused class or package. I'm looking for kind of tool that, giving a start point (a class or a method of a class), this tool could list the classes and/or packages in the included source path(s) that are not reached by the initial class(es), directly or indirectly.

Thanks a lot for the help
Emerson Cargnin
UK

gatto

Posts: 10
Re: Unused classes/packages tool
Posted: Sep 14, 2005 10:48 AM   in response to: echofloripa
  Click to reply to this thread Reply

Hi Emerson,

I think any modern IDE can do the trick for you. In eclipse, which I'm more familiar with, you just have to right click in a java file and choose (references -> workspace). Not automatic at all, but it works.

Eclipse can also be configured to generate warnings for unused variables, fields and methods. Perhaps tools like PMD or Checkstyle can help you to find unused classes, but I'm not sure.

There's also JDepend, which analyses class files (already compiled) and generates coupling reports, but I think it only works at the package level, not sure if it could be used to list class by class dependencies.

Anyway, I foresee a lot of coffee in your future, and I'm not talking about Java :-) Good luck!

[]'s
Gatto

iphillips

Posts: 3
Re: Unused classes/packages tool
Posted: Sep 15, 2005 4:16 AM   in response to: echofloripa
  Click to reply to this thread Reply

You can use ProGuard (proguard.sourceforge.net) to do this, it's really an obfuscator but the -printusage option can list all of the unused files in an application. You can even give it multiple entry points to scan from which may help if you code base is particularly gnarly.

echofloripa

Posts: 3
Re: Unused classes/packages tool
Posted: Sep 16, 2005 2:36 AM   in response to: iphillips
  Click to reply to this thread Reply

In my search for such tool I've seem proguard, just didn't tried when i saw that it was a code obfuscator, but I'll definitely try that out.

I also know that in eclipse I can find all the callers for a given method or the references for a given class, the problem is that I would have to do this by hand, and with more than 1300 classes, just in one project would take some years :)

thanks a lot for the help
Emerson

echofloripa

Posts: 3
Re: Unused classes/packages tool
Posted: Sep 16, 2005 2:41 AM   in response to: iphillips
  Click to reply to this thread Reply

Hi gatto

Thanks for the help, I'm alsoa eclipse user, although it really can find references for a given class, it would take ages to scan all the 1300 classes by hand. Jdepend and checkstyle do not verify unused classes or packages.

thanks for the help anyway.
PS: como vai o brasil, tudo certinho por ai? :P

Emerson
Reading/UK

coxcu

Posts: 184
Re: Unused classes/packages tool
Posted: Sep 15, 2005 8:03 AM   in response to: echofloripa
  Click to reply to this thread Reply

I am aware of two basic approaches: static analysis/entry point and test/coverage driven. Below are some tools that I have used for this sort of thing. There is also also a tool that will examine a codebase and list classes/methods that are more visible than they need to be. Reducing the visibility can help tools (like Eclipse) identify dead code and is also good for encapsulation. Unfortunately, I can't find a link for it.

ProGuard
http://proguard.sourceforge.net/

NoUnit
http://nounit.sourceforge.net/sample.html

Emma
http://emma.sourceforge.net/

java -verbose:class
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

skamar

Posts: 5
Re: Unused classes/packages tool
Posted: Sep 16, 2005 2:39 AM   in response to: echofloripa
  Click to reply to this thread Reply

Proposal:

1. Check out everything from SS.
2. Use Ant or Javac to compile your starting point classes. You might want to do a free text search for Class.forName( , remember to also include those classes.
Remember to set up the source paths.
3. Write a script that runs through the source folders.
Any .java file with no corresponding .class file can be deleted.
4. Build the system and run it (with the pruned source tree) to verify that you did not tear out too much.
5. Better not delete the original SS, just in case you make a mistake.


Martin Skarsaune

spj

Posts: 1
Re: Unused classes/packages tool
Posted: Sep 14, 2008 3:25 PM   in response to: echofloripa
  Click to reply to this thread Reply

Try UCDetector. It finds unused classes, methods, fields:

UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) java code. It also tries to make code final, protected or private.
UCDetector also finds cyclic dependencies between classes.




 XML java.net RSS