Commit 104ddd44db51e300e5763037a05360fc406c2376
1 parent
b85e10e2fc
Exists in
master
and in
1 other branch
排除掉常量那些字段
Showing 1 changed file with 291 additions and 294 deletions
platform-common/src/main/java/com/lyms/platform/common/utils/ReflectionUtils.java
View file @
104ddd4
... | ... | @@ -3,11 +3,7 @@ |
3 | 3 | import java.beans.BeanInfo; |
4 | 4 | import java.beans.Introspector; |
5 | 5 | import java.beans.PropertyDescriptor; |
6 | -import java.lang.reflect.Field; | |
7 | -import java.lang.reflect.InvocationTargetException; | |
8 | -import java.lang.reflect.Method; | |
9 | -import java.lang.reflect.ParameterizedType; | |
10 | -import java.lang.reflect.Type; | |
6 | +import java.lang.reflect.*; | |
11 | 7 | import java.util.HashMap; |
12 | 8 | import java.util.Iterator; |
13 | 9 | import java.util.Map; |
14 | 10 | |
15 | 11 | |
16 | 12 | |
17 | 13 | |
18 | 14 | |
19 | 15 | |
20 | 16 | |
21 | 17 | |
22 | 18 | |
... | ... | @@ -22,56 +18,55 @@ |
22 | 18 | |
23 | 19 | /** |
24 | 20 | * 反射工具类. |
25 | - * | |
21 | + * <p/> | |
26 | 22 | * 提供访问私有变量,获取泛型类型Class, 提取集合中元素的属性, 转换字符串到对象等Util函数. |
27 | - * | |
28 | 23 | */ |
29 | 24 | public class ReflectionUtils { |
30 | 25 | |
31 | - private static Logger logger = LoggerFactory | |
32 | - .getLogger(ReflectionUtils.class); | |
26 | + private static Logger logger = LoggerFactory | |
27 | + .getLogger(ReflectionUtils.class); | |
33 | 28 | |
34 | - private static final ConcurrentHashMap<Class<?>, Class<?>> classInstance = new ConcurrentHashMap<Class<?>, Class<?>> (); | |
29 | + private static final ConcurrentHashMap<Class<?>, Class<?>> classInstance = new ConcurrentHashMap<Class<?>, Class<?>>(); | |
35 | 30 | |
36 | - /** | |
37 | - * 调用Getter方法. | |
38 | - */ | |
39 | - public static Object invokeGetterMethod(Object obj, String propertyName) { | |
40 | - String getterMethodName = "get" + StringUtils.capitalize(propertyName); | |
41 | - return invokeMethod(obj, getterMethodName, new Class[] {}, | |
42 | - new Object[] {}); | |
43 | - } | |
31 | + /** | |
32 | + * 调用Getter方法. | |
33 | + */ | |
34 | + public static Object invokeGetterMethod(Object obj, String propertyName) { | |
35 | + String getterMethodName = "get" + StringUtils.capitalize(propertyName); | |
36 | + return invokeMethod(obj, getterMethodName, new Class[]{}, | |
37 | + new Object[]{}); | |
38 | + } | |
44 | 39 | |
45 | - public static Map<String, Object> beanToMap(Object obj) { | |
46 | - if(obj == null){ | |
47 | - return null; | |
48 | - } | |
49 | - Map<String, Object> map = new HashMap<String, Object>(); | |
50 | - try { | |
51 | - BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); | |
52 | - PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); | |
53 | - for (PropertyDescriptor property : propertyDescriptors) { | |
54 | - String key = property.getName(); | |
55 | - // 过滤class属性 | |
56 | - if (!key.equals("class")) { | |
57 | - // 得到property对应的getter方法 | |
58 | - Method getter = property.getReadMethod(); | |
59 | - Object value = getter.invoke(obj); | |
40 | + public static Map<String, Object> beanToMap(Object obj) { | |
41 | + if (obj == null) { | |
42 | + return null; | |
43 | + } | |
44 | + Map<String, Object> map = new HashMap<String, Object>(); | |
45 | + try { | |
46 | + BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); | |
47 | + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); | |
48 | + for (PropertyDescriptor property : propertyDescriptors) { | |
49 | + String key = property.getName(); | |
50 | + // 过滤class属性 | |
51 | + if (!key.equals("class")) { | |
52 | + // 得到property对应的getter方法 | |
53 | + Method getter = property.getReadMethod(); | |
54 | + Object value = getter.invoke(obj); | |
60 | 55 | |
61 | - map.put(key, value); | |
62 | - } | |
63 | - } | |
64 | - } catch (Exception e) { | |
65 | - e.printStackTrace(); | |
66 | - } | |
67 | - return map; | |
56 | + map.put(key, value); | |
57 | + } | |
58 | + } | |
59 | + } catch (Exception e) { | |
60 | + e.printStackTrace(); | |
61 | + } | |
62 | + return map; | |
68 | 63 | |
69 | - } | |
64 | + } | |
70 | 65 | |
71 | - public static Map<String, String> beanToStrMap(Object obj) { | |
66 | + public static Map<String, String> beanToStrMap(Object obj) { | |
72 | 67 | Map<String, Object> map = beanToMap(obj); |
73 | 68 | Map<String, String> restMap = new HashMap<>(); |
74 | - if(MapUtils.isNotEmpty(map)) { | |
69 | + if (MapUtils.isNotEmpty(map)) { | |
75 | 70 | Set<Map.Entry<String, Object>> entries = map.entrySet(); |
76 | 71 | Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); |
77 | 72 | while (iterator.hasNext()) { |
78 | 73 | |
79 | 74 | |
80 | 75 | |
81 | 76 | |
82 | 77 | |
83 | 78 | |
84 | 79 | |
85 | 80 | |
86 | 81 | |
87 | 82 | |
88 | 83 | |
89 | 84 | |
90 | 85 | |
91 | 86 | |
92 | 87 | |
93 | 88 | |
94 | 89 | |
95 | 90 | |
96 | 91 | |
97 | 92 | |
98 | 93 | |
99 | 94 | |
100 | 95 | |
101 | 96 | |
102 | 97 | |
... | ... | @@ -81,250 +76,252 @@ |
81 | 76 | } |
82 | 77 | return restMap; |
83 | 78 | |
84 | - } | |
79 | + } | |
85 | 80 | |
86 | - /** | |
87 | - * 调用Setter方法.使用value的Class来查找Setter方法. | |
88 | - */ | |
89 | - public static void invokeSetterMethod(Object obj, String propertyName, | |
90 | - Object value) { | |
91 | - invokeSetterMethod(obj, propertyName, value, null); | |
92 | - } | |
81 | + /** | |
82 | + * 调用Setter方法.使用value的Class来查找Setter方法. | |
83 | + */ | |
84 | + public static void invokeSetterMethod(Object obj, String propertyName, | |
85 | + Object value) { | |
86 | + invokeSetterMethod(obj, propertyName, value, null); | |
87 | + } | |
93 | 88 | |
94 | - /** | |
95 | - * 调用Setter方法. | |
96 | - * | |
97 | - * @param propertyType | |
98 | - * 用于查找Setter方法,为空时使用value的Class替代. | |
99 | - */ | |
100 | - public static void invokeSetterMethod(Object obj, String propertyName, | |
101 | - Object value, Class<?> propertyType) { | |
102 | - Class<?> type = propertyType != null ? propertyType : value.getClass(); | |
103 | - String setterMethodName = "set" + StringUtils.capitalize(propertyName); | |
104 | - invokeMethod(obj, setterMethodName, new Class[] { type }, | |
105 | - new Object[] { value }); | |
106 | - } | |
89 | + /** | |
90 | + * 调用Setter方法. | |
91 | + * | |
92 | + * @param propertyType 用于查找Setter方法,为空时使用value的Class替代. | |
93 | + */ | |
94 | + public static void invokeSetterMethod(Object obj, String propertyName, | |
95 | + Object value, Class<?> propertyType) { | |
96 | + Class<?> type = propertyType != null ? propertyType : value.getClass(); | |
97 | + String setterMethodName = "set" + StringUtils.capitalize(propertyName); | |
98 | + invokeMethod(obj, setterMethodName, new Class[]{type}, | |
99 | + new Object[]{value}); | |
100 | + } | |
107 | 101 | |
108 | - /** | |
109 | - * 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数. | |
110 | - */ | |
111 | - public static Object getFieldValue(final Object obj, final String fieldName) { | |
112 | - Field field = getAccessibleField(obj, fieldName); | |
102 | + /** | |
103 | + * 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数. | |
104 | + */ | |
105 | + public static Object getFieldValue(final Object obj, final String fieldName) { | |
106 | + Field field = getAccessibleField(obj, fieldName); | |
113 | 107 | |
114 | - if (field == null) { | |
115 | - throw new IllegalArgumentException("Could not find field [" | |
116 | - + fieldName + "] on target [" + obj + "]"); | |
117 | - } | |
108 | + if (field == null) { | |
109 | + throw new IllegalArgumentException("Could not find field [" | |
110 | + + fieldName + "] on target [" + obj + "]"); | |
111 | + } | |
118 | 112 | |
119 | - Object result = null; | |
120 | - try { | |
121 | - result = field.get(obj); | |
122 | - } catch (IllegalAccessException e) { | |
123 | - logger.error("不可能抛出的异常{}", e.getMessage()); | |
124 | - } | |
125 | - return result; | |
126 | - } | |
113 | + Object result = null; | |
114 | + try { | |
115 | + result = field.get(obj); | |
116 | + } catch (IllegalAccessException e) { | |
117 | + logger.error("不可能抛出的异常{}", e.getMessage()); | |
118 | + } | |
119 | + return result; | |
120 | + } | |
127 | 121 | |
128 | - /** | |
129 | - * 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数. | |
130 | - */ | |
131 | - public static void setFieldValue(final Object obj, final String fieldName, | |
132 | - final Object value) { | |
133 | - Field field = getAccessibleField(obj, fieldName); | |
122 | + /** | |
123 | + * 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数. | |
124 | + */ | |
125 | + public static void setFieldValue(final Object obj, final String fieldName, | |
126 | + final Object value) { | |
127 | + Field field = getAccessibleField(obj, fieldName); | |
134 | 128 | |
135 | - if (field == null) { | |
136 | - throw new IllegalArgumentException("Could not find field [" | |
137 | - + fieldName + "] on target [" + obj + "]"); | |
138 | - } | |
129 | + if (field == null) { | |
130 | + throw new IllegalArgumentException("Could not find field [" | |
131 | + + fieldName + "] on target [" + obj + "]"); | |
132 | + } | |
139 | 133 | |
140 | - try { | |
141 | - field.set(obj, value); | |
142 | - } catch (IllegalAccessException e) { | |
143 | - logger.error("不可能抛出的异常:{}", e.getMessage()); | |
144 | - } | |
145 | - } | |
134 | + try { | |
135 | + field.set(obj, value); | |
136 | + } catch (IllegalAccessException e) { | |
137 | + logger.error("不可能抛出的异常:{}", e.getMessage()); | |
138 | + } | |
139 | + } | |
146 | 140 | |
147 | - /** | |
148 | - * 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问. | |
149 | - * | |
150 | - * 如向上转型到Object仍无法找到, 返回null. | |
151 | - */ | |
152 | - public static Field getAccessibleField(final Object obj, | |
153 | - final String fieldName) { | |
154 | - Assert.notNull(obj, "object不能为空"); | |
155 | - Assert.hasText(fieldName, "fieldName"); | |
156 | - for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass | |
157 | - .getSuperclass()) { | |
158 | - try { | |
159 | - Field field = superClass.getDeclaredField(fieldName); | |
160 | - field.setAccessible(true); | |
161 | - return field; | |
162 | - } catch (NoSuchFieldException e) {// NOSONAR | |
163 | - // Field不在当前类定义,继续向上转型 | |
164 | - } | |
165 | - } | |
166 | - return null; | |
167 | - } | |
141 | + /** | |
142 | + * 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问. | |
143 | + * <p/> | |
144 | + * 如向上转型到Object仍无法找到, 返回null. | |
145 | + */ | |
146 | + public static Field getAccessibleField(final Object obj, | |
147 | + final String fieldName) { | |
148 | + Assert.notNull(obj, "object不能为空"); | |
149 | + Assert.hasText(fieldName, "fieldName"); | |
150 | + for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass | |
151 | + .getSuperclass()) { | |
152 | + try { | |
153 | + Field field = superClass.getDeclaredField(fieldName); | |
154 | + field.setAccessible(true); | |
155 | + return field; | |
156 | + } catch (NoSuchFieldException e) {// NOSONAR | |
157 | + // Field不在当前类定义,继续向上转型 | |
158 | + } | |
159 | + } | |
160 | + return null; | |
161 | + } | |
168 | 162 | |
169 | - /** | |
170 | - * 直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况. | |
171 | - */ | |
172 | - public static Object invokeMethod(final Object obj, | |
173 | - final String methodName, final Class<?>[] parameterTypes, | |
174 | - final Object[] args) { | |
175 | - Method method = getAccessibleMethod(obj, methodName, parameterTypes); | |
176 | - if (method == null) { | |
177 | - throw new IllegalArgumentException("Could not find method [" | |
178 | - + methodName + "] on target [" + obj + "]"); | |
179 | - } | |
163 | + /** | |
164 | + * 直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况. | |
165 | + */ | |
166 | + public static Object invokeMethod(final Object obj, | |
167 | + final String methodName, final Class<?>[] parameterTypes, | |
168 | + final Object[] args) { | |
169 | + Method method = getAccessibleMethod(obj, methodName, parameterTypes); | |
170 | + if (method == null) { | |
171 | + throw new IllegalArgumentException("Could not find method [" | |
172 | + + methodName + "] on target [" + obj + "]"); | |
173 | + } | |
180 | 174 | |
181 | - try { | |
182 | - return method.invoke(obj, args); | |
183 | - } catch (Exception e) { | |
184 | - throw convertReflectionExceptionToUnchecked(e); | |
185 | - } | |
186 | - } | |
175 | + try { | |
176 | + return method.invoke(obj, args); | |
177 | + } catch (Exception e) { | |
178 | + throw convertReflectionExceptionToUnchecked(e); | |
179 | + } | |
180 | + } | |
187 | 181 | |
188 | - /** | |
189 | - * 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. | |
190 | - * | |
191 | - * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... | |
192 | - * args) | |
193 | - */ | |
194 | - public static Method getAccessibleMethod(final Object obj, | |
195 | - final String methodName, final Class<?>... parameterTypes) { | |
196 | - Assert.notNull(obj, "object不能为空"); | |
182 | + /** | |
183 | + * 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. | |
184 | + * <p/> | |
185 | + * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... | |
186 | + * args) | |
187 | + */ | |
188 | + public static Method getAccessibleMethod(final Object obj, | |
189 | + final String methodName, final Class<?>... parameterTypes) { | |
190 | + Assert.notNull(obj, "object不能为空"); | |
197 | 191 | |
198 | - for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass | |
199 | - .getSuperclass()) { | |
200 | - try { | |
201 | - Method method = superClass.getDeclaredMethod(methodName, | |
202 | - parameterTypes); | |
192 | + for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass | |
193 | + .getSuperclass()) { | |
194 | + try { | |
195 | + Method method = superClass.getDeclaredMethod(methodName, | |
196 | + parameterTypes); | |
203 | 197 | |
204 | - method.setAccessible(true); | |
198 | + method.setAccessible(true); | |
205 | 199 | |
206 | - return method; | |
200 | + return method; | |
207 | 201 | |
208 | - } catch (NoSuchMethodException e) {// NOSONAR | |
209 | - // Method不在当前类定义,继续向上转型 | |
210 | - } | |
211 | - } | |
212 | - return null; | |
213 | - } | |
202 | + } catch (NoSuchMethodException e) {// NOSONAR | |
203 | + // Method不在当前类定义,继续向上转型 | |
204 | + } | |
205 | + } | |
206 | + return null; | |
207 | + } | |
214 | 208 | |
215 | - /** | |
216 | - * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class. eg. public UserDao | |
217 | - * extends HibernateDao<User> | |
218 | - * | |
219 | - * @param clazz | |
220 | - * The class to introspect | |
221 | - * @return the first generic declaration, or Object.class if cannot be | |
222 | - * determined | |
223 | - */ | |
224 | - @SuppressWarnings({ "unchecked", "rawtypes" }) | |
225 | - public static <T> Class<T> getSuperClassGenricType(final Class clazz) { | |
226 | - return getSuperClassGenricType(clazz, 0); | |
227 | - } | |
209 | + /** | |
210 | + * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class. eg. public UserDao | |
211 | + * extends HibernateDao<User> | |
212 | + * | |
213 | + * @param clazz The class to introspect | |
214 | + * @return the first generic declaration, or Object.class if cannot be | |
215 | + * determined | |
216 | + */ | |
217 | + @SuppressWarnings({"unchecked", "rawtypes"}) | |
218 | + public static <T> Class<T> getSuperClassGenricType(final Class clazz) { | |
219 | + return getSuperClassGenricType(clazz, 0); | |
220 | + } | |
228 | 221 | |
229 | - /** | |
230 | - * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class. | |
231 | - * | |
232 | - * 如public UserDao extends HibernateDao<User,Long> | |
233 | - * | |
234 | - * @param clazz | |
235 | - * clazz The class to introspect | |
236 | - * @param index | |
237 | - * the Index of the generic ddeclaration,start from 0. | |
238 | - * @return the index generic declaration, or Object.class if cannot be | |
239 | - * determined | |
240 | - */ | |
241 | - @SuppressWarnings("rawtypes") | |
242 | - public static Class getSuperClassGenricType(final Class clazz, | |
243 | - final int index) { | |
222 | + /** | |
223 | + * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class. | |
224 | + * <p/> | |
225 | + * 如public UserDao extends HibernateDao<User,Long> | |
226 | + * | |
227 | + * @param clazz clazz The class to introspect | |
228 | + * @param index the Index of the generic ddeclaration,start from 0. | |
229 | + * @return the index generic declaration, or Object.class if cannot be | |
230 | + * determined | |
231 | + */ | |
232 | + @SuppressWarnings("rawtypes") | |
233 | + public static Class getSuperClassGenricType(final Class clazz, | |
234 | + final int index) { | |
244 | 235 | |
245 | - if (classInstance.containsKey(clazz)) { | |
246 | - return classInstance.get(clazz); | |
247 | - } | |
236 | + if (classInstance.containsKey(clazz)) { | |
237 | + return classInstance.get(clazz); | |
238 | + } | |
248 | 239 | |
249 | - Type genType = clazz.getGenericSuperclass(); | |
240 | + Type genType = clazz.getGenericSuperclass(); | |
250 | 241 | |
251 | - if (!(genType instanceof ParameterizedType)) { | |
252 | - logger.warn(clazz.getSimpleName() | |
253 | - + "'s superclass not ParameterizedType"); | |
254 | - return Object.class; | |
255 | - } | |
242 | + if (!(genType instanceof ParameterizedType)) { | |
243 | + logger.warn(clazz.getSimpleName() | |
244 | + + "'s superclass not ParameterizedType"); | |
245 | + return Object.class; | |
246 | + } | |
256 | 247 | |
257 | - Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); | |
248 | + Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); | |
258 | 249 | |
259 | - if (index >= params.length || index < 0) { | |
260 | - logger.warn("Index: " + index + ", Size of " | |
261 | - + clazz.getSimpleName() + "'s Parameterized Type: " | |
262 | - + params.length); | |
263 | - return Object.class; | |
264 | - } | |
265 | - if (!(params[index] instanceof Class)) { | |
266 | - logger.warn(clazz.getSimpleName() | |
267 | - + " not set the actual class on superclass generic parameter"); | |
268 | - return Object.class; | |
269 | - } | |
270 | - Class classGenricType = (Class) params[index]; | |
271 | - classInstance.put(clazz, classGenricType); | |
272 | - return classGenricType; | |
273 | - } | |
250 | + if (index >= params.length || index < 0) { | |
251 | + logger.warn("Index: " + index + ", Size of " | |
252 | + + clazz.getSimpleName() + "'s Parameterized Type: " | |
253 | + + params.length); | |
254 | + return Object.class; | |
255 | + } | |
256 | + if (!(params[index] instanceof Class)) { | |
257 | + logger.warn(clazz.getSimpleName() | |
258 | + + " not set the actual class on superclass generic parameter"); | |
259 | + return Object.class; | |
260 | + } | |
261 | + Class classGenricType = (Class) params[index]; | |
262 | + classInstance.put(clazz, classGenricType); | |
263 | + return classGenricType; | |
264 | + } | |
274 | 265 | |
275 | - /** | |
276 | - * 将反射时的checked exception转换为unchecked exception. | |
277 | - */ | |
278 | - public static RuntimeException convertReflectionExceptionToUnchecked( | |
279 | - Exception e) { | |
280 | - if (e instanceof IllegalAccessException | |
281 | - || e instanceof IllegalArgumentException | |
282 | - || e instanceof NoSuchMethodException) { | |
283 | - return new IllegalArgumentException("Reflection Exception.", e); | |
284 | - } else if (e instanceof InvocationTargetException) { | |
285 | - return new RuntimeException("Reflection Exception.", | |
286 | - ((InvocationTargetException) e).getTargetException()); | |
287 | - } else if (e instanceof RuntimeException) { | |
288 | - return (RuntimeException) e; | |
289 | - } | |
290 | - return new RuntimeException("Unexpected Checked Exception.", e); | |
291 | - } | |
292 | - | |
293 | - /** | |
294 | - * | |
295 | - * 传入一个对象获取到这个对象需要修改的字段值 | |
296 | - * | |
297 | - * @param object | |
298 | - * @return | |
299 | - */ | |
300 | - public static Map<String,Object> getUpdateField(Object object){ | |
301 | - Map<String,Object> resultMap = new HashMap<String,Object>(); | |
302 | - if(null==object){ | |
303 | - return resultMap; | |
304 | - } | |
305 | - Field [] fields1 = object.getClass().getDeclaredFields(); | |
306 | - Field [] fields2 = object.getClass().getSuperclass().getDeclaredFields(); | |
307 | - //子对象和父对象的字段 | |
308 | - Field [] fields = ArrayUtils.merge(fields1,fields2); | |
266 | + /** | |
267 | + * 将反射时的checked exception转换为unchecked exception. | |
268 | + */ | |
269 | + public static RuntimeException convertReflectionExceptionToUnchecked( | |
270 | + Exception e) { | |
271 | + if (e instanceof IllegalAccessException | |
272 | + || e instanceof IllegalArgumentException | |
273 | + || e instanceof NoSuchMethodException) { | |
274 | + return new IllegalArgumentException("Reflection Exception.", e); | |
275 | + } else if (e instanceof InvocationTargetException) { | |
276 | + return new RuntimeException("Reflection Exception.", | |
277 | + ((InvocationTargetException) e).getTargetException()); | |
278 | + } else if (e instanceof RuntimeException) { | |
279 | + return (RuntimeException) e; | |
280 | + } | |
281 | + return new RuntimeException("Unexpected Checked Exception.", e); | |
282 | + } | |
309 | 283 | |
310 | - Object result = null; | |
311 | - String name = null; | |
312 | - for(Field field:fields){ | |
313 | - field.setAccessible(true); | |
314 | - name= field.getName(); | |
315 | - //过滤掉序列化的字段 | |
316 | - if("serialVersionUID".equals(name)){ | |
317 | - continue; | |
318 | - } | |
319 | - try { | |
320 | - result = field.get(object); | |
321 | - } catch (Exception e) { | |
322 | - } | |
323 | - if(null!=result){ | |
324 | - resultMap.put(name, result); | |
325 | - } | |
326 | - } | |
327 | - return resultMap; | |
328 | - } | |
284 | + /** | |
285 | + * 传入一个对象获取到这个对象需要修改的字段值 | |
286 | + * | |
287 | + * @param object | |
288 | + * @return | |
289 | + */ | |
290 | + public static Map<String, Object> getUpdateField(Object object) { | |
291 | + Map<String, Object> resultMap = new HashMap<String, Object>(); | |
292 | + if (null == object) { | |
293 | + return resultMap; | |
294 | + } | |
295 | + Field[] fields1 = object.getClass().getDeclaredFields(); | |
296 | + Field[] fields2 = object.getClass().getSuperclass().getDeclaredFields(); | |
297 | + //子对象和父对象的字段 | |
298 | + Field[] fields = ArrayUtils.merge(fields1, fields2); | |
299 | + | |
300 | + Object result = null; | |
301 | + String name = null; | |
302 | + for (Field field : fields) { | |
303 | +/** | |
304 | + * | |
305 | + native transient volatile synchronized final static protected private public | |
306 | + | |
307 | + 0 0 0 0 0 1 0 0 1 | |
308 | + */ | |
309 | + ; | |
310 | + //排除static /final的字段 | |
311 | + if (64>field.getModifiers()&&field.getModifiers() >= 12) { | |
312 | + continue; | |
313 | + } | |
314 | + field.setAccessible(true); | |
315 | + name = field.getName(); | |
316 | + try { | |
317 | + result = field.get(object); | |
318 | + } catch (Exception e) { | |
319 | + } | |
320 | + if (null != result) { | |
321 | + resultMap.put(name, result); | |
322 | + } | |
323 | + } | |
324 | + return resultMap; | |
325 | + } | |
329 | 326 | } |