Get annotations for enum type variable

前端 未结 5 971
孤城傲影
孤城傲影 2020-12-29 02:34

I have some nonnull variable (e.g. en1) of Enum type. The question is: how to get annotations related to enumeration constant referenced by e

5条回答
  •  情书的邮戳
    2020-12-29 02:46

    I just read from your comment that you already found the answer. I just wanted to remark for other people interested that, in order for that to work, those annotations must have been declared with the correct retention policy, like this:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Anno1 {
       // ...
    }
    

    Without this, they will not be accessible at runtime.

    Further reading:

    • JavaDoc: RetentionPolicy
    • Wikipedia: Java annotation

提交回复
热议问题