问题
I have a package
package one.two.three
and in that package I have
public trait first
public class Second
and
pubic class Third
I want to get all the classes and interface(in one.two.three) names as Strings. heard that the best way to do it is using reflection.
can someone tell me a way to do that thing please.
回答1:
Well, that is a bit more complex task than one would thought.
For instance, you may have classes in the same package but in different locations (e.g., 2 JAR files loaded by an UrlClassLoader
may contain a package a.b.c
).
The best way is to use already existent tools and libraries, such as the Reflections API for instance (I'm not aware of a Scala wrapper or alternative yet). They offer a robust and well tested interface for such queries.
来源:https://stackoverflow.com/questions/21015700/get-all-the-class-names-in-a-package-with-scala