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 / August 2007

Tip: Looking for answers? Try searching our database.

How to find all classes in package XXX implementing interface YYY

Thread view: 
Sideswipe - 28 Aug 2007 18:52 GMT
I would like to use reflection in someway to load all classes in a
given package that implement an interface.
Basically, this is for a factory that loads all classes in the same
package as it, by default, with the ability to override.

I assume this is possible but the documentation isn't jumping out at
me on how that's done.

Ideas anyone?

Christian Bongiorno
http://christian.bongiorno.org
Hunter Gratzner - 28 Aug 2007 19:37 GMT
> I would like to use reflection

Reflection is usually the wrong tool. Think twice before you use it.
E.g. if you do this so a superclass knows all its subclasses then you
probably have a design error and you should rethink your design.

But you have other problems, too.

> in someway to load all classes in a
> given package that implement an interface.

Since a package is open-ended you can't find all classes in a given
package. Please spare us the discussion of this fact. If you don't
believe it, read the Java spec. Without having all potential
candidates you can't find all classes in a given package that
implement a particular interface.

> I assume this is possible but the documentation isn't jumping out at
> me on how that's done.

It is not possible.

> Ideas anyone?

Modify your requirement. For example, all classes in a jar or a
directory to which you have the pathname. Or all classes from a (text)
list of class names.
Lew - 28 Aug 2007 22:14 GMT
Sideswipe wrote:
>> in someway to load all classes in a
>> given package that implement an interface.

> Since a package is open-ended you can't find all classes in a given
> package. Please spare us the discussion of this fact. If you don't
> believe it, read the Java spec. Without having all potential
> candidates you can't find all classes in a given package that
> implement a particular interface.

>> I assume this is possible but the documentation isn't jumping out at
>> me on how that's done.

> It is not possible.

To expound slightly more on Hunter's point, though as he says if you google
around the Usenet archives you will find quite a bit on this FAQ, new classes
can appear in a package any time.  You'd have to run your search for infinite
time.

It's like making a dictionary of every word in the English language.  The
language changes while you're compiling the list.  You can never finish, Sisyphus.

Signature

Lew

Daniel Pitts - 29 Aug 2007 01:49 GMT
> Sideswipe wrote:
> >> in someway to load all classes in a
[quoted text clipped - 18 lines]
> --
> Lew

Often people think of this as a solution to the service provider
pattern.  Java has a specification for providing service providers. I
suggest you look at it.
Sideswipe - 29 Aug 2007 18:01 GMT
I didn't realize this was such a FAQ. More search did turn up some
ideas. I also fully understand the English dictionary analogy. But,
even though that's true it doesn't stop people from printing
dictionaries. Which is what I was looking for.
Sideswipe - 29 Aug 2007 17:59 GMT
Generally I don't bother responding to a rude poster, but this time I
will.

I wasn't looking for the end-all be all list of classes implementing
an interface in a package. Only the ones at startup and currently in
the class loader.

I wasn't asking you to _judge_ my question and ponder what stupid
thing I might be cooking up either.

This post from sun shows a possibility:
http://forum.java.sun.com/thread.jspa?threadID=341935&start=15

I actually did want to limit the scope to only classes in the same
jar. Try to actually be a positive member of this community and show
how even a limited case could be done and what the caveats are.

-----------------
Perhaps a an interesting extension to ClassLoader would be to add a
listener method for every time a new class is loaded.
Andrew Thompson - 29 Aug 2007 18:40 GMT
...
>I wasn't looking ...
...
>I wasn't asking ...

Did you consider contracting a help-desk or consultant?*
Be sure to mention those conditions when signing up, so
they might charge the appropriate rate.  This, OTOH, is
a discussion forum.

>...Try to actually be a positive member of this community

Try not to spit the dummy when someone pursues the
technical matter, or questions the entire strategy.

>...and show
>how even a limited case could be done and what the caveats are.

* This is not a help desk, in case you might have
any illusions.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 29 Aug 2007 02:54 GMT
On Tue, 28 Aug 2007 17:52:27 -0000, Sideswipe
<christian.bongiorno@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>I would like to use reflection in someway to load all classes in a
>given package that implement an interface.

see http://mindprod.com/project/interfacefinder.html
Signature

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

Andreas Leitgeb - 29 Aug 2007 11:06 GMT
> see http://mindprod.com/project/interfacefinder.html

While I understand that you do not give any free support
to takers of such a project, but I've got a bugreport
about the text there:
"  2.) Split it at the semicolons with Regex.split.

Semicolons? That's only on MsWindows-platform, so
rather than splitting at semicola, one should
*always* split paths at File.pathSeparator instead.


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



©2008 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.