Logo

dev-resources.site

for different kinds of informations.

How to find all the classes of a package in Java

Published at
12/7/2020
Categories
java
tutorial
classpath
scanner
Author
roberto_gentili
Categories
4 categories in total
java
open
tutorial
open
classpath
open
scanner
open
Author
15 person written this
roberto_gentili
open
How to find all the classes of a package in Java

To find all classes of a package in Java we need to use the ClassHunter of Burningwave Core library. The ClassHunter is a classpath scan engine that queries iterated classes and returns only the classes that match a chosen criteria, moreover the searches are executed in a multithread context and recursively through folder and supported compressed files (zip, jar, jmod, war and ear) even in nested compressed files. To use the ClassHunter you should simply add the following to your projects dependencies:

To perform a scan it is necessary to create a configuration object of type SearchConfig to which must be (optionally) passed the paths to be scanned and the query criteria represented by the ClassCriteria. If no path will be passed to SearchConfig, the scan will be executed on the paths indicated by the ‘paths.hunters.default-search-config.paths’ property of the burningwave.properties.

The fastest and most optimized way to find all the classes of a package is by iterating the resources accessible through ClassLoader thus avoiding that ClassLoader loads the classes that are outside the scanned paths:

try (SearchResult result = classHunter.findBy(
    //Highly optimized scanning by filtering resources before loading from ClassLoader
    SearchConfig.forResources(
        "org/springframework"
    )
)) {
    return result.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

The findBy method loads all classes in the paths of the SearchConfig received as input and then execute the queries of the ClassCriteria. The findBy method also caches data so after the first search it is possible to take advantage of faster searches. In addition to the loadCache method, loading data into the cache can also take place via the findBy method if the latter receives a SearchConfig without ClassCriteria as input.

Finding all classes that have package name that matches a regex

ComponentSupplier componentSupplier = ComponentContainer.getInstance();
ClassHunter classHunter = componentSupplier.getClassHunter();

SearchConfig searchConfig = SearchConfig.byCriteria(
    ClassCriteria.create().allThoseThatMatch((cls) -> {
        return cls.getPackage().getName().matches(".*springframework.*");
    })
);

try (ClassHunter.SearchResult searchResult = classHunter.findBy(searchConfig)) {
    return searchResult.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

Finding all annotated classes of a package without considering the classes hierarchy

try (
    SearchResult result = classHunter.findBy(
        //Highly optimized scanning by filtering resources before loading from ClassLoader
        SearchConfig.forResources(
            "org/springframework"
        ).by(
            ClassCriteria.create().allThoseThatMatch((cls) -> {
                return cls.getAnnotations() != null && cls.getAnnotations().length > 0;
            }).or().byMembers(
                MethodCriteria.withoutConsideringParentClasses().allThoseThatMatch((method) -> {
                    return method.getAnnotations() != null && method.getAnnotations().length > 0;
                })
            ).or().byMembers(
                FieldCriteria.withoutConsideringParentClasses().allThoseThatMatch((field) -> {
                    return field.getAnnotations() != null && field.getAnnotations().length > 0;
                })
            ).or().byMembers(
                ConstructorCriteria.withoutConsideringParentClasses().allThoseThatMatch((ctor) -> {
                    return ctor.getAnnotations() != null && ctor.getAnnotations().length > 0;
                })
            )
        )
    )
) {
    return result.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

Finding all annotated classes of a package by considering the classes hierarchy

try (
        SearchResult result = classHunter.findBy(
        //Highly optimized scanning by filtering resources before loading from ClassLoader
        SearchConfig.forResources(
            "org/springframework"
        ).by(
            ClassCriteria.create().allThoseThatHaveAMatchInHierarchy((cls) -> {
                return cls.getAnnotations() != null && cls.getAnnotations().length > 0;
            }).or().byMembers(
                MethodCriteria.forEntireClassHierarchy().allThoseThatMatch((method) -> {
                    return method.getAnnotations() != null && method.getAnnotations().length > 0;
                })
            ).or().byMembers(
                FieldCriteria.forEntireClassHierarchy().allThoseThatMatch((field) -> {
                    return field.getAnnotations() != null && field.getAnnotations().length > 0;
                })
            ).or().byMembers(
                ConstructorCriteria.forEntireClassHierarchy().allThoseThatMatch((ctor) -> {
                    return ctor.getAnnotations() != null && ctor.getAnnotations().length > 0;
                })
            )
        )
    )
) {
    return result.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

Finding all classes of a package that have a particular annotation on at least one Field without considering the classes hierarchy

ComponentSupplier componentSupplier = ComponentContainer.getInstance();
ClassHunter classHunter = componentSupplier.getClassHunter();

try (
    SearchResult result = classHunter.findBy(
        //Highly optimized scanning by filtering resources before loading from ClassLoader
        SearchConfig.forResources(
            "org/springframework"
        ).by(
            ClassCriteria.create().byMembers(
                FieldCriteria.withoutConsideringParentClasses().allThoseThatMatch((field) -> {
                    return field.getAnnotation(NotNull.class) != null;
                })
            )
        )
    )
) {
    return result.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

Finding all classes of a package that have a particular annotation on at least one Field by considering the classes hierarchy

try (
    SearchResult result = classHunter.findBy(
        //Highly optimized scanning by filtering resources before loading from ClassLoader
        SearchConfig.forResources(
            "org/springframework"
        ).by(
            ClassCriteria.create().byMembers(
                FieldCriteria.forEntireClassHierarchy().allThoseThatMatch((field) -> {
                    return field.getAnnotation(NotNull.class) != null;
                })
            )
        )
    )
) {
    return result.getClasses();
}
Enter fullscreen mode Exit fullscreen mode

In this article we learned how to find all classes within a package and the complete code of the example above is available here.

scanner Article's
29 articles in total
Favicon
Printer Scanners VS Mobile Scanner - Do Printers Still Have a Role?
Favicon
Top Notch Solutions on OMR Sheet, Scanners, Softwares & Testing
Favicon
Barcode Printer
Favicon
Building a .NET TWAIN Document Scanner Application for Windows and macOS using MAUI
Favicon
How to create a React Native Document Scanner
Favicon
How to create a Flutter Document Scanner
Favicon
Explorando redes com ESP32 WIFI.
Favicon
Building a Document Digitization App with Flutter for TWAIN, WIA, and eSCL Scanners
Favicon
How to Scan Documents from TWAIN, WIA, SANE Compatible Scanners in Python
Favicon
Comparison of barcode scanner libraries
Favicon
How to Scan Documents from eSCL Scanners in Web Applications
Favicon
How to Add a QR and Barcode Scanner to Your Vue.js App
Favicon
Integration of SAP WM with Barcode Scanners | Speranza IoT Blog
Favicon
Criando scanner TCP/IP em Golang
Favicon
Top 10 trending github repos for Java developers in this week🏀.
Favicon
How to Build Windows Virtual Scanner and Feed Custom Images
Favicon
How to Build a QR Code Scanner for Windows and Android with Qt QML
Favicon
How to Implement a Flutter QR Code Scanner Plugin from Scratch
Favicon
HackerRank #27 | 1D Array | 🇧🇷
Favicon
HackerRank #6 | Scanner e End-of-file | 🇧🇷
Favicon
Prototyping today is happening like this !
Favicon
...here it is
Favicon
Diving into Dynamsoft JavaScript Barcode Scanner
Favicon
All Formats QR Scanner & Barcode Creator
Favicon
A fast and flexible way to scan the class paths in Java
Favicon
How to find all the classes of a package in Java
Favicon
Vue Sci-Fi Scanner Transition
Favicon
BarCode and QR Code Scanning in React Native Apps
Favicon
8 Best WordPress Malware Scanners in 2024[Updated]

Featured ones: