Skip to content

ClassCastException when evaluating the eq/ne operator in the EL [SPR-11708] #16330

Closed

Description

@spring-projects-issues

Oliver Becker opened SPR-11708 and commented

I suppose evaluating '=' or '!=' should never ever throw a ClassCastException. However, this happens with comparable operands of different types.

Example: 10 != 'ten' should evaluate to true IMO. But it doesn't. To check for yourself, add the following line to the corresponding test OperatorTests.testNotEqual()

evaluate("10 ne '10'", true, Boolean.class);

The cause is the call to compare(..) if the first operand is comparable.
See class org.springframework.expression.spel.ast.Operator, method equalityCheck. Below the number handling, in line 99 the code contains the following three lines

if (left != null && (left instanceof Comparable)) {
	return (state.getTypeComparator().compare(left, right) == 0);
}

Simply removing these lines would fix the bug. Moreover, there is no failing test case after doing so, I'm not sure what kind of use case has been in mind when adding these lines to the evaluation of equals.

Alternatively catching the exception would solve the problem, too. But if you do so then please add a test that shows the necessity for using the compare logic.

My vote is for removing the cited code snippet.


Affects: 4.0.3

Issue Links:

Activity

  1. spring-projects-issues commented on Apr 18, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    The use of Comparable might be a compatibility issue. See discussion in #13832

  2. spring-projects-issues commented on Apr 21, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I've refined the Comparable check to only apply if there is a common superclass that implements Comparable. Additionally, this is just a fallback check now; we're always checking the (more efficient) nullSafeEquals first.

    While being at it, I've also added a special check for CharSequence arguments to consider them equal if their toString() results are equal. This implies that we're never doing a Comparable fallback check for a String pair, since we know that String.compareTo matches the equals impl.

    Juergen

  3. added
    type: bug
    in: core
    on Jan 11, 2019
  4. added this to the 4.0.4 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions