How to get list of classes from a package
Rémi Forax
forax at univ-mlv.fr
Mon Feb 16 20:35:53 UTC 2009
Tom Hawtin a écrit :
> David M. Lloyd wrote:
>> On 02/16/2009 10:22 AM, Ulf Zibis wrote:
>>>
>>> can anybody tell me, how I could get al list or enumeration of the
>>> classes, which belong to a package.
>>> I can't see any appropriate method in java.lang.Package :-(
>>
>> This isn't really possible at run time, since one doesn't know
>> whether a class exists until one tries to load it. The classloader
>> might not even know. Also, a package can span classloaders, adding
>> more complexity to the problem.
>
> IIRC, you will get a different Package for each class loader, for a
> given package name. Certainly classes with the same package name will
> not have Java access to default/protected
> members/classes/interface/constructors from different class loaders.
>
> But yes, a list of classes is not necessarily complete. Also, IIRC,
> Proxy dynamically inserts classes into packages as well.
>
> Tom Hawtin
Ulf, if you can create an agent, you can get all loaded classes:
http://download.java.net/jdk7/docs/api/java/lang/instrument/Instrumentation.html#getAllLoadedClasses()
But if you want thoses classes to find some that implement an interface,
java.util.ServiceLoader is your friend:
http://download.java.net/jdk7/docs/api/java/util/ServiceLoader.html
cheers,
Rémi
More information about the core-libs-dev
mailing list