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 / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Obtaining derived classes

Thread view: 
EjP - 22 Sep 2004 04:08 GMT
Is there a way to get a list of classes derived from a particular base
class?

Something like

   Class c = Class.forName("BaseClass");
   Class d[] = c.getDerivedClasses();  //I know this doesn't work

Any advice would be appreciated.

                Thanks,
                E
Tony Morris - 22 Sep 2004 07:35 GMT
> Is there a way to get a list of classes derived from a particular base
> class?

No, since the concept doesn't exist - a superclass cannot know about it's
subclasses.
You might load a package (java.lang.reflect.Package) and query its classes
(only those that have been loaded) to determine if they are a subclass, but
I suspect that this is in vain in achieving what is more likely than not, a
workaround to a design issue that you have.

http://www.xdweb.net/~dibblego/java/faq/answers.html#q32

Good luck.

Signature

Tony Morris
http://xdweb.net/~dibblego/

Owen Jacobson - 22 Sep 2004 08:30 GMT
> Is there a way to get a list of classes derived from a particular base
> class?
[quoted text clipped - 5 lines]
>
> Any advice would be appreciated.

The concept of "list of derived classes" is very vaguely-defined, and can
change over the course of execution as classes are loaded.  What is the
real problem you are trying to solve?

Signature

Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

EjP - 22 Sep 2004 14:24 GMT
>>Is there a way to get a list of classes derived from a particular base
>>class?
[quoted text clipped - 9 lines]
> change over the course of execution as classes are loaded.  What is the
> real problem you are trying to solve?

Basically, I'm writing a hardware interface that will perform
user-defined operations in response to a set of trigger
inputs (it's for a haunted hause I'm putting together in
my garage).  The actions are defined by writing derived
classes based on a particular base class.  There's a
steering GUI that allows the user to select which action
to perform for each of the triggers from a pulldown list,
and I want that list to automatically load.

The straightforward way is to put all the action classes
in a directory and just look there when the program launches,
but I was hoping there might be a "cuter" way.  I guess not.

And yes, this is all overkill for what I'm trying to do,
but I was just having some fun.

Thanks for the response.

                -E
Tony Morris - 23 Sep 2004 00:19 GMT
> >>Is there a way to get a list of classes derived from a particular base
> >>class?
[quoted text clipped - 22 lines]
> in a directory and just look there when the program launches,
> but I was hoping there might be a "cuter" way.  I guess not.

Write your own class loader that finds all these classes.
Though, I much prefer the (perhaps less 'cuter' from your perspective) more
efficient and user-friendly approach of requiring the user to explicitly
specify the implementation class (I'm assuming the class must implement some
interface (your supertype)) and use the system class loader to load it. This
is a typical approach.

Signature

Tony Morris
http://xdweb.net/~dibblego/

Michael Borgwardt - 22 Sep 2004 09:08 GMT
> Is there a way to get a list of classes derived from a particular base
> class?

No, since there's not even a way to get a list of *all* classes.
EjP - 22 Sep 2004 17:50 GMT
> Is there a way to get a list of classes derived from a particular base
> class?
[quoted text clipped - 8 lines]
>                 Thanks,
>                 E

OK, I think I see the error of my ways.  How about advice on
the quickest way to load all the classes in a particular
directory?

Thanks,
E
Andrew Thompson - 22 Sep 2004 18:32 GMT
> ..How about advice on
> the quickest way to load all the classes in a particular
> directory?

URL theDir = this.getClass().getResource("/");
File f = new File( url.getFile() );
f.listFiles( new ClassFilter() );
...

class ClassFilter extends FileFilter {

 boolean accept(File f) {
   return f.getName().toLowerCase().endsWith(".class");
 }
}

( quickly, of course.. ;-)

BTW, please keep your posts to c.l.j.help for the moment
EjP, it is not generally a good idea to x-post between
c.l.j.help and c.l.j.programmer.

** F'Ups set to c.l.j.help **

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

EjP - 22 Sep 2004 19:23 GMT
>>..How about advice on
>>the quickest way to load all the classes in a particular
[quoted text clipped - 21 lines]
>
> HTH

Thanks.  Perfect.

-E
Ann - 22 Sep 2004 19:09 GMT
> Is there a way to get a list of classes derived from a particular base
> class?
[quoted text clipped - 8 lines]
> Thanks,
> E

I might be wrong, but can't you use 'grep'?
steve - 28 Sep 2004 22:58 GMT
>> Is there a way to get a list of classes derived from a particular base
>> class?
[quoted text clipped - 10 lines]
>
> I might be wrong, but can't you use 'grep'?

you don't want to load all the classes at startup.
you just need a class loader that "points" to the directory (classpath) that
hte files are in.
As the java app needs the classes , it will load them on the fly.

you need a small loader program, that does not directly reference the other
classes.
instigate a URL: type loader, as this allows the class files to be pulled
from any place, disk, internet, network, etc.

then launch an instance of your root application from this loader.
java will go thru and find all the needed classes.

steve


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.