question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get All Classes by a Package Name

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ronshapirocommented, Aug 8, 2019

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.

1reaction
perceptron8commented, Aug 23, 2017

One more catch: in getAllClassesRecursive(String packageName), similar to ClassPath.html#line.151, you should check that classInfo.getName().startsWith(packagePrefix) instead of classInfo.getPackageName().startsWith(packagePrefix), otherwise you exclude base package (packageName doesn’t start with packageName + ".").

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found