BeanUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.imed.costaccount.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.BeanUtils;
  4. import org.springframework.util.CollectionUtils;
  5. import java.lang.reflect.Field;
  6. import java.util.*;
  7. @Slf4j
  8. public final class BeanUtil {
  9. /**
  10. * 将对象转为为指定的对象
  11. *
  12. * @param source
  13. * @param <T>
  14. * @return
  15. */
  16. public static <T> T convertObj(Object source, Class<T> clazz) {
  17. Object model = null;
  18. if (source == null || clazz == null) {
  19. return null;
  20. }
  21. try {
  22. model = clazz.newInstance();
  23. } catch (Exception e) {
  24. log.error("将对象转为为指定的对象异常", e);
  25. }
  26. BeanUtils.copyProperties(source, model);
  27. return (T) model;
  28. }
  29. public static <T> T convertObj(Object source, T result) {
  30. BeanUtils.copyProperties(source, result);
  31. return result;
  32. }
  33. public static <F, T> List<T> convertList(List<F> fromList, Class<T> tClass) {
  34. List<T> tList = new ArrayList<>();
  35. if (CollectionUtils.isEmpty(fromList)) {
  36. return tList;
  37. }
  38. fromList.forEach(f -> tList.add(convertObj(f, tClass)));
  39. return tList;
  40. }
  41. public static <F, T> List<T> convertListIgnoreCase(List<F> fromList, Class<T> clazz) {
  42. List<T> tList = new ArrayList<>();
  43. if (CollectionUtils.isEmpty(fromList)) {
  44. return tList;
  45. }
  46. fromList.forEach(f -> {
  47. try {
  48. tList.add(copyIgnoreCase(f, clazz.newInstance()));
  49. } catch (Exception e) {
  50. log.error("将对象转为为指定的对象异常", e);
  51. }
  52. });
  53. return tList;
  54. }
  55. /**
  56. * 模仿Spring中 BeanUtils.copyProperties(source,target)
  57. * 类型不同不可以转换
  58. * 但是
  59. * 大小写可以忽略
  60. * 下划线 _ 被忽略
  61. *
  62. * @param source
  63. * @param clazz
  64. * @param <T>
  65. * @return
  66. */
  67. public static <T> T copyIgnoreCase(Object source, Class<T> clazz) {
  68. try {
  69. return copyIgnoreCase(source, clazz.newInstance());
  70. } catch (Exception e) {
  71. log.error("将对象转为为指定的对象异常", e);
  72. }
  73. return null;
  74. }
  75. /**
  76. * 模仿Spring中 BeanUtils.copyProperties(source,target)
  77. * 类型不同不可以转换
  78. * 但是
  79. * 大小写可以忽略
  80. * 下划线 _ 被忽略
  81. *
  82. * @param source
  83. * @param target
  84. * @param <T>
  85. * @return
  86. */
  87. public static <T> T copyIgnoreCase(Object source, T target) {
  88. Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
  89. CacheFieldMap.getFieldMap(target.getClass()).forEach((k, it) -> {
  90. Field field = sourceMap.get(k);
  91. if (field != null && field.getType().equals(it.getType())) {
  92. it.setAccessible(true);
  93. field.setAccessible(true);
  94. try {
  95. it.set(target, field.get(source));
  96. } catch (Exception e) {
  97. log.error("对象复制错误", e);
  98. }
  99. }
  100. });
  101. return target;
  102. }
  103. private static class CacheFieldMap {
  104. private static Map<String, Map<String, Field>> cacheMap = new HashMap<>();
  105. private static final String SER_STR = "serialVersionUID";
  106. private static Map<String, Field> getFieldMap(Class clazz) {
  107. final String name = clazz.getName();
  108. Map<String, Field> result = cacheMap.get(name);
  109. if (result != null) {
  110. return result;
  111. }
  112. synchronized (name) {
  113. if (result != null) {
  114. return result;
  115. }
  116. Map<String, Field> fieldMap = new HashMap<>();
  117. for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
  118. Arrays.stream(clazz.getDeclaredFields()).forEach(field -> {
  119. //忽略序列号字段
  120. if (SER_STR.equals(field.getName())) {
  121. return;
  122. }
  123. fieldMap.put(field.getName().toLowerCase().replace("_", ""), field);
  124. }
  125. );
  126. }
  127. cacheMap.put(name, fieldMap);
  128. return fieldMap;
  129. }
  130. }
  131. }
  132. public static <T> T copyProperties(Object source, T result) {
  133. BeanUtils.copyProperties(source, result);
  134. return result;
  135. }
  136. }