/src/main/java/cn/jeeweb/core/utils/MyBeanUtils.java

https://github.com/white-cat/jeeweb-mybatis · Java · 323 lines · 205 code · 23 blank · 95 comment · 63 complexity · 3cff86af1255909726e9335cbadf34d9 MD5 · raw file

  1. package cn.jeeweb.core.utils;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import org.apache.commons.beanutils.DynaBean;
  7. import org.apache.commons.beanutils.DynaProperty;
  8. import org.apache.commons.beanutils.PropertyUtils;
  9. import org.apache.commons.beanutils.PropertyUtilsBean;
  10. import com.alibaba.fastjson.JSONObject;
  11. /**
  12. *
  13. * @ClassName: MyBeanUtils
  14. * @Description:实体拷贝
  15. * @author: 王存见
  16. * @date: 2017年3月1日 上午9:17:55
  17. *
  18. *
  19. */
  20. @SuppressWarnings({ "rawtypes", "unchecked" })
  21. public class MyBeanUtils extends PropertyUtilsBean {
  22. public MyBeanUtils() {
  23. super();
  24. }
  25. private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
  26. // Validate existence of the specified beans
  27. if (dest == null) {
  28. throw new IllegalArgumentException("No destination bean specified");
  29. }
  30. if (orig == null) {
  31. throw new IllegalArgumentException("No origin bean specified");
  32. }
  33. // Copy the properties, converting as necessary
  34. if (orig instanceof DynaBean) {
  35. DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
  36. for (int i = 0; i < origDescriptors.length; i++) {
  37. String name = origDescriptors[i].getName();
  38. if (PropertyUtils.isWriteable(dest, name)) {
  39. Object value = ((DynaBean) orig).get(name);
  40. try {
  41. getInstance().setSimpleProperty(dest, name, value);
  42. } catch (Exception e) {
  43. ; // Should not happen
  44. }
  45. }
  46. }
  47. } else if (orig instanceof Map) {
  48. Iterator names = ((Map<?, ?>) orig).keySet().iterator();
  49. while (names.hasNext()) {
  50. String name = (String) names.next();
  51. if (PropertyUtils.isWriteable(dest, name)) {
  52. Object value = ((Map) orig).get(name);
  53. try {
  54. getInstance().setSimpleProperty(dest, name, value);
  55. } catch (Exception e) {
  56. ; // Should not happen
  57. }
  58. }
  59. }
  60. } else
  61. /* if (orig is a standard JavaBean) */
  62. {
  63. PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
  64. for (int i = 0; i < origDescriptors.length; i++) {
  65. String name = origDescriptors[i].getName();
  66. // String type =
  67. // origDescriptors[i].getPropertyType().toString();
  68. if ("class".equals(name)) {
  69. continue; // No point in trying to set an object's class
  70. }
  71. if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
  72. try {
  73. Object value = PropertyUtils.getSimpleProperty(orig, name);
  74. getInstance().setSimpleProperty(dest, name, value);
  75. } catch (java.lang.IllegalArgumentException ie) {
  76. ; // Should not happen
  77. } catch (Exception e) {
  78. ; // Should not happen
  79. }
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * 对象拷贝 数据对象空值不拷贝到目标对象
  86. *
  87. * @Title: copyBeanNotNull2Bean
  88. * @Description: 对象拷贝 数据对象空值不拷贝到目标对象
  89. * @param databean
  90. * @param tobean
  91. * @throws Exception
  92. * @return: void
  93. */
  94. public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception {
  95. PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
  96. for (int i = 0; i < origDescriptors.length; i++) {
  97. String name = origDescriptors[i].getName();
  98. // String type = origDescriptors[i].getPropertyType().toString();
  99. if ("class".equals(name)) {
  100. continue; // No point in trying to set an object's class
  101. }
  102. if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
  103. try {
  104. Object value = PropertyUtils.getSimpleProperty(databean, name);
  105. if (value != null) {
  106. getInstance().setSimpleProperty(tobean, name, value);
  107. }
  108. } catch (java.lang.IllegalArgumentException ie) {
  109. ; // Should not happen
  110. } catch (Exception e) {
  111. ; // Should not happen
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * 对象拷贝 数据对象空值不拷贝到目标对象
  118. *
  119. * @Title: copyJsonNotNull2Bean
  120. * @Description: 对象拷贝 数据对象空值不拷贝到目标对象
  121. * @param json
  122. * @param tobean
  123. * @throws Exception
  124. * @return: void
  125. */
  126. public static void copyJsonNotNull2Bean(String json, Object tobean) throws Exception {
  127. Object databean = JSONObject.parseObject(json, tobean.getClass());
  128. copyBeanNotNull2Bean(databean, tobean);
  129. }
  130. /**
  131. * 把orig和dest相同属性的value复制到dest中
  132. *
  133. * @param dest
  134. * @param orig
  135. * @throws IllegalAccessException
  136. * @throws InvocationTargetException
  137. */
  138. public static void copyBean2Bean(Object dest, Object orig) throws Exception {
  139. convert(dest, orig);
  140. }
  141. public static void copyBean2Map(Map map, Object bean) {
  142. PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
  143. for (int i = 0; i < pds.length; i++) {
  144. PropertyDescriptor pd = pds[i];
  145. String propname = pd.getName();
  146. try {
  147. Object propvalue = PropertyUtils.getSimpleProperty(bean, propname);
  148. map.put(propname, propvalue);
  149. } catch (IllegalAccessException e) {
  150. } catch (InvocationTargetException e) {
  151. } catch (NoSuchMethodException e) {
  152. }
  153. }
  154. }
  155. /**
  156. * 将Map内的key与Bean中属性相同的内容复制到BEAN中
  157. *
  158. * @param bean
  159. * Object
  160. * @param properties
  161. * Map
  162. * @throws IllegalAccessException
  163. * @throws InvocationTargetException
  164. */
  165. public static void copyMap2Bean(Object bean, Map properties)
  166. throws IllegalAccessException, InvocationTargetException {
  167. // Do nothing unless both arguments have been specified
  168. if ((bean == null) || (properties == null)) {
  169. return;
  170. }
  171. // Loop through the property name/value pairs to be set
  172. Iterator names = properties.keySet().iterator();
  173. while (names.hasNext()) {
  174. String name = (String) names.next();
  175. // Identify the property name and value(s) to be assigned
  176. if (name == null) {
  177. continue;
  178. }
  179. Object value = properties.get(name);
  180. try {
  181. Class clazz = PropertyUtils.getPropertyType(bean, name);
  182. if (null == clazz) {
  183. continue;
  184. }
  185. String className = clazz.getName();
  186. if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  187. if (value == null || value.equals("")) {
  188. continue;
  189. }
  190. }
  191. getInstance().setSimpleProperty(bean, name, value);
  192. } catch (NoSuchMethodException e) {
  193. continue;
  194. }
  195. }
  196. }
  197. /**
  198. * 自动转Map key值大写 将Map内的key与Bean中属性相同的内容复制到BEAN中
  199. *
  200. * @param bean
  201. * Object
  202. * @param properties
  203. * Map
  204. * @throws IllegalAccessException
  205. * @throws InvocationTargetException
  206. */
  207. public static void copyMap2Bean_Nobig(Object bean, Map properties)
  208. throws IllegalAccessException, InvocationTargetException {
  209. // Do nothing unless both arguments have been specified
  210. if ((bean == null) || (properties == null)) {
  211. return;
  212. }
  213. // Loop through the property name/value pairs to be set
  214. Iterator names = properties.keySet().iterator();
  215. while (names.hasNext()) {
  216. String name = (String) names.next();
  217. // Identify the property name and value(s) to be assigned
  218. if (name == null) {
  219. continue;
  220. }
  221. Object value = properties.get(name);
  222. // 命名应该大小写应该敏感(否则取不到对象的属性)
  223. // name = name.toLowerCase();
  224. try {
  225. if (value == null) { // 不光Date类型,好多类型在null时会出错
  226. continue; // 如果为null不用设 (对象如果有特殊初始值也可以保留?)
  227. }
  228. Class clazz = PropertyUtils.getPropertyType(bean, name);
  229. if (null == clazz) { // 在bean中这个属性不存在
  230. continue;
  231. }
  232. String className = clazz.getName();
  233. // 临时对策(如果不处理默认的类型转换时会出错)
  234. if (className.equalsIgnoreCase("java.util.Date")) {
  235. value = new java.util.Date(((java.sql.Timestamp) value).getTime());// wait
  236. // to
  237. // do:貌似有时区问题,
  238. // 待进一步确认
  239. }
  240. // if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  241. // if (value == null || value.equals("")) {
  242. // continue;
  243. // }
  244. // }
  245. getInstance().setSimpleProperty(bean, name, value);
  246. } catch (NoSuchMethodException e) {
  247. continue;
  248. }
  249. }
  250. }
  251. /**
  252. * Map内的key与Bean中属性相同的内容复制到BEAN中 对于存在空值的取默认值
  253. *
  254. * @param bean
  255. * Object
  256. * @param properties
  257. * Map
  258. * @param defaultValue
  259. * String
  260. * @throws IllegalAccessException
  261. * @throws InvocationTargetException
  262. */
  263. public static void copyMap2Bean(Object bean, Map properties, String defaultValue)
  264. throws IllegalAccessException, InvocationTargetException {
  265. // Do nothing unless both arguments have been specified
  266. if ((bean == null) || (properties == null)) {
  267. return;
  268. }
  269. // Loop through the property name/value pairs to be set
  270. Iterator names = properties.keySet().iterator();
  271. while (names.hasNext()) {
  272. String name = (String) names.next();
  273. // Identify the property name and value(s) to be assigned
  274. if (name == null) {
  275. continue;
  276. }
  277. Object value = properties.get(name);
  278. try {
  279. Class clazz = PropertyUtils.getPropertyType(bean, name);
  280. if (null == clazz) {
  281. continue;
  282. }
  283. String className = clazz.getName();
  284. if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  285. if (value == null || value.equals("")) {
  286. continue;
  287. }
  288. }
  289. if (className.equalsIgnoreCase("java.lang.String")) {
  290. if (value == null) {
  291. value = defaultValue;
  292. }
  293. }
  294. getInstance().setSimpleProperty(bean, name, value);
  295. } catch (NoSuchMethodException e) {
  296. continue;
  297. }
  298. }
  299. }
  300. }