blob: 6fb1e3f44687b339aa14121ba8c013052e2b992f (
plain)
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
|
package com.keuin.kbackupfabric.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @Author 落叶飞翔的蜗牛
* @Date 2018/3/10
* @Description 常用反射函数
*/
public final class ReflectionUtils {
/**
* Read private fields.
*/
public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(filedName);
field.setAccessible(true);
return field.get(instance);
}
/**
* Modify private fields.
*/
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);
}
/**
* Call private methods.
*/
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);
}
}
|