Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner TypeAnnotationsScanner was not configured, when used from a referenced library

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?

like image 279
samairtimer Avatar asked Mar 06 '20 07:03

samairtimer


2 Answers

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);
like image 86
emeraldjava Avatar answered Oct 12 '22 22:10

emeraldjava


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.

like image 24
Oleksandr Avatar answered Oct 12 '22 23:10

Oleksandr