How to determine subclasses of a class  
Author Message
Olli Plough





PostPosted: 2007-11-1 18:21:00 Top

java-programmer, How to determine subclasses of a class Hello,

I have a situation where all classes that implement a certain
interface are candidates to have a specific annotation. Now I want to
obtain all classes that implement that interface and select those that
have the anotation of interest. Question is how this can be done.
There seems to be no way you can ask a class for its subclasses (as
f.e. in Smalltalk), which would make things realy easy. Any ideas how
this can be solved? Do I have to write my own class loader? Hopefully
not ...

Thanks for any hints.
Cheers, Oliver

 
Are Nybakk





PostPosted: 2007-11-1 19:14:00 Top

java-programmer >> How to determine subclasses of a class Olli Plough wrote:
> Hello,
>
> I have a situation where all classes that implement a certain
> interface are candidates to have a specific annotation. Now I want to
> obtain all classes that implement that interface and select those that
> have the anotation of interest. Question is how this can be done.
> There seems to be no way you can ask a class for its subclasses (as
> f.e. in Smalltalk), which would make things realy easy. Any ideas how
> this can be solved? Do I have to write my own class loader? Hopefully
> not ...
>
> Thanks for any hints.
> Cheers, Oliver
>

What you ask is really out of my field, but I came to think of generics
wild cards the moment I read your post. Maybe that can be used somehow?

Good luck
 
Silvio Bierman





PostPosted: 2007-11-1 20:02:00 Top

java-programmer >> How to determine subclasses of a class Olli Plough wrote:
> Hello,
>
> I have a situation where all classes that implement a certain
> interface are candidates to have a specific annotation. Now I want to
> obtain all classes that implement that interface and select those that
> have the anotation of interest. Question is how this can be done.
> There seems to be no way you can ask a class for its subclasses (as
> f.e. in Smalltalk), which would make things realy easy. Any ideas how
> this can be solved? Do I have to write my own class loader? Hopefully
> not ...
>
> Thanks for any hints.
> Cheers, Oliver
>

There is no predefined way of achieving this. Class loaders are supposed
to supply the definition of a class X as soon as it is needed. They are
not intended for such queries, simply because the scope of your query is
undefined.
You probably mean all classes Y that implement interface X that are in
the classpath or something. That classpath is an artifact of the default
system class loader and as such is not a part of the Java language but
of its current implementation. If you would be using a class loader that
implements completely different class lookup logic the results would be
completely different.

If you are using the standard class loader then there is nothing that
keeps you from scanning the classpath, looking for .class files on it
and looking in all .tar and .zip files to see if the have .class files
in them. Load the classes you find and you can check if the implement X.

Kind regards,

Silvio Bierman
 
 
ram





PostPosted: 2007-11-1 20:52:00 Top

java-programmer >> How to determine subclasses of a class Olli Plough <email***@***.com> writes:
>obtain all classes that implement that interface and select those that

A similar operation exists in the library 籸am.jar?
This operation is based on Code by Ralf Ullrich.

One can find all classes of a jar that have any chosen
property, as long as this property can be obtained from the
class reference.

For example, to find all classes implementing 籮ava.util.Map?
one sets a filter accepting only classes which
籮ava.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.

However, the client does not need to specify this 籩ntryPath?
as "java.lang.Object" is the default obtained by extending
籨e.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 籸am.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

 
 
Lew





PostPosted: 2007-11-1 21:09:00 Top

java-programmer >> How to determine subclasses of a class Olli Plough wrote:
>> I have a situation where all classes that implement a certain
>> interface are candidates to have a specific annotation. Now I want to
>> obtain all classes that implement that interface and select those that
>> have the anotation of interest. Question is how this can be done.

In general, it cannot. The best you can hope for is to find all the classes
that *at the moment* implement the interface and are accessible in the classpath.

--
Lew
 
 
Daniel Pitts





PostPosted: 2007-11-2 9:15:00 Top

java-programmer >> How to determine subclasses of a class Lew wrote:
> Olli Plough wrote:
>>> I have a situation where all classes that implement a certain
>>> interface are candidates to have a specific annotation. Now I want to
>>> obtain all classes that implement that interface and select those that
>>> have the anotation of interest. Question is how this can be done.
>
> In general, it cannot. The best you can hope for is to find all the
> classes that *at the moment* implement the interface and are accessible
> in the classpath.
>
Or, use the Service Provider pattern. This I believe pattern used in the
Java crypto API.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
 
Roedy Green





PostPosted: 2007-11-2 9:29:00 Top

java-programmer >> How to determine subclasses of a class On Thu, 01 Nov 2007 03:21:17 -0700, Olli Plough <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>I have a situation where all classes that implement a certain
>interface are candidates to have a specific annotation. Now I want to
>obtain all classes that implement that interface and select those that
>have the anotation of interest. Question is how this can be done.
>There seems to be no way you can ask a class for its subclasses (as
>f.e. in Smalltalk), which would make things realy easy. Any ideas how
>this can be solved? Do I have to write my own class loader? Hopefully
>not ...

the catch is, many of the classes you want are probably not even
loaded yet. I think you will have to scan the byte codes of classes
on the classpath.

See http://mindprod.com/jgloss/javaclassformat.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Chris





PostPosted: 2007-11-4 2:42:00 Top

java-programmer >> How to determine subclasses of a class > I have a situation where all classes that implement a certain
> interface are candidates to have a specific annotation. Now I want to
> obtain all classes that implement that interface and select those that
> have the anotation of interest. Question is how this can be done.
> There seems to be no way you can ask a class for its subclasses (as
> f.e. in Smalltalk), which would make things realy easy. Any ideas how
> this can be solved? Do I have to write my own class loader? Hopefully
> not ...

I have the same problem. This is the best answer I have found:

http://weblogs.java.net/blog/enicholas/archive/2006/04/creating_a_serv.html

Unfortunately, it depends on putting a properties file in your jar that
references the classes the implement the interface. Not ideal.

JDK 1.6 has a service provide interface that's worth looking at.

http://java.sun.com/javase/6/docs/api/java/util/ServiceLoader.html

If you find a more direct way, please post it here.