How to list groovy classes in a specific package?

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

+3
source share
1 answer

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

There is a way you can do this if you really need to. Classes are just files on disk. A list of files that end in .class will provide you with a list of Java classes. If you are looking for the contents of the Jar file (it's just a zip, see this for reading jars ), you can open the jar and look at each class in the archive. The directory structure always matches the package structure, so it’s easy for you to take the package you are looking for and list all the .class files in this directory.

+4
source

Source: https://habr.com/ru/post/1716943/


All Articles