dynamically finding other classes implementing the same interface

H

Henry Townsend

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
 
H

Henry Townsend

Henry said:
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
 
F

Furious George

Henry said:
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
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.

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

Furious George

Henry said:
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();

(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 {
}
}
 
O

Oliver Wong

Furious George said:
This is theretically possible, but in practice it is not possible.

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.

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
 
H

Henry Townsend

Oliver said:
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
 
O

Oliver Wong

Henry Townsend said:
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).

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...e8e8edf3690/fa8c5581a8050395#fa8c5581a8050395
for why.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top