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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Trending

  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • Why Vulnerability Management Fails Without Reliable Asset Data
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • To Switch, or Not to Switch the AI Model

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Nov. 18, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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