1
0

BeanUtils.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * 创建时间:2017-09-06 13:48
  3. * 项目名称:kmall_pt
  4. * 类名称:BeanUtils.java
  5. * 包名称:com.kmall.common.utils
  6. */
  7. package com.kmall.common.utils;
  8. import org.springframework.beans.BeansException;
  9. import org.springframework.beans.FatalBeanException;
  10. import org.springframework.util.Assert;
  11. import java.beans.BeanInfo;
  12. import java.beans.Introspector;
  13. import java.beans.PropertyDescriptor;
  14. import java.lang.reflect.Method;
  15. import java.lang.reflect.Modifier;
  16. import java.util.*;
  17. /**
  18. * 名称:BeanUtils <br>
  19. * 描述:<br>
  20. *
  21. * @author Scott
  22. * @version 1.0
  23. * @since 1.0.0
  24. */
  25. public class BeanUtils extends org.springframework.beans.BeanUtils {
  26. /**
  27. * 默认忽略字段<br>
  28. */
  29. private static String[] IGNORE_PROPERTIES = {"createUser", "createTime"};
  30. /**
  31. * 重写copyProperties,NULL值,可以拷贝
  32. * 增加默认忽略字段 modify by zhangguoqing at 2017年4月8日16:13:55
  33. *
  34. * @param source 拷贝元实体
  35. * @param target 拷贝目标实体
  36. * @throws BeansException 抛出异常
  37. */
  38. public static void copyProperties(Object source, Object target, String[] ignoreList)
  39. throws BeansException {
  40. Assert.notNull(source, "Source must not be null");
  41. Assert.notNull(target, "Target must not be null");
  42. List<String> ignorePropertyList = new ArrayList<String>();
  43. ignorePropertyList.addAll(Arrays.asList(IGNORE_PROPERTIES));
  44. // 传入的忽略数组非空扩展忽略数组
  45. if (ignoreList != null && ignoreList.length != 0) {
  46. ignorePropertyList.addAll(Arrays.asList(ignoreList));
  47. }
  48. Class<?> actualEditable = target.getClass();
  49. PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
  50. for (PropertyDescriptor targetPd : targetPds) {
  51. if (targetPd.getWriteMethod() != null) {
  52. PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(),
  53. targetPd.getName());
  54. if (sourcePd != null && sourcePd.getReadMethod() != null
  55. && !ignorePropertyList.contains(targetPd.getName())) {
  56. try {
  57. Method readMethod = sourcePd.getReadMethod();
  58. if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
  59. readMethod.setAccessible(true);
  60. }
  61. Object value = readMethod.invoke(source);
  62. // 这里判断value是否为空,过滤Integer类型字段 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
  63. // if (value != null &&
  64. // !"java.lang.Integer".equals(readMethod.getReturnType().getName())) {
  65. // if (value != null && !"".equals(value)) {
  66. Method writeMethod = targetPd.getWriteMethod();
  67. if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
  68. writeMethod.setAccessible(true);
  69. }
  70. writeMethod.invoke(target, value);
  71. // }
  72. } catch (Throwable ex) {
  73. throw new FatalBeanException(
  74. "Could not copy properties '" + targetPd.getName()
  75. + "' from source to target",
  76. ex);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 重写copyProperties,忽略NULL值
  84. *
  85. * @param source
  86. * @param target
  87. * @throws BeansException
  88. */
  89. public static void copyProperties(Object source, Object target) throws BeansException {
  90. Assert.notNull(source, "Source must not be null");
  91. Assert.notNull(target, "Target must not be null");
  92. Class<?> actualEditable = target.getClass();
  93. PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
  94. for (PropertyDescriptor targetPd : targetPds) {
  95. if (targetPd.getWriteMethod() != null) {
  96. PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
  97. if (sourcePd != null && sourcePd.getReadMethod() != null) {
  98. try {
  99. Method readMethod = sourcePd.getReadMethod();
  100. if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
  101. readMethod.setAccessible(true);
  102. }
  103. Object value = readMethod.invoke(source);
  104. // 这里判断value是否为空,过滤Integer类型字段 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
  105. // if (value != null && !"java.lang.Integer".equals(readMethod.getReturnType().getName())) {
  106. if (value != null && !"".equals(value)) {
  107. Method writeMethod = targetPd.getWriteMethod();
  108. if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
  109. writeMethod.setAccessible(true);
  110. }
  111. writeMethod.invoke(target, value);
  112. }
  113. } catch (Throwable ex) {
  114. throw new FatalBeanException("Could not copy properties '" + targetPd.getName() + "' from source to target", ex);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * bean 转为map
  122. *
  123. * @param obj bean对象
  124. * @param isAllowNull 空字段是否转换 false 不转换
  125. * @return map值
  126. */
  127. public static Map<String, Object> bean2Map(Object obj, boolean isAllowNull) {
  128. if (obj == null) {
  129. return null;
  130. }
  131. Map<String, Object> map = new HashMap<String, Object>();
  132. try {
  133. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  134. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  135. for (PropertyDescriptor property : propertyDescriptors) {
  136. String key = property.getName();
  137. // 过滤class属性
  138. if (!key.equals("class")) {
  139. // 得到property对应的getter方法
  140. Method getter = property.getReadMethod();
  141. Object value = getter.invoke(obj);
  142. if (isAllowNull || value != null && !value.toString().isEmpty()) {
  143. map.put(key, value);
  144. }
  145. }
  146. }
  147. } catch (Exception e) {
  148. System.out.println("transBean2Map Error " + e);
  149. }
  150. return map;
  151. }
  152. /**
  153. * map转bean
  154. *
  155. * @param targetMap 被转化的map
  156. * @param obj 对象
  157. */
  158. public static void map2Bean(Map<String, Object> targetMap, Object obj) {
  159. Map<String, Object> map = new HashMap<String, Object>();
  160. for (String key : targetMap.keySet()) {
  161. Object value = targetMap.get(key);
  162. map.put(StringUtils.lineToHump(key), value);
  163. }
  164. try {
  165. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  166. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  167. for (PropertyDescriptor property : propertyDescriptors) {
  168. String key = property.getName();
  169. if (map.containsKey(key)) {
  170. try {
  171. Object value = map.get(key);
  172. // 得到property对应的setter方法
  173. Method setter = property.getWriteMethod();
  174. setter.invoke(obj, value);
  175. } catch (Exception e) {
  176. throw new RRException("实体转换错误:" + key);
  177. }
  178. }
  179. }
  180. } catch (Exception e) {
  181. e.getStackTrace();
  182. throw new RRException("数据转换异常!");
  183. }
  184. }
  185. }