How to get annotations of a member variable?

前端 未结 8 2130
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 00:29

I want to know a class\'s some member variable\'s annotations , I use BeanInfo beanInfo = Introspector.getBeanInfo(User.class) to introspect a class , and use <

8条回答
  •  时光说笑
    2020-11-29 00:53

    You can get annotations on the getter method:

    propertyDescriptor.getReadMethod().getDeclaredAnnotations();
    

    Getting the annotations of a private field seems like a bad idea... what if the property isn't even backed by a field, or is backed by a field with a different name? Even ignoring those cases, you're breaking abstraction by looking at private stuff.

提交回复
热议问题