1
0

MapBeanUtil.java 6.8 KB

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