Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How to determine subclasses of a class

Thread view: 
Olli Plough - 01 Nov 2007 11:21 GMT
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 - 01 Nov 2007 12:14 GMT
> Hello,
>
[quoted text clipped - 9 lines]
> 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 - 01 Nov 2007 13:01 GMT
> Hello,
>
[quoted text clipped - 9 lines]
> 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
Lew - 01 Nov 2007 14:08 GMT
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.

Signature

Lew

Daniel Pitts - 02 Nov 2007 02:14 GMT
> Olli Plough wrote:
>>> I have a situation where all classes that implement a certain
[quoted text clipped - 5 lines]
> 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.

Signature

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

Stefan Ram - 01 Nov 2007 13:52 GMT
>obtain all classes that implement that interface and select those that

 A similar operation exists in the library »ram.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 »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.

 However, 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
Roedy Green - 02 Nov 2007 02:29 GMT
>I have a situation where all classes that implement a certain
>interface are candidates to have a specific annotation. Now I want to
[quoted text clipped - 4 lines]
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chris - 03 Nov 2007 19:42 GMT
> I have a situation where all classes that implement a certain
> interface are candidates to have a specific annotation. Now I want to
[quoted text clipped - 4 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.