-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectUtil.java
More file actions
242 lines (225 loc) · 6.73 KB
/
Copy pathReflectUtil.java
File metadata and controls
242 lines (225 loc) · 6.73 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
package cn.xanderye.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author XanderYe
* @description:
* @date 2024/7/5 9:33
*/
public class ReflectUtil {
/**
* 获取静态变量值
* @param clazz
* @param fieldName
* @return
*/
public static Object getStaticField(Class<?> clazz, String fieldName) {
return getField(clazz, null, fieldName);
}
/**
* 获取对象属性值
* @param clazz
* @param obj
* @param fieldName
* @return
*/
public static Object getField(Class<?> clazz, Object obj, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
/**
* 设置静态变量值
* @param clazz
* @param fieldName
* @param value
*/
public static void setStaticField(Class<?> clazz, String fieldName, Object value) {
setField(clazz, null, fieldName, value);
}
/**
* 设置对象属性值
* @param clazz
* @param obj
* @param fieldName
* @param value
*/
public static void setField(Class<?> clazz, Object obj, String fieldName, Object value) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 创建一个无参数对象
* @param className
* @return
*/
public static Object createObject(String className) {
try {
Class<?> clazz = Class.forName(className);
return createObject(clazz);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 创建一个无参数对象
* @param clazz
* @return
*/
public static Object createObject(Class<?> clazz) {
Class<?>[] paramTypes = new Class[]{};
Object[] args = new Object[]{};
return createObject(clazz, paramTypes, args);
}
/**
* 创建一个单参数对象
* @param className
* @param paramType
* @param arg
* @return
*/
public static Object createObject(String className, Class<?> paramType, Object arg) {
try {
Class<?> clazz = Class.forName(className);
return createObject(clazz, paramType, arg);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 创建一个单参数对象
* @param clazz
* @param paramType
* @param arg
* @return
*/
public static Object createObject(Class<?> clazz, Class<?> paramType, Object arg) {
Class<?>[] paramTypes = new Class[]{paramType};
Object[] args = new Object[]{arg};
return createObject(clazz, paramTypes, args);
}
/**
* 创建一个多参数对象
* @param clazz
* @param paramTypes
* @param args
* @return
*/
public static Object createObject(Class<?> clazz, Class<?>[] paramTypes, Object[] args) {
try {
if (paramTypes == null) {
paramTypes = new Class[]{};
}
if (args == null) {
args = new Object[]{};
}
Constructor<?> constructor = clazz.getDeclaredConstructor(paramTypes);
constructor.setAccessible(true);
return constructor.newInstance(args);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 反射执行静态无参数方法
* @param clazz
* @param methodName
* @return
*/
public static Object invokeStaticMethod(Class<?> clazz, String methodName) {
Class<?>[] paramTypes = new Class[]{};
Object[] args = new Object[]{};
return invokeMethod(clazz, null, methodName, paramTypes, args);
}
/**
* 反射执行静态单参数方法
* @param clazz
* @param methodName
* @return
*/
public static Object invokeStaticMethod(Class<?> clazz, String methodName, Class<?> paramType, Object arg) {
if (null == paramType) {
return invokeStaticMethod(clazz, methodName);
}
Class<?>[] paramTypes = new Class[]{paramType};
Object[] args = new Object[]{arg};
return invokeMethod(clazz, null, methodName, paramTypes, args);
}
/**
* 反射执行静态方法
* @param clazz
* @param methodName
* @param paramTypes
* @param args
* @return
*/
public static Object invokeStaticMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes, Object[] args) {
return invokeMethod(clazz, null, methodName, paramTypes, args);
}
/**
* 反射执行对象无参数方法
* @param clazz
* @param obj
* @param methodName
* @return
*/
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName) {
Class<?>[] paramTypes = new Class[]{};
Object[] args = new Object[]{};
return invokeMethod(clazz, obj, methodName, paramTypes, args);
}
/**
* 反射执行对象单参数方法
* @param clazz
* @param methodName
* @return
*/
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Class<?> paramType, Object arg) {
if (null == paramType) {
return invokeMethod(clazz, obj, methodName);
}
Class<?>[] paramTypes = new Class[]{paramType};
Object[] args = new Object[]{arg};
return invokeMethod(clazz, obj, methodName, paramTypes, args);
}
/**
* 反射执行对象方法
* @param clazz
* @param obj
* @param methodName
* @param paramTypes
* @param args
* @return
*/
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Class<?>[] paramTypes, Object[] args) {
try {
if (paramTypes == null) {
paramTypes = new Class[]{};
}
if (args == null) {
args = new Object[]{};
}
Method method = clazz.getDeclaredMethod(methodName, paramTypes);
method.setAccessible(true);
return method.invoke(obj, args);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}