ReflectUtils.java
3.36 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.diligrp.assistant.shared.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* 利用反射进行操作的一个工具类
*/
public class ReflectUtils {
/**
* 利用反射获取指定对象的指定属性
*/
public static Object getFieldValue(Object target, String fieldName) {
Object result = null;
Field field = ReflectUtils.getField(target, fieldName);
if (field != null) {
field.setAccessible(true);
try {
result = field.get(target);
} catch (IllegalArgumentException | IllegalAccessException ex) {
throw new RuntimeException("Illegal access or argument exception", ex);
}
}
return result;
}
/**
* 利用反射获取指定对象里面的指定属性
*/
private static Field getField(Object obj, String fieldName) {
Field field = null;
for (Class<?> clazz = obj.getClass();
clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName);
break;
} catch (NoSuchFieldException e) {
// 当前类没有此方法则向父类查找,都没有就返回NULL
}
}
return field;
}
/**
* 利用反射设置指定对象的指定属性为指定的值
*/
public static void setFieldValue(Object obj, String fieldName, String fieldValue) {
Field field = ReflectUtils.getField(obj, fieldName);
if (field != null) {
try {
field.setAccessible(true);
field.set(obj, fieldValue);
} catch (IllegalArgumentException | IllegalAccessException ex) {
throw new RuntimeException("Illegal access or argument exception", ex);
}
}
}
/**
* 调用对象方法, 包含private/protected修饰的方法.
*/
public static Object invokeMethod(final Object target, final String methodName, final Class<?>[] parameterTypes,
final Object[] parameters) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method method = getDeclaredMethod(target, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method ["
+ methodName + "] parameterType " + Arrays.toString(parameterTypes)
+ " on target [" + target + "]");
}
method.setAccessible(true);
return method.invoke(target, parameters);
}
/**
* 在target对象上查找方法,如果当前类定义未定义则向父类查找,都未查找到则返回Null.
*/
protected static Method getDeclaredMethod(Object target, String methodName, Class<?>[] parameterTypes) {
AssertUtils.notNull(target, "target must be not null");
for (Class<?> superClass = target.getClass(); superClass != Object.class;
superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException sme) {
// 当前类未定义Method则向父类查找
}
}
return null;
}
}