aeb82cb8b86e43402137a7046035597080eac399.svn-base 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package cn.com.goldenwater.dcproj.utils;
  2. import org.apache.commons.beanutils.BeanUtils;
  3. import java.beans.BeanInfo;
  4. import java.beans.Introspector;
  5. import java.beans.PropertyDescriptor;
  6. import java.lang.reflect.Field;
  7. import java.lang.reflect.Method;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * 当把Person类作为BeanUtilTest的内部类时,程序出错<br>
  13. * java.lang.NoSuchMethodException: Property '**' has no setter method<br>
  14. * 本质:内部类 和 单独文件中的类的区别 <br>
  15. * BeanUtils.populate方法的限制:<br>
  16. * The class must be public, and provide a public constructor that accepts no arguments. <br>
  17. * This allows tools and applications to dynamically create new instances of your bean, <br>
  18. * without necessarily knowing what Java class name will be used ahead of time
  19. */
  20. public class BeanUtil {
  21. // Map --> Bean 2: 利用org.apache.commons.beanutils 工具类实现 Map --> Bean
  22. public static void transMap2Bean2(Map<String, Object> map, Object obj) {
  23. if (map == null || obj == null) {
  24. return;
  25. }
  26. try {
  27. BeanUtils.populate(obj, map);
  28. } catch (Exception e) {
  29. System.out.println("transMap2Bean2 Error " + e);
  30. }
  31. }
  32. /**
  33. * 通过Introspector 属性赋值 ,通过list过滤
  34. * @param fromObj
  35. * @param toObj
  36. */
  37. public static void copyObject(Object fromObj,Object toObj,List<String> list) {
  38. if (fromObj == null || toObj == null) {
  39. return;
  40. }
  41. try {
  42. Class fromClass = fromObj.getClass();
  43. BeanInfo beanInfo = Introspector.getBeanInfo(toObj.getClass());
  44. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  45. for (PropertyDescriptor property : propertyDescriptors) {
  46. try {
  47. String key = property.getName();
  48. if (list != null && list.contains(key)) {
  49. continue;
  50. }
  51. Field filed = fromClass.getDeclaredField(key);
  52. filed.setAccessible(true);
  53. if (filed != null) {
  54. Object value = filed.get(fromObj);
  55. if (value != null) {
  56. property.getWriteMethod().invoke(toObj, value);
  57. }
  58. }
  59. } catch (Exception e) {
  60. System.out.println("setValue error,continue current pro" +e.getMessage());
  61. }
  62. }
  63. } catch (Exception e) {
  64. System.out.println("copyObject Error" + e);
  65. }
  66. }
  67. /**
  68. * 通过BeanUtils.copyProperties 复制,无法过滤属性列表
  69. * @param fromObj
  70. * @param toObj
  71. */
  72. public static void copyObject1(Object fromObj,Object toObj){
  73. if (fromObj == null || toObj == null) {
  74. return;
  75. }
  76. try {
  77. BeanUtils.copyProperties(toObj,fromObj);
  78. } catch (Exception e) {
  79. System.out.println("copyObject Error" + e);
  80. }
  81. }
  82. /**
  83. * 通过BeanUtils.copyProperty 复制 list过滤属性列表
  84. * @param fromObj
  85. * @param toObj
  86. */
  87. public static void copyObject2(Object fromObj,Object toObj,List<String> list){
  88. if (fromObj == null || toObj == null) {
  89. return;
  90. }
  91. Field []fields = toObj.getClass().getDeclaredFields();
  92. for (Field field : fields){
  93. String name = field.getName();
  94. if (list != null && list.contains(name)) {
  95. continue;
  96. }
  97. try {
  98. BeanUtils.copyProperty(toObj, name, fromObj);
  99. } catch (Exception e) {
  100. System.out.println("copyObject Error" + e);
  101. }
  102. }
  103. }
  104. // Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
  105. public static void transMap2Bean(Map<String, Object> map, Object obj) {
  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 (map.containsKey(key)) {
  112. Object value = map.get(key);
  113. // 得到property对应的setter方法
  114. Method setter = property.getWriteMethod();
  115. setter.invoke(obj, value);
  116. }
  117. }
  118. } catch (Exception e) {
  119. System.out.println("transMap2Bean Error " + e);
  120. }
  121. return;
  122. }
  123. // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
  124. public static Map<String, Object> transBean2Map(Object obj) {
  125. if (obj == null) {
  126. return null;
  127. }
  128. Map<String, Object> map = new HashMap<String, Object>();
  129. try {
  130. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  131. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  132. String key="";
  133. for (PropertyDescriptor property : propertyDescriptors) {
  134. key = property.getName();
  135. // 过滤class属性
  136. if (!"class".equals(key)) {
  137. // 得到property对应的getter方法
  138. Method getter = property.getReadMethod();
  139. Object value = getter.invoke(obj);
  140. map.put(key, value);
  141. }
  142. }
  143. } catch (Exception e) {
  144. System.out.println("transBean2Map Error " + e);
  145. }
  146. return map;
  147. }
  148. }