1

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!

Mueller2016
  • 195
  • 1
  • 9
  • Possible duplicate of [Generating hashCode() and equals() when creating Java classes using Mojo Jaxb2 maven plugin](https://stackoverflow.com/questions/9062539/generating-hashcode-and-equals-when-creating-java-classes-using-mojo-jaxb2-m) – glytching Sep 14 '17 at 07:16
  • the solution in this question was to use the argument: -Xequals -XhashCode If i try this i get the following Exception: unbekannter Parameter -Xequals -XhashCode I think the difference between the 2 Questions is the Version. – Mueller2016 Sep 14 '17 at 08:23

1 Answers1

1

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 2
    Small update: in version 1.11.1 arguments were renamed with `-Xequals -XhashCode` – petronius Sep 02 '19 at 15:55
  • @petronius There's no version 1.11.1 - it was released by mistake. Official version is 0.11.1. – lexicore Sep 03 '19 at 07:13
  • @petronius Also, arguments were *not* renamed. – lexicore Sep 03 '19 at 07:14
  • I don't get it, maven repo 1.11.1 do exist: https://mvnrepository.com/artifact/org.jvnet.jaxb2_commons/jaxb2-basics (as well as 1.11.1-PUBLISHED-BY-MISTAKE). – petronius Sep 04 '19 at 09:49
  • To make it work, I had to write `-Xequals` and `-XhashCode`, because with `-XsimpleEquals` I got the error: `[ERROR] Failed to execute goal [...] An API incompatibility was encountered while executing org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0:generate: java.lang.NoSuchMethodError: org.jvnet.jaxb2_commons.plugin.util.StrategyClassUtils.superClassNotIgnored(Lcom/sun/tools/xjc/outline/ClassOutline;Lorg/jvnet/jaxb2_commons/plugin/Ignoring;)Ljava/lang/Boolean;` Using maven-jaxb2-plugin 0.14.0. – petronius Sep 04 '19 at 09:54
  • @petronius `1.11.1` was released by mistake. I - the author - consider it therefore to be non-existent. I can't remove it from the Maven central repo, though. – lexicore Sep 04 '19 at 10:19
  • @petronius `-Xequals` vs. `-XsimpleEquals` are two separate plugins. (Same for `-XhashCode` vs. `-XsimpleEquals`.) So nothing was renamed. – lexicore Sep 04 '19 at 10:21