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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.