xml - Generate Equals und HashCode with jaxb2-maven-plugin Version 2.2 -
we use jaxb2-maven-plugin (version 2.2) , generate equal , hashcode method each jaxbobject. have alreade binding.xjb file configure anything.
is there way generate methods?
if try add arguments -xequals -xhashcode, following exception: unbekannter parameter -xequals -xhashcode
configuration:
<configuration> <arguments>-xequals -xhashcode</arguments> </configuration>
thank you!
you can generate hashcode
, equals
jaxb2 basics plugins:
<project ...> ... <build> <plugins> ... <plugin> <groupid>org.jvnet.jaxb2.maven2</groupid> <artifactid>maven-jaxb2-plugin</artifactid> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <extension>true</extension> <args> <arg>-xsimpleequals</arg> <arg>-xsimplehashcode</arg> </args> <plugins> <plugin> <groupid>org.jvnet.jaxb2_commons</groupid> <artifactid>jaxb2-basics</artifactid> <version>...</version> </plugin> </plugins> </configuration> </plugin> </plugins> </build> ... </project>
this generate deep reflection-free runtime dependencies-free equals
, hashcode
methods:
public boolean equals(object object) { if ((object == null)||(this.getclass()!= object.getclass())) { return false; } if (this == object) { return true; } final purchaseordertype = ((purchaseordertype) object); { usaddress leftshipto; leftshipto = this.getshipto(); usaddress rightshipto; rightshipto = that.getshipto(); if (this.shipto!= null) { if (that.shipto!= null) { if (!leftshipto.equals(rightshipto)) { return false; } } else { return false; } } else { if (that.shipto!= null) { return false; } } } // ... return true; } public int hashcode() { int currenthashcode = 1; { currenthashcode = (currenthashcode* 31); usaddress theshipto; theshipto = this.getshipto(); if (this.shipto!= null) { currenthashcode += theshipto.hashcode(); } } // ... return currenthashcode; }
i prefer -xequals
, -xhashcode
generate "strategic" methods. "strategic" in sense these method use passed strategy equality or hash code calculation:
public boolean equals(object object) { final equalsstrategy2 strategy = jaxbequalsstrategy.instance2; return equals(null, null, object, strategy); } public boolean equals(objectlocator thislocator, objectlocator thatlocator, object object, equalsstrategy2 strategy) { if ((object == null)||(this.getclass()!= object.getclass())) { return false; } if (this == object) { return true; } final purchaseordertype = ((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, (this.shipto!= null), (that.shipto!= null))) { return false; } } // ... return true; } public int hashcode() { final hashcodestrategy2 strategy = jaxbhashcodestrategy.instance2; return this.hashcode(null, strategy); } public int hashcode(objectlocator locator, hashcodestrategy2 strategy) { int currenthashcode = 1; { usaddress theshipto; theshipto = this.getshipto(); currenthashcode = strategy.hashcode(locatorutils.property(locator, "shipto", theshipto), currenthashcode, theshipto, (this.shipto!= null)); } // ... return currenthashcode; }
strategic methods quite cool because can customize equality/hash code calculation - instance log structure differ. comes price of runtime dependency.
disclaimer: i'm author of jaxb2 basics.
Comments
Post a Comment