mardi 25 juillet 2017

getTypesAnnotatedWith returns classes without annotation

I'm trying to use class annotations in Java, and I have the strange behaviour where getTypesAnnotatedWith returns a class on which getAnnotation returns null.

Is there some reason getTypesAnnotatedWith returns classes without annotation?

For example, in my project the following piece of code returns (correctly) one of the classes I annotated:

new Reflections("my.nice.package")
    .getTypesAnnotatedWith(MyTag.class)
    .iterator().next();

However, if I call getAnnotation on it, the result is null:

new Reflections("my.nice.package")
    .getTypesAnnotatedWith(MyTag.class)
    .iterator().next()
    .getAnnotation(MyTag.class));

This is the annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTag {
    public String value();
}

and, for completeness, this is how I annotate classes:

@MyTag("FooBar")
class MyClass {
    // ...
}





Aucun commentaire:

Enregistrer un commentaire