Locating all classes implementing a given interface?

B

Bas

Hi,

my app allows third party code as plugins for certain behaviour. The
actual implementation to use can be specified using a string that
contains the classname. This works great.

However, I'd like the user to be able to specify the class name to use
using a popup menu.

The plugin is contained in a jar that is visible in the classpath.

Is it possible for my code to find all classes implementing my plugin
interface that are available at runtime? Iterating over a directory,
locate the jars and check those is not really what I want. Is this
sort of information dynamically available from the jre?

Thanks,

bas.
 
S

Stefan Ram

Bas said:
Is it possible for my code to find all classes implementing my plugin
interface that are available at runtime? Iterating over a directory,
locate the jars and check those is not really what I want. Is this
sort of information dynamically available from the jre?

I have written a library routine for this, based on code by
Ralf Ullrich.

For example, to find all classes implementing »java.util.Map«,
one sets a filter accepting only classes which
»java.util.Map« is assignable from:

public boolean accepts( final java.lang.Class class_ )
{ return java.util.Map.class.isAssignableFrom( class_ ); }

One also needs to provide a starting class to find the jar, which
is done as follows.

public java.lang.String entryPath(){ return "java.lang.Object"; }

The jar containing this type will be chosen for exploration.

In the following example, the client does not need to specify
this »entryPath«, as "java.lang.Object" is the default
obtained by extending
»de.dclj.ram.java.lang.reflect.Finder.DefaultSpecification«.

The example client is:

public class Main
{ /* based on an idea and on code by Ralf Ullrich from 2006 */

public static void main( final java.lang.String[] args )
{ new de.dclj.ram.java.lang.reflect.Finder
( new de.dclj.ram.java.lang.reflect.Finder.DefaultSpecification()
{
public boolean isClassFinder(){ return true; }

public boolean accepts( final java.lang.Class class_ )
{ return java.util.Map.class.isAssignableFrom( class_ ); }

}).inspectJar(); }}

class java.lang.ProcessEnvironment
class java.rmi.server.RemoteObjectInvocationHandler$MethodToHash_Maps$1
class java.security.AuthProvider
(...)
class java.util.Properties
class java.util.Hashtable
interface java.util.Map

The library »ram.jar« is an early publication in alpha state,
it is experimental, changing, and mostly undocumented. See:

http://www.purl.org/stefan_ram/pub/ram-jar

Known issues:

- I have ideas to improve the interface, which are not yet
implemented.

- To compile the library from the sources, one might need
to take care of some minor bugs: If I remember
correctly, some »private«s need to be replaced by
»public«.
 
M

Mark Thornton

Bas said:
Hi,

my app allows third party code as plugins for certain behaviour. The
actual implementation to use can be specified using a string that
contains the classname. This works great.

However, I'd like the user to be able to specify the class name to use
using a popup menu.

The plugin is contained in a jar that is visible in the classpath.

Is it possible for my code to find all classes implementing my plugin
interface that are available at runtime? Iterating over a directory,
locate the jars and check those is not really what I want. Is this
sort of information dynamically available from the jre?

Thanks,

bas.

The correct way to implement this type of feature is using
java.util.ServiceLoader (since Java 6). This has been the recommended
approach since at least Java 1.4, but ServiceLoader was added to make it
easier.

Mark Thornton
 
K

keke

Hi,

my app allows third party code as plugins for certain behaviour. The
actual implementation to use can be specified using a string that
contains the classname. This works great.
I think there are two approaches:

1/ manually scan all classes available in your jar file
2/ specify your implementation somewhere, for example in your
manifest.mf file.

Cheers
K
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top