如何通过反射获取和设置对象私有字段的值?

来源:互联网 发布:muji值得买的东西 知乎 编辑:程序博客网 时间:2024/05/19 22:23

可以通过类对象的getDeclaredField()方法字段(Field)对象,然后再通过字段对象的setAccessible(true)将其设置为可以访问,接下来就可以通过get/set方法来获取/设置字段的值了。下面的代码实现了一个反射的工具类,其中的两个静态方法分别用于获取和设置私有字段的值,字段可以是基本类型也可以是对象类型且支持多级对象操作,例如ReflectionUtil.get(dog, “owner.car.engine.id”);可以获得dog对象的主人的汽车的引擎的ID号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;


 


/**


 *
 反射工具类


 */


public class ReflectionUtil {


 


    private ReflectionUtil() {
    throw new AssertionError();


    }


 


    /**


     *
 通过反射取对象指定字段(属性)的值


     *
 @param target 目标对象


     *
 @param fieldName 字段的名字


     *
 @throws 如果取不到对象指定字段的值则抛出异常


     *
 @return 字段的值


     */


    public static Object getValue(Object target, String fieldName) {
    Class<?> clazz = target.getClass();
    String[] fs = fieldName.split("\\.");
    try{


            for(int i = 0;i < fs.length - 1;i++) {
                Field f = clazz.getDeclaredField(fs[i]);
                f.setAccessible(true);
                target= f.get(target);
                clazz = target.getClass();
                }
            Field f = clazz.getDeclaredField(fs[fs.length - 1]);
            f.setAccessible(true);
            return f.get(target);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }
    }


 


    /**


     *
 通过反射给对象的指定字段赋值


     *
 @param target 目标对象


     *
 @param fieldName 字段的名称


     *
 @param value 值


     */


    public static void setValue(Object target, String fieldName, Object value) {
        Class<?> clazz = target.getClass();
        String[] fs = fieldName.split("\\.");
        try{
            for(int i = 0; i < fs.length - 1; i++) {
                Field f = clazz.getDeclaredField(fs[i]);
                f.setAccessible(true);
                Object val = f.get(target);
                if(val == null){
                    Constructor<?> c = f.getType().getDeclaredConstructor();
                    c.setAccessible(true);
                    val = c.newInstance();
                    f.set(target, val);
                }
                target = val;
                clazz = target.getClass();
            }
            Field f = clazz.getDeclaredField(fs[fs.length - 1]);
            f.setAccessible(true);
            f.set(target, value);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }
    }


 


}
阅读全文
0 0
原创粉丝点击