码迷,mamicode.com
首页 > 其他好文 > 详细

通过反射,如何操作私有成员变量(取/赋值),如何调用私有方法?

时间:2016-07-20 23:00:36      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

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

工作中直到涉及到UT,才体会到它的重要性,现归纳整理一个小例子:

 

反射工具类:

 1 import java.lang.reflect.Field;
 2 import java.lang.reflect.InvocationTargetException;
 3 import java.lang.reflect.Method;
 4 
 5 public class ReflectionUtil {
 6 
 7     /***
 8      * 获取私有成员变量的值
 9      * 
10      */
11     public static Object getValue(Object instance, String fieldName)
12             throws IllegalAccessException, NoSuchFieldException {
13 
14         Field field = instance.getClass().getDeclaredField(fieldName);
15         field.setAccessible(true); // 参数值为true,禁止访问控制检查
16 
17         return field.get(instance);
18     }
19 
20     /***
21      * 设置私有成员变量的值
22      * 
23      */
24     public static void setValue(Object instance, String fileName, Object value)
25             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
26 
27         Field field = instance.getClass().getDeclaredField(fileName);
28         field.setAccessible(true);
29         field.set(instance, value);
30     }
31 
32     /***
33      * 访问私有方法
34      * 
35      */
36     public static Object callMethod(Object instance, String methodName, Class[] classes, Object[] objects)
37             throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
38             InvocationTargetException {
39 
40         Method method = instance.getClass().getDeclaredMethod(methodName, classes);
41         method.setAccessible(true);
42         return method.invoke(instance, objects);
43     }
44 }

 

 1 package com.test;
 2 
 3 public class Person {
 4 
 5     private String name;
 6     private int age;
 7 
 8     public Person(String name, int age) {
 9         this.name = name;
10         this.age = age;
11     }
12 
13     private String getInfo(String str, int num) {
14         return str + num + " apples";
15     }
16 
17 }

 

 1 import com.test.Person;
 2 
 3 public class ReflectTest {
 4 
 5     public static void main(String[] args) throws Exception {
 6 
 7         Person person = new Person("jack", 25);
 8 
 9         // test get private value
10         System.out.println("jack‘s name:" + ReflectionUtil.getValue(person, "name"));
11         System.out.println("jack‘s age:" + ReflectionUtil.getValue(person, "age"));
12 
13         // test set private value
14         ReflectionUtil.setValue(person, "name", "jason");
15         ReflectionUtil.setValue(person, "age", 10);
16         System.out.println("jack‘s name:" + ReflectionUtil.getValue(person, "name"));
17         System.out.println("jack‘s age:" + ReflectionUtil.getValue(person, "age"));
18 
19         // test call private method
20         String result = (String) ReflectionUtil.callMethod(person, "getInfo", new Class[] { String.class, int.class },
21                 new Object[] { "I hava ", 4 });
22         System.out.println("result: " + result);
23     }
24 }

 

结果:

jack‘s name:jack
jack‘s age:25
jack‘s name:jason
jack‘s age:10
result: I hava 4 apples

 

通过反射,如何操作私有成员变量(取/赋值),如何调用私有方法?

标签:

原文地址:http://www.cnblogs.com/jiangyi-uestc/p/5689771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!