Search code examples
javareflectionreflections

Java Reflections library: find subclasses in package non recursively


Given the following snippet:

Set<Class<? extends MyClass>> allClasses = 
  new Reflections("mypackage").getSubTypesOf(MyClass.class);

The library recursively search for all the classes extending MyClass in the given package and subpackages. I need to do this in the given package only, i.e., not in the subpackages. Is there a way to do so?


Solution

  • This is brute force, but:

    1) Get subclasses of MyClass. Put in a set.

    2) Iterate over classes in the set; remove classes which belong to other packages (i.e. those for which _someClass.getPackage().equals(MyClass.class.getPackage()) is false)