DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  1. DZone
  2. Coding
  3. Java
  4. How to Find All the Classes of a Package in Java

How to Find All the Classes of a Package in Java

In this article let's take a look at how to find all classes of a package in Java

By 
Jonathan Burton user avatar
Jonathan Burton
·
Nov. 18, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
14.2K Views

Join the DZone community and get the full member experience.

Join For Free

To find all classes of a package in Java we can use the ClassHunter of Burningwave Core library. So we start by adding the following dependency to our pom.xml:

XML
x
 
1
<dependency>
2
    <groupId>org.burningwave</groupId>
3
    <artifactId>core</artifactId>
4
    <version>8.4.0</version>
5
</dependency>


The next steps are the following:

  • retrieving the ClassHunter through the ComponentContainer
  • defining a regular expression that we must pass to the ClassCriteria object that will be injected into the SearchConfig object
  • calling the loadInCache method that loads in the cache all loadable classes of the indicated paths, then applies the criteria filter and then returns the SearchResult object which contains the classes that match the criteria

And now let’s find all classes whose package contains “springframework” string:

Java
xxxxxxxxxx
1
12
 
1
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
2
ClassHunter classHunter = componentSupplier.getClassHunter();
3
4
CacheableSearchConfig searchConfig = SearchConfig.byCriteria(
5
    ClassCriteria.create().allThat((cls) -> {
6
        return cls.getPackage().getName().matches(".*springframework.*");
7
    })
8
);
9
10
try (SearchResult searchResult = classHunter.loadInCache(searchConfig).find()) {
11
    return searchResult.getClasses();
12
}


In the example above the search is performed, as default, in all the runtime class paths but, if we want to search only in some jar of the runtime class paths or in other jar outside the runtime class paths we can expressly indicate them:

Java
xxxxxxxxxx
1
16
 
1
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
2
PathHelper pathHelper = componentSupplier.getPathHelper();
3
ClassHunter classHunter = componentSupplier.getClassHunter();
4
5
CacheableSearchConfig searchConfig = SearchConfig.forPaths(
6
    pathHelper.getPaths(path -> path.matches(".*?spring.*?.jar"))
7
).by(
8
    ClassCriteria.create().allThat((cls) -> {
9
        return cls.getPackage().getName().matches(".*springframework.*");
10
    })
11
);
12
13
try (SearchResult searchResult = classHunter.loadInCache(searchConfig).find()) {
14
    return searchResult.getClasses();
15
}


Let’s break down the example above:

  • In the forPaths method we can add all absolute paths we want: both folders, zip, jar, ear, war and jmod files will be recursively scanned and in this case we are scanning only jar whose name contains the string spring
  • The loadInCache method loads all classes in the paths of the SearchConfig received as input and then execute the queries of the ClassCriteria on the cached data. Once the data has been cached, it is possible to take advantage of faster searches of the loaded paths even by simply calling the findBy method.

In this article we have learned an alternative way to find classes for a given criterion that we can change in any way according to our needs and the complete source code is available on GitHub.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook