You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ClassCastException when evaluating the eq/ne operator in the EL [SPR-11708] #16330
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
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.
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.
Activity
spring-projects-issues commentedon Apr 18, 2014
Oliver Becker commented
The use of
Comparablemight be a compatibility issue. See discussion in #13832spring-projects-issues commentedon Apr 21, 2014
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