Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails on classes with an array field and at least one other field #27

Closed
GoogleCodeExporter opened this issue Mar 29, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

EqualsVerifier fails on the following class:

private final class ArrayThing {
    private final int id;
    private final byte[] arr;

    public ArrayThing(int id, byte[] arr) {
        this.id = id;
        this.arr = arr;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ArrayThing)) {
            return false;
        }
        ArrayThing other = (ArrayThing)obj;
        return arr == null ? other.arr == null : Arrays.equals(arr, other.arr);
    }

    @Override
    public int hashCode() {
        return arr == null ? 0 : arr.hashCode();
    }

    @Override
    public String toString() {
        return "ArrayThing: " + id + ", " + arr;
    }
}

This happens because two instances of ArrayThing that are supposed to be equal, 
are initialised with different instances of arr.

Original issue reported on code.google.com by jan.ouw...@gmail.com on 11 Jun 2010 at 7:40

@GoogleCodeExporter
Copy link
Author

It turns out the problem was caused by the fact that Arrays.hashCode() should 
have been used instead of the array's own hashCode() method.

EqualsVerifier now catches this error and reports it properly. (It already 
reported for Arrays.equals().)

Original comment by jan.ouw...@gmail.com on 12 Jun 2010 at 10:40

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant