[JAVA] Get a list of classes in a Guava specific package

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

Get a list of classes in a Guava specific package
Get the path defined in Controller class of Spring boot as a list
Get a list of MBean information for Java applications
Extract a specific element from the list of objects
Check the dependency of a specific maven artifact in Coursier
A program that counts the number of words in a List
Get a proxy instance of the component itself in Spring Boot
Java, JS (jQuery) and Ajax. Get a specific value of JSON.
A list of rawValues for UITextContentType.
Sort a List of Java objects
List of members added in Java 9
Cast an array of Strings to a List of Integers in Java
Get the public URL of a private Flickr file in Java
How to output a list of strings in JSF as comma-separated strings
Get stuck in a Java primer
List of types added in Java 9
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
Get a rough idea of the differences between protocols, classes and structs!
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
Get the result of POST in Java
[Java] Get the day of the specific day of the week
[rails] List of actions defined in Controller
Summary of root classes in various languages
Create a List that holds multiple classes
Ability to display a list of products
How to get the ID of a user authenticated with Firebase in Swift
[Rails] How to get rid of flash messages in a certain amount of time
Stuck in an enum in front of a blacksmith
About the idea of anonymous classes in Java
Set the time of LocalDateTime to a specific time
Rails logger Get a rough idea in 1 minute
A quick review of Java learned in class
Create a native extension of Ruby in Rust
Get a list of other sessions of the same user when using Redis Session in Spring Boot (2 series). Also discard it.