public class ClassCompareUtil {
public static boolean compareObject(Object oldObject, Object newObject) {
Map<String, Map<String,Object>> resultMap= compareFields(oldObject,newObject);
if(resultMap.size()>0) {
return true;
}else {
return false;
}
}
@SuppressWarnings("rawtypes")
public static Map<String, Map<String,Object>> compareFields(Object oldObject, Object newObject) {
Map<String, Map<String, Object>> map = null;
try{
if (oldObject.getClass() == newObject.getClass()) {
map = new HashMap<String, Map<String,Object>>();
Class clazz = oldObject.getClass();
PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,Object.class).getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String name = pd.getName();
Method readMethod = pd.getReadMethod();
Object oldValue = readMethod.invoke(oldObject);
Object newValue = readMethod.invoke(newObject);
if(oldValue instanceof List){
continue;
}
if(newValue instanceof List){
continue;
}
if(oldValue instanceof Timestamp){
oldValue = new Date(((Timestamp) oldValue).getTime());
}
if(newValue instanceof Timestamp){
newValue = new Date(((Timestamp) newValue).getTime());
}
if(oldValue == null && newValue == null){
continue;
}else if(oldValue == null && newValue != null){
Map<String,Object> valueMap = new HashMap<String,Object>();
valueMap.put("oldValue",oldValue);
valueMap.put("newValue",newValue);
map.put(name, valueMap);
continue;
}
if (!oldValue.equals(newValue)) {
Map<String,Object> valueMap = new HashMap<String,Object>();
valueMap.put("oldValue",oldValue);
valueMap.put("newValue",newValue);
map.put(name, valueMap);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)