Code example for Field

Methods: getDeclaringClass, getModifiers, setAccessible

0
 
    /** 
     * 循环向上转型,获取对象的DeclaredField. 
     */ 
    protected static void makeAccessible(final Field field) {
        if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
            field.setAccessible(true);
        } 
    } 
 
    /** 
     * 循环向上转型,获取对象的DeclaredMethod. 
     */ 
    protected static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
        Assert.notNull(object, "object不能为空");
 
        for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
                .getSuperclass()) {
            try { 
                return superClass.getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {