Finding unused public methods and classes

C

Chris

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

Goofball

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!
 
R

Red Orchid

Message-ID: said:
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>
 
D

Daniel Dyer

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

Thomas Fritsch

Chris said:
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.
 
E

Ed

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top