Java Reflections: Get Classes but not SubTypes

六月ゝ 毕业季﹏ 提交于 2020-04-13 08:05:08

问题


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)

回答1:


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);


来源:https://stackoverflow.com/questions/32782240/java-reflections-get-classes-but-not-subtypes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!