We have a project with a very large number of methods and classes. Is
there any way to find all public methods and classes that are not
referenced anywhere else in the project? I'd like to clear out all the
cruft.
We're using Eclipse.
Goofball - 29 Oct 2006 21:27 GMT
As far as I think, one of the easiest and the best way to do that is to
use aspects. The aspect-oriented programming. Use AspectJ plugin for
Eclipse. Just download and install it. Open help and read the chapter
4. Idioms of the AspectJ Language Guide. There is a pretty nice aspect
on how to declare error and prohibit compiling if anywhere in the code
of the project there is a call to the specified functions. You can
declare warning or error if a call to a function(s) exists or even if
just those functions are defined. Also, errors and warning can be
declared on functions that are not called anywhere in the project. That
is very nice feature of AspectJ. And overall, AOP is a very nice
technology. Use it, if you can!
> We have a project with a very large number of methods and classes. Is
> there any way to find all public methods and classes that are not
> referenced anywhere else in the project? I'd like to clear out all the
> cruft.
>
> We're using Eclipse.
Red Orchid - 29 Oct 2006 23:48 GMT
Chris <spam_me_not@goaway.com> wrote or quoted in
Message-ID: <454507a8$0$14802$9a6e19ea@news.newshosting.com>:
> We have a project with a very large number of methods and classes. Is
> there any way to find all public methods and classes that are not
> referenced anywhere else in the project? I'd like to clear out all the
> cruft.
I think that writing a class to do that is one of simple ways.
For example,
<code>
//
// Untested ...
//
public class XXXXX {
List<String> sources;
void process() {
loadAllSourceFiles();
processMethods();
}
void loadAllSourceFiles() {
sources = new ArrayList<String>();
// recursive method will be required.
// read each source file and add her to sources;
}
class Method {
String signr; // ex: void xxxx(int x, int y)
Pattern pat;
int refCount;
public Method(String name, String signr) {
this.signr = signr;
//
// Maybe,
// "\\b" + name + "//s*\\(.*\\);"
//
pat = Pattern.compile(" ... ");
}
}
void processMethods() {
List<Method> ls = getAllMethods();
List<Method> rs = removeRefMethods(ls);
print(rs);
}
List<Method> getAllMethods() {
// LinkedList
List<Method> ls = new ArrayList<Method>();
//
// Maybe ..
//
// "\\s+(public\\s+\\w+\\s+(\\w+)\\(.*\\))"
//
// group 1 is signature.
// group 2 is method name.
//
Pattern pat = Pattern.compile(" ... ");
for (String src : sources) {
Matcher m = pat.matcher(src);
if (m.find()) {
//
// add to ls
//
}
}
return ls;
}
List<Method> removeRefMethods(List<Method> ms) {
List<Method> ls = new ArrayList<Method>();
for (String src : sources) {
for (Method m : ms) {
if (!m.isRef && m.pat.matcher(src).find()) {
m.isRef = true;
ls.add(m);
}
}
}
return ls;
}
void print(List<Method> ls) {
// to console or a file
}
}
</code>
Daniel Dyer - 30 Oct 2006 10:31 GMT
> We have a project with a very large number of methods and classes. Is
> there any way to find all public methods and classes that are not
> referenced anywhere else in the project? I'd like to clear out all the
> cruft.
>
> We're using Eclipse.
IntelliJ IDEA has an inspection (called "Unused Declaration") for this:
"This inspection reports classes, methods or fields in the specified
inspection scope that are not used or not reachable from entry points."
I don't know if Eclipse has an equivalent feature but if it doesn't you
could try an evaluation copy of IDEA.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Thomas Fritsch - 30 Oct 2006 15:24 GMT
> We have a project with a very large number of methods and classes. Is
> there any way to find all public methods and classes that are not
> referenced anywhere else in the project? I'd like to clear out all the
> cruft.
>
> We're using Eclipse.
Open the project in Eclipse.
Right-click on a class or method,
in the popup-menu select "References - Project".
You'll get a list of all references to that class or method.
Do that for all classes/methods in question.

Signature
Thomas
Ed - 30 Oct 2006 16:21 GMT
Chris skrev:
> We have a project with a very large number of methods and classes. Is
> there any way to find all public methods and classes that are not
> referenced anywhere else in the project? I'd like to clear out all the
> cruft.
>
> We're using Eclipse.
If memory serves, both findbugs and pmd offer this functionality.
.ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html