Try to get the class under a specific package using com.google.common.reflect.ClassPath.
pom.xml
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
The getTopLevelClassesRecursive method recursively fetches classes from packages below the specified package. If you just want to get the list of classes in the specified package, use the getTopLevelClasses method.
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Set<Class<?>> classes = ClassPath.from(loader)
.getTopLevelClassesRecursive(getClass().getPackage().getName()).stream()
.map(info -> info.load())
.collect(Collectors.toSet());
classes.forEach(System.out::println);
} catch (IOException e) {
// TODO
}
Recommended Posts