1
0

MapBeanUtils.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.kmall.admin.cuspay.util;
  2. import org.apache.commons.beanutils.BeanUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.beans.BeanInfo;
  6. import java.beans.IntrospectionException;
  7. import java.beans.Introspector;
  8. import java.beans.PropertyDescriptor;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Method;
  12. import java.lang.reflect.Modifier;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * map bean 相互转换
  17. *
  18. * @author Scott Chen
  19. * @version 1.0
  20. * 2017-09-19 16:58
  21. */
  22. public class MapBeanUtils {
  23. private static final Logger logger = LoggerFactory.getLogger(MapBeanUtils.class);
  24. /**
  25. * map 转 bean by BeanUtils
  26. * @param map
  27. * @param beanClass
  28. * @param <T>
  29. * @return
  30. * @throws Exception
  31. */
  32. public static <T> T toObject(Map<String, Object> map, Class<T> beanClass) {
  33. T bean;
  34. if (map == null) {
  35. return null;
  36. }
  37. try {
  38. bean = beanClass.newInstance();
  39. BeanUtils.populate(bean, map);
  40. } catch (Exception e) {
  41. logger.error("map转bean异常:{}", e.getCause());
  42. return null;
  43. }
  44. return bean;
  45. }
  46. /**
  47. * bean 转 map by Introspector
  48. *
  49. * @param bean
  50. * @return
  51. */
  52. public static <T> Map<String, Object> fromObject(T bean) {
  53. if (bean == null) {
  54. return null;
  55. }
  56. return fromObjectByIntrospector(bean);
  57. }
  58. //---------- 使用Introspector进行转换 ----------
  59. /**
  60. * map 转 bean by Introspector
  61. * @param map
  62. * @param beanClass
  63. * @param <T>
  64. * @return
  65. */
  66. public static <T> T toObjectByIntrospector(Map<String, Object> map, Class<T> beanClass) {
  67. if (map == null) {
  68. return null;
  69. }
  70. T obj = null;
  71. try {
  72. obj = beanClass.newInstance();
  73. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  74. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  75. for (PropertyDescriptor property : propertyDescriptors) {
  76. Method setter = property.getWriteMethod();
  77. if (setter != null) {
  78. setter.invoke(obj, map.get(property.getName()));
  79. }
  80. }
  81. } catch (InstantiationException e) {
  82. String info = "[toObjectByIntrospector] InstantiationException::Map转换Bean失败";
  83. logger.error(info);
  84. e.printStackTrace();
  85. } catch (IllegalAccessException e) {
  86. String info = "[toObjectByIntrospector] IllegalAccessException::Map转换Bean失败";
  87. logger.error(info);
  88. e.printStackTrace();
  89. } catch (IntrospectionException e) {
  90. String info = "[toObjectByIntrospector] IntrospectionException::Map转换Bean失败";
  91. logger.error(info);
  92. e.printStackTrace();
  93. } catch (InvocationTargetException e) {
  94. String info = "[toObjectByIntrospector] InvocationTargetException::Map转换Bean失败";
  95. logger.error(info);
  96. e.printStackTrace();
  97. }
  98. return obj;
  99. }
  100. /**
  101. * bean 转 map by Introspector
  102. * @param obj
  103. * @param <T>
  104. * @return
  105. */
  106. public static <T> Map<String, Object> fromObjectByIntrospector(T obj) {
  107. if (obj == null) {
  108. return null;
  109. }
  110. Map<String, Object> map = new HashMap<String, Object>();
  111. try {
  112. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  113. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  114. for (PropertyDescriptor property : propertyDescriptors) {
  115. String key = property.getName();
  116. if (key.compareToIgnoreCase("class") == 0) {
  117. continue;
  118. }
  119. Method getter = property.getReadMethod();
  120. Object value = getter != null ? getter.invoke(obj) : null;
  121. map.put(key, value);
  122. }
  123. } catch (IntrospectionException e) {
  124. String info = "[fromObjectByIntrospector] IntrospectionException::Bean转换Map失败";
  125. logger.error(info);
  126. e.printStackTrace();
  127. } catch (InvocationTargetException e) {
  128. String info = "[fromObjectByIntrospector] InvocationTargetException::Bean转换Map失败";
  129. logger.error(info);
  130. e.printStackTrace();
  131. } catch (IllegalArgumentException e) {
  132. String info = "[fromObjectByIntrospector] IllegalArgumentException::Bean转换Map失败,";
  133. logger.error(info);
  134. e.printStackTrace();
  135. } catch (IllegalAccessException e) {
  136. String info = "[fromObjectByIntrospector] IllegalAccessException::Bean转换Map失败";
  137. logger.error(info);
  138. e.printStackTrace();
  139. }
  140. return map;
  141. }
  142. //---------- 使用reflect进行转换 ----------
  143. /**
  144. * map 转 bean by Reflect
  145. * @param map
  146. * @param beanClass
  147. * @param <T>
  148. * @return
  149. */
  150. public static <T> T toObjectByReflect(Map<String, Object> map, Class<T> beanClass) {
  151. if (map == null) {
  152. return null;
  153. }
  154. T obj = null;
  155. try {
  156. obj = beanClass.newInstance();
  157. Field[] fields = obj.getClass().getDeclaredFields();
  158. for (Field field : fields) {
  159. int mod = field.getModifiers();
  160. if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
  161. continue;
  162. }
  163. field.setAccessible(true);
  164. field.set(obj, map.get(field.getName()));
  165. }
  166. }catch (InstantiationException e) {
  167. String info = "[toObjectByReflect] InstantiationException::Map转换Bean失败";
  168. logger.error(info);
  169. e.printStackTrace();
  170. } catch (IllegalAccessException e) {
  171. String info = "[toObjectByReflect] IllegalAccessException::Map转换Bean失败";
  172. logger.error(info);
  173. e.printStackTrace();
  174. }
  175. return obj;
  176. }
  177. /**
  178. * bean 转 map by Reflect
  179. * @param obj
  180. * @param <T>
  181. * @return
  182. */
  183. public static <T> Map<String, Object> fromObjectByReflect(T obj) {
  184. if (obj == null) {
  185. return null;
  186. }
  187. Map<String, Object> map = new HashMap<String, Object>();
  188. try{
  189. Field[] declaredFields = obj.getClass().getDeclaredFields();
  190. for (Field field : declaredFields) {
  191. field.setAccessible(true);
  192. map.put(field.getName(), field.get(obj));
  193. }
  194. } catch (IllegalAccessException e) {
  195. String info = "[fromObjectByReflect] IllegalAccessException::Bean转换Map失败";
  196. logger.error(info);
  197. e.printStackTrace();
  198. }
  199. return map;
  200. }
  201. }