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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  • Is Big Data Dying?
  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  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
13.9K 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

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!