Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Obtaining derived classes

Reply
Thread Tools

Obtaining derived classes

 
 
EjP
Guest
Posts: n/a
 
      09-22-2004

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



 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      09-22-2004


"EjP" <> wrote in message
news:ciqq4n$mrn$...
>
> 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/...swers.html#q32

Good luck.

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


 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      09-22-2004
On Tue, 21 Sep 2004 22:08:43 -0500, EjP wrote:

>
> 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.


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?

--
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.

 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-22-2004
EjP wrote:
> 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.
 
Reply With Quote
 
EjP
Guest
Posts: n/a
 
      09-22-2004
Owen Jacobson wrote:

> On Tue, 21 Sep 2004 22:08:43 -0500, EjP wrote:
>
>
>>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.

>
>
> 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?
>

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
 
Reply With Quote
 
EjP
Guest
Posts: n/a
 
      09-22-2004
EjP wrote:
>
> 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
>
>
>

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

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-22-2004
On Wed, 22 Sep 2004 11:50:52 -0500, EjP wrote:

> ..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

--
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
 
Reply With Quote
 
Ann
Guest
Posts: n/a
 
      09-22-2004

"EjP" <> wrote in message
news:ciqq4n$mrn$...
>
> 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


I might be wrong, but can't you use 'grep'?


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      09-22-2004
"EjP" <> wrote in message
news:ciru6j$aae$...
> Owen Jacobson wrote:
>
> > On Tue, 21 Sep 2004 22:08:43 -0500, EjP wrote:
> >
> >
> >>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.

> >
> >
> > 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?
> >

> 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.


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.


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



 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      09-28-2004
On Thu, 23 Sep 2004 02:09:00 +0800, Ann wrote
(in article <0dj4d.85442$MQ5.59165@attbi_s52>):

>
> "EjP" <> wrote in message
> news:ciqq4n$mrn$...
>>
>> 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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
how to link classes using pointers of their derived classes ivan.leben@gmail.com C++ 6 09-07-2006 07:00 AM
Vector of abstract classes filled with derived classes ? Manuel C++ 8 01-05-2006 09:07 PM
Base Classes in .exe, derived classes in .dll Colin Goudie C++ 6 01-26-2004 03:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57