通過反射,如何操作私有成員變量(取/賦值),如何調用私有方法?


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM