Get annotations for enum type variable

前端 未结 5 980
孤城傲影
孤城傲影 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:49

    Try this (java reflection):

    String field = En.AAA.name();
    En.class.getField(field).getAnnotations();
    

    It should get you the annotations from AAA.

    EDIT:

    As the author supposed:

    en1.getClass().getField(((Enum)en1).name()).getAnnotations(); 
    

    Works for him :)

提交回复
热议问题