1 package com.foxinmy.weixin4j.util;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Modifier;
7 import java.lang.reflect.ParameterizedType;
8 import java.lang.reflect.Type;
9 import java.util.HashSet;
10 import java.util.Set;
11
12
13
14
15
16
17 public class ReflectionUtil {
18
19
20
21
22
23
24
25 public static String getPackageName(Object obj) {
26 return obj.getClass().getPackage().getName();
27 }
28
29
30
31
32
33
34
35
36 public static Class<?> getFieldGenericType(Object obj, String fieldName) {
37 Field field = getAccessibleField(obj, fieldName);
38 Type type = field.getGenericType();
39 if (type instanceof ParameterizedType) {
40 return (Class<?>) ((ParameterizedType) type)
41 .getActualTypeArguments()[0];
42 }
43 return null;
44 }
45
46
47
48
49
50
51
52
53
54
55 public static Object invokeMethod(Object object, String propertyName) {
56 try {
57 Method getterMethod = object.getClass().getMethod(propertyName);
58 return getterMethod.invoke(object);
59 } catch (Exception e) {
60 e.printStackTrace();
61 return null;
62 }
63 }
64
65 public static Object invokeMethod(Object object, String propertyName,
66 Object... args) {
67 try {
68 Method getterMethod = object.getClass().getMethod(propertyName);
69 return getterMethod.invoke(object, args);
70 } catch (Exception e) {
71 e.printStackTrace();
72 return null;
73 }
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static Object invokeGetterMethod(Object object, String propertyName)
91 throws Exception {
92 String getterMethodName = null;
93 Method getterMethod = null;
94 String propertyNa = null;
95 if (propertyName.contains(".")) {
96 propertyNa = StringUtil.substringBefore(propertyName, ".");
97 getterMethodName = "get" + StringUtil.capitalize(propertyNa);
98 getterMethod = object.getClass().getMethod(getterMethodName);
99 return invokeGetterMethod(getterMethod.invoke(object),
100 StringUtil.substringAfter(propertyName, "."));
101 } else {
102 getterMethodName = "get" + StringUtil.capitalize(propertyName);
103 getterMethod = object.getClass().getMethod(getterMethodName);
104 return getterMethod.invoke(object);
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public static void invokeSetterMethod(Object object, String propertyName,
121 Object propertyValue) {
122 Class<?> setterMethodClass = propertyValue.getClass();
123 invokeSetterMethod(object, propertyName, propertyValue,
124 setterMethodClass);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public static void invokeSetterMethod(Object object, String propertyName,
143 Object propertyValue, Class<?> setterMethodClass) {
144 String setterMethodName = "set" + StringUtil.capitalize(propertyName);
145 try {
146 Method setterMethod = object.getClass().getMethod(setterMethodName,
147 setterMethodClass);
148 setterMethod.invoke(object, propertyValue);
149 } catch (Exception e) {
150 e.printStackTrace();
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163 public static Object getFieldValue(Object object, String fieldName) {
164 Field field = getAccessibleField(object, fieldName);
165 if (field == null) {
166 throw new IllegalArgumentException("Could not find field "
167 + fieldName);
168 }
169 Object result = null;
170 try {
171 result = field.get(object);
172 } catch (IllegalAccessException e) {
173
174 }
175 return result;
176 }
177
178
179
180
181
182
183
184
185
186
187 public static void setFieldValue(Object object, String fieldName,
188 Object value) {
189 Field field = getAccessibleField(object, fieldName);
190 if (field == null) {
191 throw new IllegalArgumentException("Could not find field "
192 + fieldName);
193 }
194 try {
195 field.set(object, value);
196 } catch (IllegalAccessException e) {
197
198 }
199 }
200
201
202 public static String getFieldType(Object object, String fieldName) {
203 Field field = getAccessibleField(object, fieldName);
204 return field.getType().getSimpleName();
205 }
206
207 public static Field getAccessibleField(final Object object,
208 final String fieldName) {
209 for (Class<?> superClass = object.getClass(); superClass != Object.class;) {
210 try {
211 Field field = superClass.getDeclaredField(fieldName);
212 field.setAccessible(true);
213 return field;
214 } catch (NoSuchFieldException e) {
215 return null;
216 }
217 }
218 return null;
219 }
220
221 public static Set<Field> getAllField(Class<?> clazz) {
222 Set<Field> fieldSet = new HashSet<Field>();
223 while (clazz != Object.class) {
224 Field[] fields = clazz.getDeclaredFields();
225 for (Field field : fields) {
226 int modifier = field.getModifiers();
227 if (Modifier.isFinal(modifier) || Modifier.isStatic(modifier)) {
228 continue;
229 }
230 fieldSet.add(field);
231 }
232 clazz = clazz.getSuperclass();
233 }
234 return fieldSet;
235 }
236 }