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

Non-compliant with Java spec: "The hashCode method should return different hash codes for non equal objects." #250

Open
pafeu81 opened this issue Dec 29, 2020 · 1 comment

Comments

@pafeu81
Copy link

pafeu81 commented Dec 29, 2020

The code given below is a valid POJO. Java POJOs are allowed to return same hashCode() for non-equal objects. Might not be optimal, but it's valid and actually fairly common situation - see https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

However for a simple pojo listed below pojo-tester (v 0.7.6) throws:

Class (...)MyPojoTest.MyPojo has bad 'hashCode' method implementation.
The hashCode method should return different hash codes for non equal objects.

The test code:

package com.my.pojo;
import org.junit.jupiter.api.Test;
import java.util.Objects;
import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor;

// hashCode() and equals() generated with IntelliJ
class MyPojoTest {

    public static class MyPojo {
        private String name;
        private int age;

        public String getName() {  return name;   }
        public void setName(String name) {  this.name = name; }

        public int getAge() {    return age;  }
        public void setAge(int age) { this.age = age;  }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            MyPojo myPojo = (MyPojo) o;
            return age == myPojo.age &&
                    Objects.equals(name, myPojo.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }

    @Test
    public void testMyPojo() {
        assertPojoMethodsFor(MyPojo.class).areWellImplemented();
    }
}

@Hitesh-Kirtane
Copy link

This error is thrown when we try to use any type of collection object as a member variable inside our POJO class.

This issue is still not fixed in this repository and there is a forked project being updated - https://github.com/obsidiandynamics/pojo-tester also has the same issue which makes this hashcode testing feature of the project unusable currently.

Since this project is now on hold, I hope the maintainer gets good collaborative support & this project to be resumed soon along with fixes for this issue.

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

No branches or pull requests

2 participants