深入比较两个Java对象 [英] Deep Comparision of Two Java Objects

查看:184
本文介绍了深入比较两个Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经提及少数问题此处



用例 -



1。)给定任何两个对象 $ >也,现在比较两个对象的集合,现在内部的集合相同的数据可以颠倒,意思是我有 List< Address> ,它包含两个例如住宅地址办公地址),但是在这两个列表中,数据可能处于不同的索引。

3。)需要创建相同类型的第三个对象,并复制相似的数据,并将属性设置为null并使用不同的数据。


$ b

4。)它可能有引用类。



我累了很多解决方案,其他,我想写一些通用的解决方案。虽然在两个对象中生成两个XML ,然后逐节点比较,但只想获得更多选项。



解决方案



要访问私有字段,您需要调用 Class.getDeclaredField(String name) Class.getDeclaredFields()方法。方法 Class.getField(String name) Class.getFields()方法 return公共字段,所以他们不会工作。



要访问私有方法,您需要调用 Class.getDeclaredMethod name,Class [] parameterTypes) Class.getDeclaredMethods()方法。方法 Class.getMethod(String name,Class [] parameterTypes) Class.getMethods() c $ c> return public methods



XMLUnit 将工作,它将递归地比较列表引用,还可以选择排除您不需要的字段希望比较。

  String expectedXML =some xml; 
String actualXML =some xml;

DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML,actualXML));
diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
System.out.println(Differences found:+ diff1.getAllDifferences()。toString());

RecursiveElementNameAndTextQualifier



< blockquote>

比较两个XML中的所有元素和文本节点。允许$​​ b $ b元素的复杂,深层嵌套类型,在
不同的订单返回,但具有相同的内容被认为
可比较。



Already referred few question here and there.

Use Case -

1.) Given any Two objects, compare both of them property by property.

2.) It will contains Collections also, now compare collections of both the objects, now inside collection same data can be upside down, meaning say i have List<Address>, it contains two entries in both (say Residential Address, Office Address), but in both list the data may be at different indexes.

3.) Need to create 3rd Object of same type, with similar data copied, and properties set to null with different data.

4.) It might have reference classes as well.

I tired many solutions but stuck somewhere or the other, i am thinking of writing some generic solution. Though of generating two xml's out of the two objects and then comparing node by node, but just want to get more options.

Or How much Java reflection is stronger in this case.

解决方案

answer to @markspace question.

To access a private field you will need to call the Class.getDeclaredField(String name) or Class.getDeclaredFields() method. The methods Class.getField(String name) and Class.getFields() methods only return public fields, so they won't work.

To access a private method you will need to call the Class.getDeclaredMethod(String name, Class[] parameterTypes) or Class.getDeclaredMethods() method. The methods Class.getMethod(String name, Class[] parameterTypes) and Class.getMethods() methods only return public methods.

XMLUnit will work, it compares the list references recursively, also it has option to exclude fields which you do not wish to compare.

 String expectedXML = "some xml";
 String actualXML = "some xml";

 DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
 diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
 System.out.println("Differences found: " + diff1.getAllDifferences().toString());

RecursiveElementNameAndTextQualifier

Compares all Element and Text nodes in two pieces of XML. Allows elements of complex, deeply nested types that are returned in different orders but have the same content to be recognized as comparable.

这篇关于深入比较两个Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆