How to get annotations of a member variable?

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

    You have to use reflection to get all the member fields of User class, iterate through them and find their annotations

    something like this:

    public void getAnnotations(Class clazz){
        for(Field field : clazz.getDeclaredFields()){
            Class type = field.getType();
            String name = field.getName();
            field.getDeclaredAnnotations(); //do something to these
        }
    }
    

提交回复
热议问题