public class CompareTwoClassUtils { public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2) throws Exception { List<Map<String, Object>> list = new ArrayList<>(); Class<?> clazz1 = class1.getClass(); Class<?> clazz2 = class2.getClass(); Field[] fields1 = clazz1.getDeclaredFields(); Field[] fields2 = clazz2.getDeclaredFields(); for (int i = 0; i < fields1.length; i++) { for (int j = 0; j < fields2.length; j++) { if(fields1[i].getName().equals(fields2[j].getName())){ fields1[i].setAccessible(true); fields2[j].setAccessible(true); if(!compareTwo(fields1[i].get(class1), fields2[j].get(class2))){ Map<String, Object> tempMap = new HashMap<>(); tempMap.put("name", fields1[i].getName()); tempMap.put("ole", fields1[i].get(class1)); tempMap.put("new", fields2[j].get(class2)); list.add(tempMap); } } } } return list; } public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2, String checkEnum) throws Exception { List<Map<String, Object>> list = new ArrayList<>(); Class<?> clazz1 = class1.getClass(); Class<?> clazz2 = class2.getClass(); Field[] fields1 = clazz1.getDeclaredFields(); Field[] fields2 = clazz2.getDeclaredFields(); for (int i = 0; i < fields1.length; i++) { for (int j = 0; j < fields2.length; j++) { if(fields1[i].getName().equals(fields2[j].getName())){ fields1[i].setAccessible(true); fields2[j].setAccessible(true); if(!compareTwo(fields1[i].get(class1), fields2[j].get(class2))){ Map<String, Object> tempMap = new HashMap<>(); if(checkEnum.equals("CompanyInfoEnum")){ String value = CompanyInfoEnum.getValue(fields1[i].getName()); if(null != value){ tempMap.put("name", value); tempMap.put("old", fields1[i].get(class1)); tempMap.put("new", fields2[j].get(class2)); list.add(tempMap); } }else if(checkEnum.equals("CompanyDetailEnum")){ String value = CompanyDetailEnum.getValue(fields1[i].getName()); if(null != value){ tempMap.put("name", value); tempMap.put("old", fields1[i].get(class1)); tempMap.put("new", fields2[j].get(class2)); list.add(tempMap); } } } } } } return list; } public static boolean compareTwo(Object object1, Object object2){ boolean equals1 = object1==null || "".equals(object1.toString().trim()) || "null".equals(object1.toString().trim()); boolean equals2 = object2==null || "".equals(object2.toString().trim()) || "null".equals(object2.toString().trim()); if(equals1){ if(equals2){ return true; }else{ return false; } }else{ if(equals2){ return false; }else{ if(object1.equals(object2)){ return true; }else{ return false; } } } } }
获取两个对象同名属性内容不相同的列表工具类
最新推荐文章于 2024-08-21 13:55:04 发布