Search code examples
javareflection

Java Reflections: Get Classes but not SubTypes


I'm upgrading from org.reflections:reflections:0.9.5 to version 0.9.9. I am using:

Reflections reflectionPaths = new Reflections("resources", new TypeAnnotationsScanner());
Set<Class<?>> rootResourceClasses = reflectionPaths.getTypesAnnotatedWith(Path.class);

Which gets me all classes in the resources package with an @Path Annotation. Since the library has been updated the top line requires an extra new SubTypesScanner() for the code to run. I however do not want sub types to be returned.

How can I use this new version of the library to pull back only classes and interfaces that are not sub types?


I get this Exception if I don't include the SubTypesScanner

org.reflections.ReflectionsException: Scanner SubTypesScanner was not configured
    at org.reflections.Store.get(Store.java:58)
    at org.reflections.Store.get(Store.java:70)
    at org.reflections.Store.getAll(Store.java:97)
    at org.reflections.Reflections.getAllAnnotated(Reflections.java:423)
    at org.reflections.Reflections.getTypesAnnotatedWith(Reflections.java:384)
    at org.reflections.Reflections.getTypesAnnotatedWith(Reflections.java:370)

Solution

  • I believe you using this annotation "javax.ws.rs.Path". Pls try this :-

    Reflections reflectionPaths = new Reflections("resources", new TypeAnnotationsScanner());
    Set<Class<?>> rootResourceClasses = reflectionPaths.getTypesAnnotatedWith(Path.class, true);