mardi 25 août 2020

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:

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class AnnotationTest{

    public static void main(String[] args){
        Class<?> clazz = AnnotationTest.class;
        for(Method method : clazz.getMethods()){
            System.out.println(method.getName() + ":");
            for(Annotation annotation : method.getDeclaredAnnotations()){
                System.out.println(" - " + annotation.annotationType().getName());
            }
            System.out.println();
        }
    }

    @ExampleAnnotation
    public void doSomething(){}

    public @interface ExampleAnnotation{}

}

Actual MCVE output:

main:

doSomething:

wait:

wait:

wait:

equals:

toString:

hashCode:
 - jdk.internal.HotSpotIntrinsicCandidate

getClass:
 - jdk.internal.HotSpotIntrinsicCandidate

notify:
 - jdk.internal.HotSpotIntrinsicCandidate

notifyAll:
 - jdk.internal.HotSpotIntrinsicCandidate

Expected MCVE output:

// irrelevant method entries aren't shown

doSomething:
 - AnnotationTest.ExampleAnnotation

// irrelevant method entries aren't shown




Aucun commentaire:

Enregistrer un commentaire