yanglaoyaunguanlixitong/src/main/java/com/utils/ClazzDiff.java

(开头部分) 8KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

package com.utils;

import com.annotation.ColumnInfo;
import org.apache.tools.ant.util.DateUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 对象比较
 */
public class ClazzDiff {


    /**
     * 对象比较,返回出list,obj1的属性值为空的时候自动忽略比对obj2的属性值
     * @param obj1 对象1
     * @param obj2 对象2
     * @param ignoreProperties 忽略的属性
     * @return
     */
    public  List<String> ClazzDiffColumn(Object obj1, Object obj2, @Nullable String... ignoreProperties) throws NoSuchFieldException, IllegalAccessException {

        Assert.notNull(obj1, "obj1不是空的");
        Assert.notNull(obj2, "obj2不是空的");


        List<String> list = new ArrayList<>();

        Class<?> obj1Class = obj1.getClass();
        Class<?> obj2Class = obj2.getClass();
        if (!obj1Class.isInstance(obj2)) {
            throw new IllegalArgumentException("传入的两个类不是同一个类");
        }
        List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);//要忽略的属性

        /**
         * 当前表
         */
        Field[] obj1DeclaredFields = obj1Class.getDeclaredFields();
        for (Field f : obj1DeclaredFields) {//ojb1的所有字段循环
            f.setAccessible(true);
            Field obj1Field = obj1Class.getDeclaredField(f.getName());
            Field obj2Field = obj2Class.getDeclaredField(f.getName());
            obj1Field.setAccessible(true);
            obj2Field.setAccessible(true);

//            属性1不为空,属性2不为空 如果传入的有ignoreProperties的话,不为空并且不包含
            if (obj1Field.get(obj1) != null &&(ignoreList == null || !ignoreList.contains(obj1Field.getName())) ) {
                ColumnInfo columnInfo = obj1Field.getAnnotation(ColumnInfo.class);
                Object o1 = obj1Field.get(obj1);
                Object o2 = obj2Field.get(obj2);

                if(!String.valueOf(o1).equals(String.valueOf(o2))){
                    if(f.getName().contains("File")){
后面还有 130 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。