• Post Reply Bookmark Topic Watch Topic
  • New Topic

All Classes in a Package?  RSS feed

 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't seem from the APIs of Package, Class, or ClassLoader that there is an obvious way of accessing all the class objects in a particular package.

Anyone know of a good way to go about doing that?

I've seen some hinting at searching for jar files and loading them as resources, navigating them with a JarInputStream, or using public libraries to read the bytecode (javassist or ASM).

That seems more power than I need (or effort than I'm hoping to exert).

Just being able to access the class names of all the classes from one package known to one class loader would be fine with me.
 
Eric Weinberg
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,

This link together with this link seems to accomplish what you're looking for.

Essentially, the idea is to treat the package name as a directory (which it is) then you manipulate the File object to get all the classes.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small, simple program that does that kind of thing. It output:

Searching for package java.util.*

java/util/AbstractList$1 found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
java/util/AbstractMap$1$1 found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
...
java/util/Properties found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
java/util/Hashtable found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
java/util/Dictionary found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
java/util/Map found in C:\Program Files\Java\jre1.5.0_04\lib\rt.jar
236 matches found


I'm in the process of putting it on SourceForge.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Weinberg:
Essentially, the idea is to treat the package name as a directory (which it is) then you manipulate the File object to get all the classes.


I'm not sure whether Norm's code takes this into account, but it is possible classes in a single package to be defined in multiple places on the classpath.

Take as an example a code base with three folders on the classpath named /one /two and /three all with sub directories com/javaranch/dave

Then /one contains class com/javaranch/dave/One.class /two contains com/javaranch/dave/Two.class and /three contains com/javaranch/dave/Three.class

Not insurmountable, just annoying.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!