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

  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • A Spring Boot App With Half the Startup Time
  • Implementing the Planning Pattern With Java Enterprise and LangChain4j

Trending

  • AI, OAuth, and Other Platform APIs in the Core
  • AI Broke Your Definition of Done
  • I Built a VS Code Extension to Debug Azure AI Foundry Agents Without Leaving My Editor
  • The Reliability Gap: Why Enterprise AI Keeps Failing After It Already Works
  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

  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • A Spring Boot App With Half the Startup Time
  • Implementing the Planning Pattern With Java Enterprise and LangChain4j

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