Say I have a class Foo implementing interface Bar. I want Foo to be able
to learn, at runtime, the names of all other classes implementing Bar.
Is this doable? I've looked through the Class API but don't see anything
appropriate
Background: I'm trying to set up my app with a "plugin" style
architecture, thus accepting user-added classes. I'd like each subclass
to expose a "helpMessage()" method, such that a user could request help
from all subclasses (those shipped with it plus aftermarket plugins)
using code which would discover them dynamically and invoke
subclass.helpMessage() on each. It feels like this should be possible
but I haven't found the magic button yet.
Thanks,
HT
> Say I have a class Foo implementing interface Bar. I want Foo to be able
> to learn, at runtime, the names of all other classes implementing Bar.
> Is this doable? I've looked through the Class API but don't see anything
> appropriate
My best try is below. Not sure why it didn't work; I always get a
zero-length list:
Class cls = this.getClass();
// this does produce the desired interface
Class sup = cls.getSuperclass();
Class[] list = sup.getClasses();
HT
Furious George - 27 May 2006 06:30 GMT
> > Say I have a class Foo implementing interface Bar. I want Foo to be able
> > to learn, at runtime, the names of all other classes implementing Bar.
[quoted text clipped - 7 lines]
> // this does produce the desired interface
> Class sup = cls.getSuperclass();
(1) You would probably want to use cls.getInterfaces rather than
cls.getSuperclass.
> Class[] list = sup.getClasses();
sup.getClasses returns a list of classes that are members of the sup
class. You want a list of classes that implement sup. For a non
zero-length list try
public class NonZeroLengthList {
public void tryIt ( ) {
Class cls = this.getClass();
Class[] list = cls.getClasses();
System.out.println("The length of the list is " + list.length + ". It
should be 1 (Class1).");
}
private class Class1 {
}
}
> HT
> Say I have a class Foo implementing interface Bar. I want Foo to be able
> to learn, at runtime, the names of all other classes implementing Bar.
> Is this doable? I've looked through the Class API but don't see anything
> appropriate
This is theretically possible, but in practice it is not possible.
> Background: I'm trying to set up my app with a "plugin" style
> architecture, thus accepting user-added classes. I'd like each subclass
[quoted text clipped - 3 lines]
> subclass.helpMessage() on each. It feels like this should be possible
> but I haven't found the magic button yet.
I would create a registry. When the user wants to "plugin" a new
class, they would use the registry. The registry would keep track of
class relationships.
> Thanks,
> HT
Oliver Wong - 29 May 2006 19:35 GMT
>> Say I have a class Foo implementing interface Bar. I want Foo to be able
>> to learn, at runtime, the names of all other classes implementing Bar.
[quoted text clipped - 14 lines]
> class, they would use the registry. The registry would keep track of
> class relationships.
You could also mandate that all plugins must reside in some specific
directory (this is what Eclipse does). Your program would then only need to
scan this directory, load the classes, and check if they implement the
desired interface.
- Oliver
Henry Townsend - 29 May 2006 19:56 GMT
> You could also mandate that all plugins must reside in some specific
> directory (this is what Eclipse does). Your program would then only need
> to scan this directory, load the classes, and check if they implement
> the desired interface.
I was starting to think along those lines too. Just to be clear, you're
speaking of the filesystem level, right? Just do a glob on *.class in
the appropriate dir and then strip out anything which doesn't implement
the interface (though normally everything in there would).
My first idea was to go through the Class API. Then I was looking for a
way of listing all classes in a given package (since that one's easy to
mandate). As a fallback, I figure the filesystem level should be doable
and it sounds like you agree.
Thanks,
HT
Oliver Wong - 29 May 2006 20:18 GMT
>> You could also mandate that all plugins must reside in some specific
>> directory (this is what Eclipse does). Your program would then only need
[quoted text clipped - 5 lines]
> appropriate dir and then strip out anything which doesn't implement the
> interface (though normally everything in there would).
Yes, that's exactly it.
> My first idea was to go through the Class API. Then I was looking for a
> way of listing all classes in a given package (since that one's easy to
> mandate). As a fallback, I figure the filesystem level should be doable
> and it sounds like you agree.
Getting all classes in a package won't work. See
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/4017e
e8e8edf3690/fa8c5581a8050395#fa8c5581a8050395
for why.
- Oliver