Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Finding unused public methods and classes

Thread view: 
Chris - 29 Oct 2006 20:59 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.
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.