自定义test之java两个对象之间值的异同

来源:互联网 发布:微喜帖制作软件 编辑:程序博客网 时间:2024/06/05 11:55

    作者在java开发过程中遇到项目迁移——从一个大的系统中把一个模块迁移出来做为一个单独的系统服务(微服务),在迁移过程中会遇到大量的功能相同的java对象vo比较问题(新老系统相似功能的 vo对接——相互替代的对象),毕竟新系统的新vo多少会和老系统的vo做些优化、改进之类的,所以便有了两个类似vo对象值之间的比较,人工比较简直要眼瞎,所以作者简单写了一个小的ava两个对象之间值的异同的比较工具,比较简单,目前只提供了简单类型的比较,map,集合类型的暂时还未实现,后续完成...


//两个对象类型属性和值比较
public static Map<String,Object> compareObjectDiffFieldAndValue(Object fromObject,Object toObject) throws Exception {
Map<String,Object> result = new LinkedHashMap<String, Object>();
Field[] fromFields = fromObject.getClass().getDeclaredFields();
Field[] toFields = toObject.getClass().getDeclaredFields();
if(fromFields == null || toFields == null){
return result;
}

Map<String,String> fromFieldMap = new HashMap<String, String>();
Map<String,String> toFieldMap = new HashMap<String, String>();
Map<String,Field> commonFieldMap = new HashMap<String, Field>();
for(int i=0;i<toFields.length + fromFields.length;i++){
if(i<toFields.length){
toFieldMap.put(toFields[i].getType()+toFields[i].getName(),toFields[i].getName());
}else{
String fromKey = fromFields[i-toFields.length].getType() + fromFields[i-toFields.length].getName();
if(toFieldMap.get(fromKey) != null){
commonFieldMap.put(fromKey,fromFields[i-toFields.length]);
toFieldMap.remove(fromKey);
}else{
fromFieldMap.put(fromKey,fromFields[i-toFields.length].getName());
}
}
}



Map<String,Map<String,Object>> commonFieldUnequalMap = new HashMap<String, Map<String,Object>>();
Map<String,String> commonFieldExceptionMap = new HashMap<String, String>();//调用get方法异常属性列表

Iterator<Map.Entry<String,Field>> iterator = commonFieldMap.entrySet().iterator();
while(iterator.hasNext()) {
Field field = iterator.next().getValue();
Object fromFieldValue = null;
Object toFieldValue = null;
try {
PropertyDescriptor fromProperty = new PropertyDescriptor(field.getName(), fromObject.getClass());
PropertyDescriptor toProperty = new PropertyDescriptor(field.getName(), toObject.getClass());
Method fromReadMethod = fromProperty.getReadMethod();
Method toReadMethod = toProperty.getReadMethod();
fromFieldValue = fromReadMethod.invoke(fromObject);
toFieldValue = toReadMethod.invoke(toObject);
} catch (Exception e) {
System.out.println("Exception:" + e.getClass() + " message:" + e.getMessage());
String commomFieldUnequalKey = field.getType() + field.getName();
iterator.remove();
commonFieldExceptionMap.put(commomFieldUnequalKey, field.getName());
continue;
}
if (fromFieldValue == toFieldValue || (toFieldValue != null && toFieldValue.equals(fromFieldValue))) {
} else if(hasClassObject(fromFieldValue) && hasClassObject(toFieldValue)){

Map<String,Object> temResult = compareObjectDiffFieldAndValue(fromFieldValue,toFieldValue);
List<String> commonFieldUnequalList = (List<String>) temResult.get("commonFieldUnequalList");
if(commonFieldUnequalList != null && commonFieldUnequalList.size() > 0) {
StringBuilder names = new StringBuilder();
names.append("{");
for (Object obj : commonFieldUnequalList) {
// String name = obj.toString().split(" -> ")[0];
names.append(obj.toString()).append(" ,");
}
names = names.deleteCharAt(names.length() - 1);
names.append("}");

String commomFieldUnequalKey = field.getType() + field.getName();
iterator.remove();
Map<String, Object> map = new HashMap<String, Object>();
map.put("fieldName", field.getName());
map.put("isClass", "YES");
map.put("names", names.toString());
commonFieldUnequalMap.put(commomFieldUnequalKey, map);
}
}else{
String commomFieldUnequalKey = field.getType()+field.getName();
iterator.remove();
Map<String,Object> map = new HashMap<String, Object>();
map.put("fieldName",field.getName());
map.put("fieldFromValue",fromFieldValue);
map.put("fieldToValue",toFieldValue);
commonFieldUnequalMap.put(commomFieldUnequalKey,map);
}


}


List<String> fromFieldList = new ArrayList<String>();
List<String> toFieldList = new ArrayList<String>();
List<String> commonFieldList = new ArrayList<String>();
List<String> commonFieldUnequalList = new ArrayList<String>();
List<String> commonFieldExceptionList = new ArrayList<String>();

fromFieldList.addAll(fromFieldMap.values());
toFieldList.addAll(toFieldMap.values());
commonFieldExceptionList.addAll(commonFieldExceptionMap.values());

for(Field field:commonFieldMap.values()){
commonFieldList.add(field.getName());
}
for(Map<String,Object> map:commonFieldUnequalMap.values()){
if(map.get("isClass") != null){
commonFieldUnequalList.add(map.get("fieldName").toString() + " -> " + map.get("names"));
}else {
commonFieldUnequalList.add(map.get("fieldName").toString() + " -> " + map.get("fieldFromValue") + " vs " + map.get("fieldToValue"));
}
}


result.put("fromFieldList",fromFieldList);
result.put("toFieldList",toFieldList);
result.put("commonFieldList",commonFieldList);
result.put("commonFieldUnequalList",commonFieldUnequalList);
result.put("commonFieldExceptionList",commonFieldExceptionList);

return result;
}


private static boolean hasClassObject(Object obj){
if(obj instanceof String || obj instanceof Byte || obj instanceof Character || obj instanceof Integer || obj instanceof Long || obj instanceof Short
|| obj instanceof Boolean || obj instanceof Float || obj instanceof Double || obj instanceof Date || obj instanceof
Map || obj instanceof Collection){//map list暂时不做深入处理
return false;
}
return true;
}

fromFieldList:from对象的独有属性结合,toFieldList:to对象的独有属性集合,commonFieldList:二者共同的属性集合,commonFieldUnequalList:二者属性相同而值不同的集合(列出了属性的两个不同的值内容 ),commonFieldExceptionList:处理过程中异常的属性结合

代码的基本思路是反射先拿到两个对象的属性集合,进一步对两对象的属性名称和类型进行判断,找出名称和类型相同的属性,再利用反射调用属性的get方法获取属性的值,进一步比较值是否相等,如果不等则列出两个不同的值,方便查看。


0 0
原创粉丝点击