ReflectionUtils.java
8.33 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.bs.common.utils;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* @author meilin.huang
* @version 1.0
* @date 2019-03-27 10:04 AM
*/
public final class ReflectionUtils {
/**
* 获取所有field字段,包含父类继承的
*
* @param clazz 字段所属类型
* @return
*/
public static Field[] getFields(Class<?> clazz) {
return getFields(clazz, null);
}
/**
* 获取指定类的所有的field,包括父类
*
* @param clazz 字段所属类型
* @param fieldFilter 字段过滤器
* @return 符合过滤器条件的字段数组
*/
public static Field[] getFields(Class<?> clazz, Predicate<Field> fieldFilter) {
List<Field> fields = new ArrayList<>(32);
while (Object.class != clazz && clazz != null) {
// 获得该类所有声明的字段,即包括public、private和protected,但是不包括父类的申明字段,
// getFields:获得某个类的所有的公共(public)的字段,包括父类中的字段
for (Field field : clazz.getDeclaredFields()) {
if (fieldFilter != null && !fieldFilter.test(field)) {
continue;
}
fields.add(field);
}
clazz = clazz.getSuperclass();
}
return fields.toArray(new Field[0]);
}
/**
* 对指定类的所有字段执行consumer操作
*
* @param clazz 目标对象
* @param consumer 对字段进行操作
*/
public static void doWithFields(Class<?> clazz, Consumer<Field> consumer) {
Arrays.stream(getFields(clazz)).forEach(consumer);
}
/**
* 获取指定类的指定field,包括父类
*
* @param clazz 字段所属类型
* @param name 字段名
* @return
*/
public static Field getField(Class<?> clazz, String name) {
return getField(clazz, name, null);
}
/**
* 获取指定类的指定field,包括父类
*
* @param clazz 字段所属类型
* @param name 字段名
* @param type field类型
* @return Field对象
*/
public static Field getField(Class<?> clazz, String name, Class<?> type) {
Assert.notNull(clazz, "clazz不能为空!");
while (clazz != Object.class && clazz != null) {
for (Field field : clazz.getDeclaredFields()) {
if ((name == null || name.equals(field.getName())) &&
(type == null || type.equals(field.getType()))) {
return field;
}
}
clazz = clazz.getSuperclass();
}
return null;
}
/**
* 获取字段值
*
* @param field 字段
* @param target 字段所属实例对象
* @return 字段值
*/
public static Object getFieldValue(Field field, Object target) {
makeAccessible(field);
try {
return field.get(target);
} catch (Exception e) {
throw new IllegalStateException(String.format("获取%s对象的%s字段值错误!"
, target.getClass().getName(), field.getName()), e);
}
}
/**
* 获取对象中指定field值
*
* @param obj 对象
* @param fieldName 字段名
* @return 字段值
*/
public static Object getFieldValue(Object obj, String fieldName) {
Assert.notNull(obj, "obj不能为空!");
if (ObjectUtils.isWrapperOrPrimitive(obj)) {
return obj;
}
return getFieldValue(getField(obj.getClass(), fieldName), obj);
}
/**
* 获取指定对象中指定字段路径的值(类似js访问对象属性) <br/>
* 如:Product p = new Product(new User()) <br/>
* 可使用ReflectionUtils.getValueByFieldPath(p, "user.name")获取到用户的name属性
*
* @param obj 取值对象
* @param fieldPath 字段路径(形如 user.name)
* @return 字段value
*/
public static Object getValueByFieldPath(Object obj, String fieldPath) {
String[] fieldNames = fieldPath.split("\\.");
Object result = null;
for (String fieldName : fieldNames) {
result = getFieldValue(obj, fieldName);
if (result == null) {
return null;
}
obj = result;
}
return result;
}
/**
* 设置字段值
*
* @param field 字段
* @param target 字段所属对象实例
* @param value 需要设置的值
*/
public static void setFieldValue(Field field, Object target, Object value) {
makeAccessible(field);
try {
field.set(target, value);
} catch (Exception e) {
throw new IllegalStateException(String.format("设置%s对象的%s字段值错误!"
, target.getClass().getName(), field.getName()), e);
}
}
/**
* 设置字段为可见
*
* @param field
*/
public static void makeAccessible(Field field) {
if ((!Modifier.isPublic(field.getModifiers()) ||
!Modifier.isPublic(field.getDeclaringClass().getModifiers()) ||
Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
field.setAccessible(true);
}
}
/**
* 调用无参数方法
*
* @param method 方法对象
* @param target 调用对象
* @return 执行结果
*/
public static Object invokeMethod(Method method, Object target) {
return invokeMethod(method, target, new Object[0]);
}
/**
* 调用指定对象的方法
*
* @param method 方法对象
* @param target 调用对象
* @param args 方法参数
* @return 执行结果
*/
public static Object invokeMethod(Method method, Object target, Object... args) {
try {
makeAccessible(method);
return method.invoke(target, args);
} catch (Exception ex) {
throw new IllegalStateException(String.format("执行%s.%s()方法错误!"
, target.getClass().getName(), method.getName()), ex);
}
}
/**
* 设置方法可见性
*
* @param method 方法
*/
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) ||
!Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
method.setAccessible(true);
}
}
/**
* 是否为equals方法
*
* @see Object#equals(Object)
*/
public static boolean isEqualsMethod(Method method) {
if (!"equals".equals(method.getName())) {
return false;
}
Class<?>[] paramTypes = method.getParameterTypes();
return (paramTypes.length == 1 && paramTypes[0] == Object.class);
}
/**
* 是否为hashCode方法
*
* @see Object#hashCode()
*/
public static boolean isHashCodeMethod(Method method) {
return "hashCode".equals(method.getName()) && method.getParameterCount() == 0;
}
/**
* 是否为Object的toString方法
*
* @see Object#toString()
*/
public static boolean isToStringMethod(Method method) {
return "toString".equals(method.getName()) && method.getParameterCount() == 0;
}
public static Method getMethodByName(Class<?> clazz, String name){
List<Field> fields = new ArrayList<>(32);
while (Object.class != clazz && clazz != null) {
// 获得该类所有声明的字段,即包括public、private和protected,但是不包括父类的申明字段,
// getFields:获得某个类的所有的公共(public)的字段,包括父类中的字段
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (StrUtil.isNotBlank(name) && method.getName().equals(name)) {
return method;
}
}
clazz = clazz.getSuperclass();
}
return null;
}
}