tags:

views:

70

answers:

2

We've generated some (well a lot) of classes using HyperJAXB. All of the classes implement Equals and HashCode and have the implementation style below. Appears this code is never executed.. is there any particular reason we need this code? I'm looking to simplify the classes if we can.

   public boolean equals(Object object) {
        if (!(object instanceof MyClass)) {
            return false;
        }
        if (this == object) {
            return true;
        }
        final EqualsBuilder equalsBuilder = new JAXBEqualsBuilder();
        equals(object, equalsBuilder);
        return equalsBuilder.isEquals();
    }

    public void hashCode(HashCodeBuilder hashCodeBuilder) {
        hashCodeBuilder.append(this.getValue());
        hashCodeBuilder.append(this.getId());
    }

    public int hashCode() {
        final HashCodeBuilder hashCodeBuilder = new JAXBHashCodeBuilder();
        hashCode(hashCodeBuilder);
        return hashCodeBuilder.toHashCode();
    }
+4  A: 

If your application or the JAXB infrastructure ever puts instances of those objects into collections or maps, then there is a good chance that the equals and hashCode methods will be used.

EDIT

I found the following in the hyperjaxb3 documentation:

Generating equals and hashCode methods

Although this is not directly required by the JPA specification, it is still recommended to have correct implementations of equals and hashCode methods in entity classes. [...] With Hyperjaxb3 you can configure, which equals and hash code builders will be used [...] or turn off equals and hashCode generation off.

So it should be possible to turn off generation of those methods, but the hyperjaxb3 designers seem to be saying that it is better not to.

Stephen C
Believe it does put them into a `List`.. but you can store duplicates in all `List`-s can't you? Why would it call equals? And `List`-s don't hash anything do they?
Marcus
If something calls `List.contains(...)`, the `equals` method will be called.
Stephen C
+1  A: 

You can use

<generateEquals>false</generateEquals>
<generateHashCode>false</generateHashCode>

in your maven-hyperjaxb3-plugin configuration if you want to disable equals/hashCode methods generation. However I won't recommend to.

Sincerely yours, "hyperjaxb3 designers". :)

lexicore