Scanner TypeAnnotationsScanner was not configured, when used from a referenced library
Solution 1
Update 4 October 2021:
This bug was fixed in version 0.10.
Previous answer:
Maybe not the type of answer one wants to hear but reflections
version 0.9.12
contains a bug described here: https://github.com/ronmamo/reflections/issues/273
The bugfix has been provided in this PR (not merged yet): https://github.com/ronmamo/reflections/pull/278
The major difference between version 0.9.11
and 0.9.12
is that in 0.9.12
Guava dependency was removed in favor of Java 8 Streams API.
If you need to include this dependency without Guava transitive dependency, you may look at the next project: https://github.com/aschoerk/reflections8
Currently, the latest available version of reflections8
is 0.11.7
.
Notice, reflections8
is now considered to be obsolete due to 0.9.12
release of reflections
but their version 0.11.7
doesn't contain this bug and this version doesn't depend on Guava. So, a possible solution would be to switch to net.oneandone.reflections8:reflections8:0.11.7
.
Solution 2
Using 0.9.12 of the reflections library I found registering the Scanner for each ElementType of the annotation was required.
MyAnnotation has these ElementTypes
@Target({ElementType.TYPE,ElementType.FIELD})
public @interface MyAnnotation {
And in my reflections code I construct it like this
Reflections reflections = new Reflections("my.package.*",
new SubTypesScanner(),
new TypeAnnotationsScanner(),
new FieldAnnotationsScanner());
//FieldAnnotationsScanner
Set<Field> fields = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
System.out.println(fields);
Solution 3
this works for me: new Reflections(packageName, new TypeAnnotationsScanner());
Solution 4
Usage of Reflections8 didn't work for me, but on the page https://www.baeldung.com/reflections-library I found a solution, which worked in my case. When a resource scanner is provided in the constructor, Reflections found the resources:
Reflections reflections = new Reflections("de.my_company",new ResourcesScanner());
Set<String> foundResources = reflections.getResources(Pattern.compile(".*.xml"));
for (String foundResource:foundResources) {
System.out.println(foundResource);
}
Related videos on Youtube
samairtimer
Updated on June 04, 2022Comments
-
samairtimer almost 2 years
I have a class which uses
org.reflections.Reflections
to get annotations in classes in the class path.When i use it in the same project everything works fine, but when i export it as a JAR and refer it in a different class i get the below execption:
Exception in thread "main" org.reflections.ReflectionsException: Scanner TypeAnnotationsScanner was not configured at org.reflections.Store.get(Store.java:39) at org.reflections.Store.get(Store.java:61) at org.reflections.Store.get(Store.java:46) at org.reflections.Reflections.getTypesAnnotatedWith(Reflections.java:429)
Here is the code snippet:
System.out.println("Package to Scan: "+getPackageName()); final Reflections reflections = new Reflections(getPackageName(), new TypeAnnotationsScanner()); final Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(HandlerMapping.class,true);
I do provide a
TypeAnnotationsScanner
object, but still issue persists.Note: It does not work only when above code is referred as a jar. (I created a fat jar using maven-assembly plugin)
Any pointers?
-
Oleksandr about 4 yearsI am facing the same issue with
reflections
version0.9.12
. In0.9.11
I don't have such problems
-
-
samairtimer almost 4 yearsLooks like the only way ahead till the bug is fixed.