Java: cannot access annotations through reflection

前端 未结 1 1621
暖寄归人
暖寄归人 2020-12-06 09:41

Here is a test class:

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

public class TestAnnotations {

    @interface Annotate{}

           


        
1条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 10:03

    In order to access an annotation at runtime, it needs to have a Retention policy of Runtime.

    @Retention(RetentionPolicy.RUNTIME) @interface Annotate {}
    

    Otherwise, the annotations are dropped and the JVM is not aware of them.
    For more information, see here.

    0 讨论(0)
提交回复
热议问题