java通过Annotation对象获取注解属性的值

来源:互联网 发布:mac装什么杀毒软件 编辑:程序博客网 时间:2024/06/16 09:31
//获取该注解对象的属性值public static Object getAnnotationValue(Annotation annotation, String property) {        Object result = null;        if (annotation != null) {            InvocationHandler invo = Proxy.getInvocationHandler(annotation); //获取被代理的对象            Map map = (Map) getFieldValue(invo, "memberValues");            if (map != null) {                result = map.get(property);            }        }        return result;    }public static <T> Object getFieldValue(T object, String property) {        if (object != null && property != null) {            Class<T> currClass = (Class<T>) object.getClass();        try {                Field field = currClass.getDeclaredField(property);                field.setAccessible(true);                 return field.get(object);            } catch (NoSuchFieldException e) {                throw new IllegalArgumentException(currClass + " has no property: " + property);            } catch (IllegalArgumentException e) {                throw e;            } catch (Exception e) {                e.printStackTrace();            }        }        return null;    }
原创粉丝点击