Automatically locate all classes; filter by regexp

This was originally written by duncanidaho, and then I started using it almost a year ago, and added various bugfixes and improvements along the way.

http://javagamesfactory.org/views/view-sourcesnippet?id=1

This is an “Automatic class locater and loader”:

[] Supply a regexp filter defining a classname (e.g. "com.sun.") and ask it to find all classes anywhere on your classpath and in ANY jars that match that search
[] Provide a list of regexps that you want it to completely ignore (for increased performance, for instance), e.g. "org.apache." if you’re using some apache packages, and only want to find things in your packages
[] Find all classes that implement a given interface (Yes! Really!)
[
] …or do it using a regexp to filter out unwanted subclass names / improve search performance
[] Produces a list of classes that were REQUIRED by some of the classes it found; tells you which classes required them (so you can decide if you care or not)
[
] Extensive error handling so that it recovers from practially every possible failure (um; only by trial-and-error - I may have missed some - let me know if you hit any new ones, or spot something that should be handled and currently isn’t)
[*] …probably does some other stuff too that I’ve forgotten about :wink:

FYI it’s been running on every restart on the JGF server (auto-locates classes to be loaded to run parts of the system) for the last 6 months or so, and I’m pretty confident it’s robust now (although I found many fiddly little bugs and problems along the way).

ClassPath.java was viewable, but I think I crashed your server while clicking the ClassLocator.java link.

The server-response was an image :o (MagicWoods thumbnail) instead of the java-code… and after that the server stopped responding.

I’ve had wrong images before: game-thumbnails in the LWJGL/JOGL comparision where you would expect the green/yellow round buttons.

Hope this helps…! :slight_smile:

Incidently, I found attempting to log in to JGF crashed with an error report, which didn’t tell me where to mail it.

When I got back to the site I still could find an email address to mail the stack trace to?

Kev

Something in the last patches on Sunday is deeply wrong - generated > 15gb of logfiles in 2 days, ran out of disk space :(. Trying to find out what’s in there now, but 15Gb is not easy to examine :).

Thanks, I’ll add that to the todo list. The stack-trace-display is hard-coded (for obvious reasons) so I need to actually dig into code, but it’ll be no problem.

This sounds like exactly what I have been looking for. Any chance of posting the code here or sending me a copy?

Luke

Main site link works fine now AFAICS.

Oh wow. thats a great utility

That’s exactly what I need, but the link to the code doesn’t work. Can it be repaired?

Keith

Seems to be working fine at the moment.

NB: if it ever stops working, try again after 6am GMT (server is resetting itself every morning at 6am to combat a memory leak that periodically locks it up :frowning: )

Thanks, got it going. Very useful,

Cheers,

Maybe I’m doing s.th. wron, but it doesn’t work for me. I tried the following:


Class[] list = new ClassLocater().getSubclassesOf(java.util.EventListener.class);
for(Class c : list)
  System.out.println(c);

butg there are no classes and interfaces listed from the java.awt.event package. :frowning:

Most people want to skip the thousands of java and sun classes, so the default constructor filters them out:

public ClassLocater()
{
addSkipPrefix( “org.apache.log4j.” );
addSkipPrefix( “com.sun.” );
addSkipPrefix( “java” );
}

This is documented - if you look at the class constructor, it says:

/**

  • Automatically adds sun’s classes, the java library classes, and the Apache
  • log4j classes (a lib used by ClassLocater!) to the skip list; it’s very unlikely
  • that you’re trying to locate any of these!
    */

So, if you just comment out the line that skips everything starting with “java” you should be fine.

thanks for the answer bbb.
of course I read the doc above the construrctor and commented the lines out, but forgot to mention this ::slight_smile:


    public ClassLocater() {
       // addSkipPrefix( "org.apache.log4j." );
      //  addSkipPrefix( "com.sun." );
      //  addSkipPrefix( "java" );
    }

any ideas?

Output? Have you turned on the debug mode for the loggers? You could also try adding extra logging,especially in ClassPath (I never needed any for it, so I never added any detailed loggers, but that would be a useful thing).