我们使用jaxb2-maven-plugin (版本2.2),并为每个hashCode生成相等和JaxbObject方法。我们已经分配了一个binding.xjb文件来配置任何东西。
有任何方法来生成这个方法吗?
如果我尝试添加参数-Xequals -XhashCode,则会得到以下异常: unbekannter -Xequals -XhashCode
配置:
<configuration> <arguments>-Xequals -XhashCode</arguments> </configuration>谢谢!
发布于 2017-09-15 20:22:31
您可以使用hashCode插件生成JAXB2基础和equals:
<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>这将生成深度无反射的运行时无关依赖的equals和hashCode方法:
public boolean equals(Object object) {
if ((object == null)||(this.getClass()!= object.getClass())) {
return false;
}
if (this == object) {
return true;
}
final PurchaseOrderType that = ((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;
}我个人更喜欢生成“战略性”方法的-Xequals和-XhashCode。“策略”在某种意义上说,这些方法使用传递的策略进行相等或哈希码计算:
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 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, (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;
}策略方法非常酷,因为您可以自定义等式/哈希代码计算--例如,在结构不同的日志中。这是以运行时依赖为代价的。
免责声明:,我是JAXB2基础的作者。
https://stackoverflow.com/questions/46212667
复制相似问题