javaee / jaxb2-commons

JAXB Implementation project has been contributed to Eclipse Foundation. This repository is for legacy review only. Please refer to the Eclipse EE4J Metro project for the very latest
https://eclipse-ee4j.github.io/jaxb-ri/
Other
17 stars 15 forks source link

JAXBEqualsStrategy.equals() is not symmetric when an Enum and a Concrete class are compared #37

Closed glassfishrobot closed 8 years ago

glassfishrobot commented 9 years ago

The method JAXBEqualsStrategy.equals(ObjectLocator leftLocator, ObjectLocator rightLocator, Object lhs, Object rhs) is not symmetric when comparing an Enum and a Concrete class, thus it violates the contract of Object.equals().

If lhs is an Enum and rhs is not an Enum, a ClassCastException to java.lang.Enum is thrown. If rhs is not an enum and lhs is an Enum, no exception is thrown and the method correctly compares lhs and rhs. I observed this behaviour in 0.6.5 and verified that it also occurs in the latest released version 0.9.2.

public class EqualsStrategyTest {
    @Test
    public void testJAXBEqualsSymmetryConcreteClassAndEnum() {
        assertFalse(JAXBEqualsStrategy.INSTANCE.equals(null, null, new SomeConcreteClass(), SomeEnum.ENUM));
    }

    @Test
    public void testJAXBEqualsSymmetryEnumAndConcreteClass() {
        // This test fails and throws a ClassCastException      assertFalse(JAXBEqualsStrategy.INSTANCE.equals(null, null, SomeEnum.ENUM, new SomeConcreteClass()));
    }

    private static class SomeConcreteClass {
    }

    private enum SomeEnum {
        ENUM;
    }
}

The problem is in DefaultEqualsStrategy.equalsInternal(ObjectLocator leftLocator, ObjectLocator rightLocator, Object lhs, Object rhs):

if (lhs instanceof Enum<?>) {
            return equalsInternal(leftLocator, rightLocator, (Enum<?>) lhs, (Enum<?>) rhs);
        }

If the left hand side of the equals is an Enum, we assume that the right is an Enum as well, which is incorrect in the example case, resulting in the ClassCastException.

Environment

java8u31, using maven

Affected Versions

[current]

glassfishrobot commented 9 years ago

Reported by JoepWeijers

glassfishrobot commented 8 years ago

lexi said: Fixed in https://github.com/highsource/jaxb2-basics/issues/22.

glassfishrobot commented 9 years ago

Was assigned to super_glassfish

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JAXB2_COMMONS-37

glassfishrobot commented 8 years ago

Marked as fixed on Thursday, December 24th 2015, 12:59:31 am