/** * @Title : CompareObjUtil.java * @Package cc.messcat.common.util * @Description: TODO * @author limh * @date 2017年3月2日 */ package cc.messcat.util;
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List;
import cc.messcat.vo.Comparison;
/** * ClassName: 对象比较类 * @Description: TODO * @author limh * @date 2017年3月2日 */ public class CompareObjUtil {
public static List<Comparison> compareObj(Object beforeObj,Object afterObj) throws Exception{ List<Comparison> differents = new ArrayList<Comparison>(); if(beforeObj == null) throw new Exception("原对象不能为空"); if(afterObj == null) throw new Exception("新对象不能为空"); if(!beforeObj.getClass().isAssignableFrom(afterObj.getClass())){ throw new Exception("两个对象不相同,无法比较"); } //取出属性 Field[] beforeFields = beforeObj.getClass().getDeclaredFields(); Field[] afterFields = afterObj.getClass().getDeclaredFields(); Field.setAccessible(beforeFields, true); Field.setAccessible(afterFields, true); //遍历取出差异值 if(beforeFields != null && beforeFields.length > 0){ for(int i=0; i<beforeFields.length; i++){ Object beforeValue = beforeFields[i].get(beforeObj); Object afterValue = afterFields[i].get(afterObj); if((beforeValue != null && !"".equals(beforeValue) && !beforeValue.equals(afterValue)) || ((beforeValue == null || "".equals(beforeValue)) && afterValue != null)){ Comparison comparison = new Comparison(); comparison.setField(beforeFields[i].getName()); comparison.setBefore(beforeValue); comparison.setAfter(afterValue); differents.add(comparison); } } } return differents; } }
/** * @Title: Comparison.java * @Package cc.messcat.vo * @Description: TODO * @author limh * @date 2017年3月2日 */ package cc.messcat.vo;
/** * ClassName: 对照实体 * @Description: TODO * @author limh * @date 2017年3月2日 */ public class Comparison { //字段 private String Field; //字段旧值 private Object before; //字段新值 private Object after; /** * @return the field */ public String getField() { return Field; } /** * @param field the field to set */ public void setField(String field) { Field = field; } /** * @return the before */ public Object getBefore() { return before; } /** * @param before the before to set */ public void setBefore(Object before) { this.before = before; } /** * @return the after */ public Object getAfter() { return after; } /** * @param after the after to set */ public void setAfter(Object after) { this.after = after; } }
转载于:https://my.oschina.net/u/2484728/blog/849545