Get All Classes by a Package Name
See original GitHub issueI needed a way to get all classes that starts with a certain package name, followed by a “.”. The only way to do that is by getting all the classes (ClassPath.getAllClasses()) and filtering the result by yourself. There’s no reason that we could only get the top classes by a certin package name, and not all. I’m asking for a simple change, to add the following 2 methodes:
/** Returns all classes whose package name is {@code packageName}. */
public ImmutableSet<ClassInfo> getAllClasses(String packageName) {
checkNotNull(packageName);
ImmutableSet.Builder<ClassInfo> builder = ImmutableSet.builder();
for (ClassInfo classInfo : getAllClasses()) {
if (classInfo.getPackageName().equals(packageName)) {
builder.add(classInfo);
}
}
return builder.build();
}
/**
* Returns all classes whose package name is {@code packageName} or starts with
* {@code packageName} followed by a '.'.
*/
public ImmutableSet<ClassInfo> getAllClassesRecursive(String packageName) {
checkNotNull(packageName);
String packagePrefix = packageName + '.';
ImmutableSet.Builder<ClassInfo> builder = ImmutableSet.builder();
for (ClassInfo classInfo : getAllClasses()) {
if (classInfo.getName().startsWith(packagePrefix)) {
builder.add(classInfo);
}
}
return builder.build();
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Finding All Classes in a Java Package - Baeldung
In this tutorial, we'll explore several examples of how to find all classes in a Java package at runtime. 2. Class Loaders.
Read more >java - Can you find all classes in a package using reflection?
It can be used to get all classes in a package: ... @param directory The base directory * @param packageName The package name...
Read more >Get All Classes Within A Package - DZone
Join the DZone community and get the full member experience. Join For Free. The code below gets all classes within a given package....
Read more >Gets a list of all classes in the given package ... - Java2s.com
Gets a list of all classes in the given package and all subpackages recursively. - Java Reflection. Java examples for Reflection:package.
Read more >How to find all classes of a package in Java - Burningwave
The fastest and most optimized way to find all the classes of a package is by iterating the resources accessible through ClassLoader thus...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t think the code that this saves is common enough/complicated enough to warrant a method in Guava. It’s also much simpler with Streams.
One more catch: in
getAllClassesRecursive(String packageName)
, similar to ClassPath.html#line.151, you should check thatclassInfo.getName().startsWith(packagePrefix)
instead ofclassInfo.getPackageName().startsWith(packagePrefix)
, otherwise you exclude base package (packageName
doesn’t start withpackageName + "."
).