不同类的属性值Copy工具类BeanCopyUtil

举报
轻狂书生FS 发表于 2020/12/02 23:45:32 2020/12/02
【摘要】 package com.zheng.ocr.common.util; import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set; /...

  
  1. package com.zheng.ocr.common.util;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. /**
  9. * @Author: 轻狂书生FS
  10. * @Description: 两个不同类属性Copy
  11. * @CreateDate: 2019/4/3 14:09
  12. * @Version: 1.0
  13. */
  14. public class BeanCopyUtil {
  15. private static String getGetMethodName(String fieldName) {
  16. fieldName = replaceFirstCharToUpper(fieldName);
  17. return "get" + fieldName;
  18. }
  19. private static String getSetMethodName(String fieldName) {
  20. fieldName = replaceFirstCharToUpper(fieldName);
  21. return "set" + fieldName;
  22. }
  23. private static String replaceFirstCharToUpper(String fieldName) {
  24. char[] chars = new char[1];
  25. chars[0] = fieldName.charAt(0);
  26. String temp = new String(chars);
  27. if (chars[0] >= 'a' && chars[0] <= 'z') {
  28. fieldName = fieldName.replaceFirst(temp, temp.toUpperCase());
  29. }
  30. return fieldName;
  31. }
  32. /**
  33. * @Author: 轻狂书生FS
  34. * @Description: 该方法接收两个参数,一个是复制的源对象——要复制的对象,一个是复制的目标对象——对象副本。
  35. * 当然这个方法也可以在两个不同对象间使用,这时候只要目标对象和对象具有一个或多个相同类型及名称的属性,
  36. * 那么就会把源对象的属性值赋给目标对象的属性
  37. * @params SourceBean源对象,
  38. * TargetBean目标对象,
  39. * @return TargetBean目标对象
  40. * @CreateDate: 2019/4/3 14:12
  41. * @Version: 1.0
  42. */
  43. public static <T> T getBean( T SourceBean,T TargetBean) throws Exception {
  44. if (TargetBean == null)
  45. return null;
  46. Field[] tFields = TargetBean.getClass().getDeclaredFields();
  47. Field[] sFields = SourceBean.getClass().getDeclaredFields();
  48. try {
  49. for (Field field : tFields) {
  50. String fieldName = field.getName();
  51. if (fieldName.equals("serialVersionUID"))
  52. continue;
  53. if (field.getType() == Map.class)
  54. continue;
  55. if (field.getType() == Set.class)
  56. continue;
  57. if (field.getType() == List.class)
  58. continue;
  59. for (Field sField : sFields) {
  60. if (!sField.getName().equals(fieldName)) {
  61. continue;
  62. }
  63. Class type = field.getType();
  64. String setName = getSetMethodName(fieldName);
  65. Method tMethod = TargetBean.getClass().getMethod(setName, new Class[] { type });
  66. String getName = getGetMethodName(fieldName);
  67. Method sMethod = SourceBean.getClass().getMethod(getName, null);
  68. Object setterValue = sMethod.invoke(SourceBean, null);
  69. tMethod.invoke(TargetBean, new Object[] { setterValue });
  70. }
  71. }
  72. } catch (Exception e) {
  73. throw new Exception("设置参数信息发生异常", e);
  74. }
  75. return TargetBean;
  76. }
  77. /**
  78. * @Author: 轻狂书生FS
  79. * @Description: 该方法接收三个参数,一个是复制的源对象——要复制的对象,一个是复制的目标对象——对象副本,一个是源对象与目标对象的属性映射关系
  80. * 本方法是一层拷贝,对象属性不包括集合类型的情况
  81. * 本方法有两种使用场景:
  82. * 一种是两个对象属性名相同时,可用; 一种是属性名字不同是,通过映射关系,也可用。
  83. * @params SourceBean源对象,
  84. * TargetBean目标对象,
  85. * map源对象与目标对象的属性映射关系,key:源对象属性值,value:目标对象属性值
  86. * @return TargetBean目标对象
  87. * @CreateDate: 2019/4/3 14:37
  88. * @Version: 1.0
  89. */
  90. public static <T> T getCopyBean(T SourceBean,T TargetBean, Map<String,String> map) throws Exception {
  91. if (TargetBean == null)
  92. return null;
  93. // 分别获取源对象和目标对象的属性
  94. Field[] tFields = TargetBean.getClass().getDeclaredFields();
  95. Field[] sFields = SourceBean.getClass().getDeclaredFields();
  96. // 将目标对象的属性名字放到Set集合
  97. Set<String> tFieldNames = new HashSet<>();
  98. for (Field field:tFields) {
  99. tFieldNames.add(field.getName());
  100. }
  101. try {
  102. // 遍历源对象属性
  103. for (Field field:sFields) {
  104. String fieldName = field.getName();
  105. if (fieldName.equals("serialVersionUID"))
  106. continue;
  107. if (field.getType() == Map.class)
  108. continue;
  109. if (field.getType() == List.class)
  110. continue;
  111. // 源对象的属性的名字是否在目标对象的属性值里面
  112. boolean isContains = tFieldNames.contains(fieldName);
  113. /**
  114. * 如果存在则源对象的属性值直接赋给目标对象
  115. * 如果不存在则通过映射关系将源对象的属性值赋给目标对象
  116. */
  117. if (isContains) {
  118. Class type = field.getType();
  119. // 获得目标对象属性的set方法
  120. String setName = getSetMethodName(fieldName);
  121. Method tMethod = TargetBean.getClass().getMethod(setName,new Class[]{type});
  122. // 获得源对象属性的get方法
  123. String getName = getGetMethodName(fieldName);
  124. Method sMethod = SourceBean.getClass().getMethod(getName,null);
  125. // 获得源对象的属性的属性值
  126. Object setterValue = sMethod.invoke(SourceBean,null);
  127. // 将setterValue赋给目标对象
  128. tMethod.invoke(TargetBean,new Object[]{setterValue});
  129. } else {
  130. // 判断map和map里面的key值是否为空
  131. if ((null == map) || (null == map.get(fieldName)))
  132. continue;
  133. Class type = field.getType();
  134. // 通过映射关系map获得目标对象属性的set方法
  135. String setName = getSetMethodName(map.get(fieldName));
  136. Method tMethod = TargetBean.getClass().getMethod(setName,new Class[]{type});
  137. // 获得源对象属性的get方法
  138. String getName = getGetMethodName(fieldName);
  139. Method sMethod = SourceBean.getClass().getMethod(getName,null);
  140. Object setterValue = sMethod.invoke(SourceBean,null);
  141. tMethod.invoke(TargetBean,new Object[]{setterValue});
  142. }
  143. }
  144. } catch (Exception e) {
  145. throw new Exception("设置参数信息发生异常", e);
  146. }
  147. return TargetBean;
  148. }
  149. }

 

文章来源: blog.csdn.net,作者:LookForDream_,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/LookForDream_/article/details/89082558

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。