How can I reflectively get all of the packages in the project? I started with Package.getPackages(), but that only got all the packages associated to the current package. Is there a way to do this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This approach prints all packages only (at least a root "packageName" has to be given first).
It is derived from above.
May be off-topic (because it is not exactly in terms of java "reflection") ... However, how about the following solution:
Java packages can be treated as folders (or directories on Linux\UNIX). Assuming that you have a root package and its absolute path is known, you can recursively print all sub-folders that have *.java or *.class files using the following batch as the basis:
You can wrap this batch in java or if you run on Linux\Unix rewrite it in some shell script.
Good Luck!
Aviad.
@PhilippWendler's comment led me to a method of accomplishing what I needed. I tweaked the method a little to make it recursive.
It's possible but tricky and expensive, since you need to walk the classpath yourself. Here is how TestNG does it, you can probably extract the important part for yourself:
https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/internal/PackageUtils.java