java之反射工具类ReflectionUtil

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010675669/article/details/86625499

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionUtil {

    /***
     * 获取私有成员变量的值
     * 
     */
    public static Object getValue(Object instance, String fieldName)
            throws IllegalAccessException, NoSuchFieldException {

        Field field = instance.getClass().getDeclaredField(fieldName);
        field.setAccessible(true); // 参数值为true,禁止访问控制检查

        return field.get(instance);
    }

    /***
     * 设置私有成员变量的值
     * 
     */
    public static void setValue(Object instance, String fieldName, Object value)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

        Field field = instance.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(instance, value);
    }

    /***
     * 访问私有方法
     * 
     */
    public static Object callMethod(Object instance, String methodName, Class[] classes, Object[] objects)
            throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException {

        Method method = instance.getClass().getDeclaredMethod(methodName, classes);
        method.setAccessible(true);
        return method.invoke(instance, objects);
    }
}

猜你喜欢

转载自blog.csdn.net/u010675669/article/details/86625499