Reflection Question

Lets say we Class X

Now I need to find all the classes in the CLASSPATH which extend X.
I figure you have to use Reflection to achieve this but I'm stuck on where to go with the CLASSPATH problem. Help...

Thanks a million
maxarulAsked:
Who is Participating?
 
imladrisConnect With a Mentor Commented:
No reflection won't help here, as you have no class to "reflect" on yet. Also, since you are effectively searching for a bunch of files, a regular applet will not be able to do this.

First you should get the class path. For an application this is available from the system property: java.class.path.

Then you would have to load every class you find in each of the class path directories and any subdirectories the program uses (I hope you have enough memory (or few enough classes) to do this).

Then for each class you get a Class object using the getClass method of the Object class.
With a Class object you can get the name of the classes superclass by calling the getSuperclass method of the Class object. This returns another Class object. You can then inspect its name by calling its getName method. Now check whether it equals X. If it does you have a class that extends X, if not check if it equals Object. If it does this class does not extend X. If it is neither you should call getSuperclass again etc. etc.

I'm not sure how practical this is.
0
 
imladrisCommented:
If the solution I described is not workable for you, it might help if you described the problem you were trying to solve. Alternative solutions may be suggested that would would be more tractable.
0
All Courses

From novice to tech pro — start learning today.