java equals on recursive apache commons 1

java equals on recursive apache commons

public void testRefEqu() throws IOException {

    dummy inst1 = new dummy();
    inst1.nestedClass = new dummy2();
    inst1.nestedClass.intVal = 2;

    dummy inst2 = new dummy();
    inst2.nestedClass = new dummy2();
    inst2.nestedClass.intVal = 2;

    boolean isEqual1 = EqualsBuilder.reflectionEquals(inst1.nestedClass, inst2.nestedClass);
    boolean isEqual2 = EqualsBuilder.reflectionEquals(inst1, inst2);

Here is what the above code is Doing:
1. Create two instances of dummy class.
2. Create two instances of dummy2 class.
3. Set the nestedClass field of dummy class to the dummy2 class instances.
4. Set the intVal field of dummy2 class to 2.
5. Call reflectionEquals() on the dummy2 class instances.
6. Call reflectionEquals() on the dummy class instances.

The first call to reflectionEquals() returns true, but the second call returns false.

I’m not sure if this is a bug or if I’m doing something wrong.




I think you are doing something wrong.
EqualsBuilder.reflectionEquals(inst1.nestedClass, inst2.nestedClass);

This is comparing the two instances of dummy2.
EqualsBuilder.reflectionEquals(inst1, inst2);

This is comparing the two instances of dummy.
The two instances of dummy are not equal because they have different instances of dummy2.

Similar Posts