How to get annotations of a member variable?

前端 未结 8 2124
爱一瞬间的悲伤
爱一瞬间的悲伤 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:35

    Or you could try this

    try {
        BeanInfo bi = Introspector.getBeanInfo(User.getClass());
        PropertyDescriptor[] properties = bi.getPropertyDescriptors();
        for(PropertyDescriptor property : properties) {
            //One way
            for(Annotation annotation : property.getAnnotations()){
                if(annotation instanceof Column) {
                    String string = annotation.name();
                }
            }
            //Other way
            Annotation annotation = property.getAnnotation(Column.class);
            String string = annotation.name();
        }
    }catch (IntrospectonException ie) {
        ie.printStackTrace();
    }
    

    Hope this will help.

提交回复
热议问题