java检查对象属性修改 旧值->新值

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

public class CompareObject<T> {
    /**
     * 检查对象属性修改
     * @param oldBean
     * @param newBean
     * @return
     */
    public String contrastObj(Object oldBean, Object newBean) {
        String str="";
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        try {
            Class clazz = pojo1.getClass();
            Field[] fields = pojo1.getClass().getDeclaredFields();
            int i=1;
            for (Field field : fields) {
                String type = field.getGenericType().toString();
                System.out.println("属性类型=========="+type);
                if("serialVersionUID".equals(field.getName())){
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method getMethod = pd.getReadMethod();
                Object o1 = getMethod.invoke(pojo1);
                Object o2 = getMethod.invoke(pojo2);
                if(o1==null || o2 == null){
                    if(o2 !=null){
                        if(type.equals("class java.util.Date")){
                            Date da = o1==null? null:(Date) o1;
                            Date da2 = (Date) o2;
                           str+="  "+i+"."+getName(field.getName())+",旧值:"+(da==null?"":DateFormatUtils.format(da, "yyyy-MM-dd"))+",新值:"+DateFormatUtils.format(da2, "yyyy-MM-dd");
                           str+=";";
                           i++;
                       }
                    }
                    continue;
                }
                if (!o1.toString().equals(o2.toString())) {
                    if(type.equals("class java.util.Date")){
                         Date da = o1==null? null:(Date) o1;
                         Date da2 = (Date) o2;
                        str+="  "+i+"."+getName(field.getName())+",旧值:"+(da==null?"":DateFormatUtils.format(da, "yyyy-MM-dd"))+",新值:"+DateFormatUtils.format(da2, "yyyy-MM-dd");
                    }else{
                        
                            str+="  "+i+"."+getName(field.getName())+",旧值:"+o1+",新值:"+o2;
                    }
                    str+=";";
                    i++;
                } 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
    
    /**
     * 获取属性名称
     * @param name
     * @return
     */
    public String getName(String name){
        String status =name;
        switch (name) {
        case "A":
            status = "XXX";
            break;
        case "B":
            status = "XXX";
            break;
        case "C":
            status = "XXX";
            break;
        case "D":
            status = "XXX";
            break;
        default:
            break;
        }
        return status;
    }
}

猜你喜欢

转载自blog.csdn.net/u010833811/article/details/84850258