A beginner question about reflection, I suppose:
Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package
, it would seem like no.)
A beginner question about reflection, I suppose:
Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package
, it would seem like no.)
It is not possible, since all classes in the package might not be loaded, while you always knows package of a class.
Due to the dynamic nature off class loaders, this is not possible. Class loaders are not required to tell the VM which classes it can provide, instead they are just handed requests for classes, and have to return a class or throw an exception.
However, if you write your own class loaders, or examine the classpaths and it's jars, it's possible to find this information. This will be via filesystem operations though, and not reflection. There might even be libraries that can help you do this.
If there are classes that get generated, or delivered remotely, you will not be able to discover those classes.
The normal method is instead to somewhere register the classes you need access to in a file, or reference them in a different class. Or just use convention when it comes to naming.
You could use this method listed at http://snippets.dzone.com/posts/show/4831, using the Classloader.
Provided you are not using any dynamic class loaders you can search the classpath and for each entry search the directory or JAR file.
More detail discussion about this issue:http://forums.sun.com/thread.jspa?threadID=341935