Skip to content
Advertisement

Why is `getDeclaredAnnotations()` returning an empty list when an annotation is present?

The Javadocs entry for getDeclaredAnnotations say the following:

Returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

So, I expect this function to return an array of length 1 on doSometing, yet, it returns an array of length 0. Why? getAnnotation for the relevant type also returns null.

MCVE:

JavaScript

Actual MCVE output:

JavaScript

Expected MCVE output:

JavaScript

Advertisement

Answer

According to the Java Language Specification §9.6.4.2,

If T does not have a (meta-)annotation m that corresponds to java.lang.annotation.Retention, then a Java compiler must treat T as if it does have such a meta-annotation m with an element whose value is java.lang.annotation.RetentionPolicy.CLASS.

Your ExampleAnnotation does not have a @Retention meta-annotation, so it is only retained in the class file. However, it must be retained at runtime in order to be accessed with reflection. From the docs of RetentionPolicy.RUNTIME:

Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

Compare that with the description for CLASS:

Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

So to get your desired output, you should declare your ExampleAnnotation like this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement