Skip to main content

Unused classes/packages tool

8 replies [Last post]
echofloripa
Offline
Joined: 2003-07-23

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
extremejava
Offline
Joined: 2011-12-08

Do try ucdetector once. I am confident that you will like it. For a beginner's how-to guide, pls see ucdetector plugin faq

echofloripa
Offline
Joined: 2003-07-23

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

spj
Offline
Joined: 2008-05-25

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.

skamar
Offline
Joined: 2005-07-06

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

coxcu
Offline
Joined: 2003-06-11

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

iphillips
Offline
Joined: 2003-06-16

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
Offline
Joined: 2003-07-23

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

gatto
Offline
Joined: 2004-07-10

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