Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

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

Closed
glassfishrobot opened this issue Feb 20, 2015 · 5 comments

Comments

@glassfishrobot
Copy link
Contributor

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
Copy link
Contributor Author

Reported by JoepWeijers

@glassfishrobot
Copy link
Contributor Author

Was assigned to super_glassfish

@glassfishrobot
Copy link
Contributor Author

lexi said:
Fixed in highsource/jaxb2-basics#22.

@glassfishrobot
Copy link
Contributor Author

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

@glassfishrobot
Copy link
Contributor Author

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

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

No branches or pull requests

1 participant