MCPcopy Create free account
hub / github.com/LisenCoding/cangku / ClazzDiff

Class ClazzDiff

boot/src/main/java/com/utils/ClazzDiff.java:17–190  ·  view source on GitHub ↗

对象比较

Source from the content-addressed store, hash-verified

15 * 对象比较
16 */
17public class ClazzDiff {
18
19
20 /**
21 * 对象比较,返回出list,obj1的属性值为空的时候自动忽略比对obj2的属性值
22 * @param obj1 对象1
23 * @param obj2 对象2
24 * @param ignoreProperties 忽略的属性
25 * @return
26 */
27 public List<String> ClazzDiffColumn(Object obj1, Object obj2, @Nullable String... ignoreProperties) throws NoSuchFieldException, IllegalAccessException {
28
29 Assert.notNull(obj1, "obj1不是空的");
30 Assert.notNull(obj2, "obj2不是空的");
31
32
33 List<String> list = new ArrayList<>();
34
35 Class<?> obj1Class = obj1.getClass();
36 Class<?> obj2Class = obj2.getClass();
37 if (!obj1Class.isInstance(obj2)) {
38 throw new IllegalArgumentException("传入的两个类不是同一个类");
39 }
40 List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);//要忽略的属性
41
42 /**
43 * 当前表
44 */
45 Field[] obj1DeclaredFields = obj1Class.getDeclaredFields();
46 for (Field f : obj1DeclaredFields) {//ojb1的所有字段循环
47 f.setAccessible(true);
48 Field obj1Field = obj1Class.getDeclaredField(f.getName());
49 Field obj2Field = obj2Class.getDeclaredField(f.getName());
50 obj1Field.setAccessible(true);
51 obj2Field.setAccessible(true);
52
53// 属性1不为空,属性2不为空 如果传入的有ignoreProperties的话,不为空并且不包含
54 if (obj1Field.get(obj1) != null &&(ignoreList == null || !ignoreList.contains(obj1Field.getName())) ) {
55 ColumnInfo columnInfo = obj1Field.getAnnotation(ColumnInfo.class);
56 Object o1 = obj1Field.get(obj1);
57 Object o2 = obj2Field.get(obj2);
58
59 if(!String.valueOf(o1).equals(String.valueOf(o2))){
60 if(f.getName().contains("File")){
61 list.add(columnInfo.comment()+"-->现在 : <a type='success' style='text-decoration:none' class='el-button' href='"+o1+"' >文件下载</a>,原先 : <a type='success' style='text-decoration:none' class='el-button' href='"+o2+"' >文件下载</a>");
62 }else if(f.getName().contains("Video")){
63 list.add(columnInfo.comment()+"-->现在 : <video src='"+o1+"' width='100px' height='100px' controls='controls'></video>,原先 : <video src='"+o2+"' width='100px' height='100px' controls='controls'></video>");
64 }else if(f.getName().contains("Photo")){
65 list.add(columnInfo.comment()+"-->现在 : <img src='"+o1+"' width='100px' height='100px'>,原先 : <img src='"+o2+"' width='100px' height='100px'>");
66 }else if(f.getName().contains("Time")){
67 list.add(columnInfo.comment()+"-->现在 : ["+ DateUtils.format((Date) o1,"yyyy-MM-dd")+"],原先 : ["+DateUtils.format((Date) o2,"yyyy-MM-dd")+"]");
68 }else{
69 list.add(columnInfo.comment()+"-->现在 : ["+o1+"],原先 : ["+o2+"]");
70 }
71 }
72 }
73 }
74

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected