首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jaxb2-maven-plugin版本2.2生成等于und HashCode

使用jaxb2-maven-plugin版本2.2生成等于und HashCode
EN

Stack Overflow用户
提问于 2017-09-14 07:08:58
回答 1查看 2.3K关注 0票数 1

我们使用jaxb2-maven-plugin (版本2.2),并为每个hashCode生成相等和JaxbObject方法。我们已经分配了一个binding.xjb文件来配置任何东西。

有任何方法来生成这个方法吗?

如果我尝试添加参数-Xequals -XhashCode,则会得到以下异常: unbekannter -Xequals -XhashCode

配置:

代码语言:javascript
复制
<configuration> <arguments>-Xequals -XhashCode</arguments> </configuration>

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-09-15 20:22:31

您可以使用hashCode插件生成JAXB2基础equals

代码语言:javascript
复制
<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>

这将生成深度无反射的运行时无关依赖的equalshashCode方法:

代码语言:javascript
复制
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。“策略”在某种意义上说,这些方法使用传递的策略进行相等或哈希码计算:

代码语言:javascript
复制
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基础的作者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46212667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档