|
@@ -33,40 +33,35 @@ public class DiffUtils {
|
|
|
*/
|
|
*/
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
|
public static <T> List<DiffResult> diff(T a, T b) {
|
|
public static <T> List<DiffResult> diff(T a, T b) {
|
|
|
- if (equals(a, b)) {
|
|
|
|
|
- return Collections.EMPTY_LIST;
|
|
|
|
|
|
|
+ if (a == null && b == null) return Collections.emptyList();
|
|
|
|
|
+ if (a != null && b != null && !a.getClass().equals(b.getClass())) {
|
|
|
|
|
+ throw new IllegalArgumentException("a 与 b 必须是相同类型");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 获取并缓存一个类中需要对比的字段
|
|
|
|
|
- Map<String, Field> cachedDiffPropertyMap = DIFF_PROPERTY_CACHE.computeIfAbsent((a != null ? a : b).getClass(),
|
|
|
|
|
- clazz -> {
|
|
|
|
|
- Map<String, Field> diffPropertyMap = new HashMap<>();
|
|
|
|
|
- for (Field field : clazz.getDeclaredFields()) {
|
|
|
|
|
- DiffProperty diffProperty = field.getAnnotation(DiffProperty.class);
|
|
|
|
|
- if (diffProperty != null) {
|
|
|
|
|
- field.setAccessible(true);
|
|
|
|
|
- String name = diffProperty.name();
|
|
|
|
|
- if (name.isEmpty()) {
|
|
|
|
|
- name = field.getName();
|
|
|
|
|
- }
|
|
|
|
|
- diffPropertyMap.put(name, field);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return diffPropertyMap;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ Class<?> clazz = (a != null ? a : b).getClass();
|
|
|
|
|
+ Map<String, Field> cachedDiffPropertyMap = DIFF_PROPERTY_CACHE.computeIfAbsent(clazz, clz -> {
|
|
|
|
|
+ Map<String, Field> map = new HashMap<>();
|
|
|
|
|
+ for (Field field : clz.getDeclaredFields()) {
|
|
|
|
|
+ DiffProperty prop = field.getAnnotation(DiffProperty.class);
|
|
|
|
|
+ if (prop != null) {
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ String name = prop.name().isEmpty() ? field.getName() : prop.name();
|
|
|
|
|
+ map.put(name, field);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return map;
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- // 新老对象,属性值进行对比,并将对比结果,不同的进行返回
|
|
|
|
|
- List<DiffResult> diffResultList = new ArrayList<>();
|
|
|
|
|
- for (Map.Entry<String, Field> fieldEntry : cachedDiffPropertyMap.entrySet()) {
|
|
|
|
|
- Field field = fieldEntry.getValue();
|
|
|
|
|
- // field.get(a) 获取a对象field这个属性名的属性值!
|
|
|
|
|
- Object aValue = a == null ? null : field.get(a);
|
|
|
|
|
- Object bValue = b == null ? null : field.get(b);
|
|
|
|
|
- if (!equals(aValue, bValue)) {
|
|
|
|
|
- diffResultList.add(new DiffResult(field.getName(), fieldEntry.getKey(), aValue, bValue));
|
|
|
|
|
|
|
+ List<DiffResult> result = new ArrayList<>();
|
|
|
|
|
+ for (Map.Entry<String, Field> entry : cachedDiffPropertyMap.entrySet()) {
|
|
|
|
|
+ Field field = entry.getValue();
|
|
|
|
|
+ Object aVal = (a == null) ? null : field.get(a);
|
|
|
|
|
+ Object bVal = (b == null) ? null : field.get(b);
|
|
|
|
|
+ if (!Objects.equals(aVal, bVal)) {
|
|
|
|
|
+ result.add(new DiffResult(field.getName(), entry.getKey(), aVal, bVal));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return diffResultList;
|
|
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|