Generate Equals und HashCode with jaxb2-maven-plugin Version 2.2

3.2k Views Asked by At

We use the jaxb2-maven-plugin (Version 2.2) and we would generate the equal and hashCode Method for each JaxbObject. We have alreade a binding.xjb File to configure anything.

Is there any way to generate this Methods?

If I try to add the arguments -Xequals -XhashCode, i get the following Exception: unbekannter Parameter -Xequals -XhashCode

Configuration:

<configuration> <arguments>-Xequals -XhashCode</arguments> </configuration>

Thank you!

1

There are 1 best solutions below

7
On

You can generate hashCode and equals with 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 will generate deep reflection-free runtime dependencies-free equals and hashCode methods:

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;
}

I personally prefer -Xequals and -XhashCode which generate "strategic" methods. "Strategic" in a sense these method use a passed strategy for 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 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;
}

Strategic methods are quite cool because you can customize equality/hash code calculation - for instance log where exactly structure differ. This comes for a price of runtime dependency.

Disclaimer: I'm the author of JAXB2 Basics.