java基础总结(七十九)--通过Java反射的获取私有成员变量,调用私有方法

Java的反射工具很强大,有句著名的话:No reflection ,no frameworks.

纳整理一个小工具类:

/**
 * @Author 落叶飞翔的蜗牛
 * @Date 2018/3/10
 * @Description
 */
public class ReflectionUtils {
 
    /**
     * 获取私有成员变量的值
     * @param instance
     * @param filedName
     * @return
     */
    public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
        Field field = instance.getClass().getDeclaredField(filedName);
        field.setAccessible(true);
        return field.get(instance);
    }
 
    /**
     * 设置私有成员的值
     * @param instance
     * @param fieldName
     * @param value
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
        Field field = instance.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(instance, value);
    }
 
    /**
     * 访问私有方法
     * @param instance
     * @param methodName
     * @param classes
     * @param objects
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, String objects) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = instance.getClass().getDeclaredMethod(methodName, classes);
        method.setAccessible(true);
        return method.invoke(instance, objects);
    }
}

写个简单的单元测试如下:

/**
 * @Author 落叶飞翔的蜗牛
 * @Date 2018/3/10
 * @Description
 */
@RunWith(SpringRunner.class)
public class ReflectionUtilsTest {
 
    private String name;
 
    private void setName(String name) {
        this.name = name;
    }
 
    @Test
    public void test() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        ReflectionUtilsTest reflectionUtilsTest = new ReflectionUtilsTest();
        //访问私有属性
        System.out.println("name = " + ReflectionUtils.getPrivateField(reflectionUtilsTest, "name"));
        //设置私有属性
        ReflectionUtils.setPrivateField(reflectionUtilsTest, "name", "张三");
        System.out.println("name = " + ReflectionUtils.getPrivateField(reflectionUtilsTest, "name"));
        //调用私有方法
        ReflectionUtils.invokePrivateMethod(reflectionUtilsTest, "setName", new Class[]{String.class}, "李四");
        System.out.println("name = " + ReflectionUtils.getPrivateField(reflectionUtilsTest, "name"));
    }
}

输出结果如下:

name = null
name = 张三
name = 李四

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值