您的位置:首页 > 编程语言 > Java开发

反射访问私有成员和方法

2017-08-31 21:56 288 查看
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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java