In the first place, the RetentionPolicy
dictates what a conforming compiler has to do. Annotation
s with RetentionPolicy.SOURCE
do not make it into the class file while the other two, RetentionPolicy.CLASS
and RetentionPolicy.RUNTIME
, are stored within the class file but using different attributes to allow to make a distinction between them when reading the class file.
The documentation of RetentionPolicy.CLASS
says:
Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.
Here, the responsibility is clearly documented, the VM shall not retain them and the Reflection API built into the JRE conforms to it. Though “need not be retained” does not sound like a strong requirement.
But 3rd party libraries like the Reflection Library you are using are free to implement whatever they want when parsing a class file. Since the documentation for the method you have called simply says: “get types annotated with a given annotation”, the behavior isn’t wrong as the type has that annotation.
And you are able to find out the RetentionPolicy
of that Annotation
even before invoking that method by analyzing the Annotations
of the Annotation
. So it makes no sense invoking the method when you already know that the annotation has the RetentionPolicy.CLASS
and then bother because the method does something instead of nothing.
But, of course, it would be better if that behavior was documented completely. So you might ask the author of that 3rd party library to improve the documentation.