forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectUtils.java
More file actions
39 lines (31 loc) · 1.45 KB
/
ReflectUtils.java
File metadata and controls
39 lines (31 loc) · 1.45 KB
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
package org.tron.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.springframework.util.ReflectionUtils;
public class ReflectUtils {
public static Object getFieldObject(Object target, String fieldName) {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, target);
}
public static <T> T getFieldValue(Object target, String fieldName) {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
ReflectionUtils.makeAccessible(field);
return (T) ReflectionUtils.getField(field, target);
}
public static void setFieldValue(Object target, String fieldName, Object value) {
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, target, value);
}
public static <T> T invokeMethod(Object target, String methodName) {
Method method = ReflectionUtils.findMethod(target.getClass(), methodName);
ReflectionUtils.makeAccessible(method);
return (T) ReflectionUtils.invokeMethod(method, target);
}
public static void invokeMethod(Object target, String methodName, Class[] param, Object... args) {
Method method = ReflectionUtils.findMethod(target.getClass(), methodName, param);
ReflectionUtils.makeAccessible(method);
ReflectionUtils.invokeMethod(method, target, args);
}
}