java comparefield_How would I compare field values of two Java objects of the same but unknown type ...

We are parsing XML configuration files with JAXB into Java objects. The XML files are versioned and after loading version 1.0 and 2.0 into objects we would like to compare the two objects of the same but unknown type (there are many different configurations for all kinds of things) recursively and their field values and print out the differences.

An object might look as follows.

@XmlRootElement(name = "HelloWorld")

public class HelloWorldConfiguration {

private List helloWorldObjects = new ArrayList();

public HelloWorldConfiguration() {

HelloWorldObject o = new HelloWorldObject();

helloWorldObjects.add(o);

helloWorldObjects.add(o);

helloWorldObjects.add(o);

helloWorldObjects.add(o);

helloWorldObjects.add(o);

}

@XmlElement(name = "helloWorldObject")

public List getHelloWorldObjects() {

return helloWorldObjects;

}

public void setHelloWorldObjects(List helloWorldObjects) {

this.helloWorldObjects = helloWorldObjects;

}

}

public class HelloWorldObject {

private Stage firstName = new Stage("Tony");

private Stage secondName = new Stage("Stark");

public Stage getFirstName() {

return firstName;

}

public void setFirstName(Stage firstName) {

this.firstName = firstName;

}

public Stage getSecondName() {

return secondName;

}

public void setSecondName(Stage secondName) {

this.secondName = secondName;

}

}

For example we would like to be informed about following changes about the above HelloWorldConfiguration object?

there is additional "HelloWorldObject" item in the list (the item with its attributes must be printed on screen)

the "HelloWorldObject" at the position n has a new "firstName" value (the name of the field or XML element that changed and its value should be printed)

the new "HelloWorldObject" list is shorter by 2 following elements (the missing elements must be printed with all attributes and values)

My questions are as follows.

Would you solve this with reflection on the Java object level or compare the two different XML files?

Are there any libraries out there that already do something like that for me? On XML or Java object level?

Any examples?

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Disclaimer. I am the author of the JAXB2 Basics plugin package which includes the JAXB2 Equals plugin.

If you generate your classes from an XML Schema, the JAXB2 Equals plugin might be of use for you in this use case.

The JAXB2 Equals plugin is capable of generating equals methods which do deep structure-traversing value comparison of JAXB class instances:

public boolean equals(Object object) {

final EqualsStrategy strategy = JAXBEqualsStrategy.INSTANCE;

return equals(null, null, object, strategy);

}

public boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator, Object object, EqualsStrategy strategy) {

if (!(object instanceof PurchaseOrderType)) {

return false;

}

if (this == object) {

return true;

}

final PurchaseOrderType that = ((PurchaseOrderType) object);

{

USAddress lhsShipTo;

lhsShipTo = this.getShipTo();

USAddress rhsShipTo;

rhsShipTo = that.getShipTo();

if (!strategy.equals(LocatorUtils.property(thisLocator, "shipTo", lhsShipTo), LocatorUtils.property(thatLocator, "shipTo", rhsShipTo), lhsShipTo, rhsShipTo)) {

return false;

}

}

{

USAddress lhsBillTo;

lhsBillTo = this.getBillTo();

USAddress rhsBillTo;

rhsBillTo = that.getBillTo();

if (!strategy.equals(LocatorUtils.property(thisLocator, "billTo", lhsBillTo), LocatorUtils.property(thatLocator, "billTo", rhsBillTo), lhsBillTo, rhsBillTo)) {

return false;

}

}

// ...

return true;

}

I hope you've got the idea. You can provide a "locator" which would track the location of things being compared and a strategy which will do the comparison of individual values.

As the result you can:

Do an in-depth comparison of schema-derived JAXB class instances.

Know what is different (exact values).

Know where are the differences (exact location in the object structure).

And the whole thing is reflection-free and therefore quite fast.

Below is a snippet from another project. This is from one of the tests where I compare object "before" and "after" and log the differences.

final EqualsStrategy strategy = new org.jvnet.hyperjaxb3.lang.builder.ExtendedJAXBEqualsStrategy() {

@Override

public boolean equals(ObjectLocator leftLocator,

ObjectLocator rightLocator, Object lhs, Object rhs) {

if (!super.equals(leftLocator, rightLocator, lhs, rhs)) {

logger.debug("Objects are not equal.");

super.equals(leftLocator, rightLocator, lhs, rhs);

logger.debug("Left: "

+ (lhs == null ? "null" : lhs.toString()));

if (leftLocator != null) {

logger.debug("At [" + leftLocator.getPathAsString()

+ "].");

}

logger.debug("Right: "

+ (rhs == null ? "null" : rhs.toString()));

if (rightLocator != null) {

logger.debug("At [" + rightLocator.getPathAsString()

+ "].");

}

return false;

} else

{

return true;

}

}

};

From the other hand, this approach is not a real "diff" as you may know it from VCS. It only says that something is different, but does not calculate any "shortest edit distance".

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值