自定义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
- 自定义test之java两个对象之间值的异同
- Java中集合对象之间的异同关系
- java两个对象之间传值,简单的封装
- 自定义test之java json字符串转为java对象
- 自定义test之java对象属性拷贝简单实现
- Java 和 C# 之间的异同点
- Java 对象之间的比较,判断两个对象的某一个属性相等,则对象相等
- flex和java之间的自定义对象转换
- C++、java面向对象的异同
- 同一台计算机上的两个flash应用程序之间传递自定义对象出错
- 实现两个对象之间每个值的交换
- 两个对象之间很好的交互方法!
- java之比较两个对象
- Perl、PHP、Python、Java和Rub之间的异同比较
- 利用eclipse比较两个文件的代码差异或者一个文件不同版本之间的异同
- python之于java的异同 -- 类
- Java之POJO和javabean的异同
- 自定义test之dubbo注解的实现
- String类和StringBuffer类的区别
- npm install node-sass fail
- 树形结合 斜率优化 初步学习
- 西邮记(XUPT)
- 蜘蛛图表SpiderChart
- 自定义test之java两个对象之间值的异同
- Longest Substring Without Repeating Characters leetcode
- 线性时间排序
- 推荐几个比较好的:Json解析的网络源文件连接
- 如何查看linux操作系统是32位还是64位
- 使用gradle打包jar包
- Integer.MAX_VALUE+1=Integer.MIN_VALUE
- AngularJS 初始化指令- ng-init
- UML详解