views:

137

answers:

1

If I have a package (foo.bar), is there some groovy sugar that makes it easy for me to enumerate all the classes in said package?

+2  A: 

Groovy is built on top of Java so when it comes to class loading, any Groovy class is just a Java class. Java does not provide a way to do what you are looking for. You would think it would be a method on http://java.sun.com/javase/6/docs/api/java/lang/Package.html but there is not anything to list the classes.

There is a way you can do it if you really need though. Classes are just files on disk. Listing the files that end in .class would give you the list of Java classes. If you are looking for the contents of a Jar file (which is just a zip, see this for reading jars), you can open the jar and look at each class in the archive. Directory structure always corresponds to the package structure so it should not be hard to take a package you are looking for and list all .class files in that directory.

Chris Dail
Yeah, that's where I ended up... I was hoping that there was something in the GDK I had missed that addressed this issue... but it doesn't take a whole lot of code to get it done.
danb