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
The implementation of the SpEL == operator in org.springframework.expression.spel.ast.OpEQ does some special handling for Number types and Comparables, but finally uses left == right.
I think it should always call left.equals(right) - or for null-safety left == null ? right == null : left.equals(right)
This would be consistent with Scala or Groovy, for example.
I just noted that at least all of the standard Java Number subclasses implement Comparable.
So the special code for Number might be completely dropped.
The change sounds fine to me. But the pull request in that other issue to add a floatliteral will likely also fix the 'a float 1.2 would be equal to a short 1' situation.
I recently fell again in the trap described in this issue (with database entity objects instead of numbers).
Since Spring 4.0 aims at a stronger Groovy integration I think it is time to bring this issue back on the table. Thanks.
@Oliver: I'm taking a look at this. I just want to understand your proposal - you want to remove everything in the implementation of OpEq and replace it with:
Object left = getLeftOperand().getValueInternal(state).getValue();
Object right = getRightOperand().getValueInternal(state).getValue();
return BooleanTypedValue.forValue(left==null?right==null:left.equals(right));
This removes the ability for a SpEL consumer to plug in their own type comparator - not necessarily a problem. But if it were removed from here it makes sense to also remove it from the other mathematical operators (otherwise we could get into some weirdness where == returns one answer but is contradicted by the <,<=,>=,> answer for the same operands). We are then left with: if the user needs objects to be comparable they should ensure the classes for those objects implement Comparable.
Removing the whole body of OpEq won't do the right thing for numbers. They may implement Comparable but they don't do the widening for us. Without the special handling will mean checks like this fail "2.0d == 2" which currently work. So I propose under this bug simply to change the == to use .equals in the final clause of the comparison method.
My only hesitation with that is that it disturbs the ordering where right now Comparable is consulted ahead of falling back on equals. Someone may have built a system that depends on it being like that. Maybe they implemented Comparable but didn't wire up equals to use compareTo so it does something different... It may be a bug in their app but the important thing is that the behaviour will change when they update to a new spring level and it may not be obvious why.
Of course you could argue a similar point about switching from == to equals...
If you need the expressions to go fast, you want the JIT that I have in a branch which compiles SpEL expressions to bytecode.
Thanks for the work on it.
I still dont see the point on not calling == and .equals upfront. A comparable should still have a valid equals implementation, right? Also from the performance point of view this would be better i would say.
In the end this pull request should solve the pitfall.
Looks fine to with one exception: the order of Number class checks. BigDecimals won't be compared correctly. I suppose new BigDecimal("1.2") == new BigDecimal("1.3") would return true (fallback to int).
I wonder if both are numbers then why not simply compare double values (for all number types). Would this break anything?
Do you have a unit test that fails with this simplification?
if (left instanceof Number && right instanceof Number) {
Number op1 = (Number) left;
Number op2 = (Number) right;
return (op1.doubleValue() == op2.doubleValue());
}
Basically in an attempt to get the simplest thing in, I did the minimum in the PR - switch from == to .equals(). Given the bug has been open for 18months I thought I'd do something we could just get in and then we can move on. If we want further jiras to explore changing the ordering of trying comparable then equals by promoting equals to be tried first, we can perhaps look at that. I'm pretty sure there are many areas of SpEL that could be optimized.
Does the other big PR related to BigDecimal address what you are describing Oliver? I haven't looked at that one yet, it seemed to include handling of the BigX types everywhere.
@Oliver that alternative version of checking the numbers does behave for the regression tests I have but it will also break down if there is precision loss when the BigDecimal is converted to a double. I suspect proper Big handling as in that other PR is the right thing to do to support these types properly.
Activity
spring-projects-issues commentedon Mar 2, 2012
Oliver Becker commented
I just noted that at least all of the standard Java Number subclasses implement
Comparable.So the special code for
Numbermight be completely dropped.spring-projects-issues commentedon Mar 8, 2012
Chris Beams commented
Added Andy Clement as a watcher.
@Andyplease take a look at this an comment if you have a moment.spring-projects-issues commentedon Aug 6, 2012
Chris Beams commented
ping for comments from
@Andyspring-projects-issues commentedon Aug 10, 2012
Andy Clement commented
The change sounds fine to me. But the pull request in that other issue to add a floatliteral will likely also fix the 'a float 1.2 would be equal to a short 1' situation.
spring-projects-issues commentedon Aug 12, 2012
Chris Beams commented
Thanks, Andy. Linked to #13802 and slating for the general backlog then.
spring-projects-issues commentedon Jun 16, 2013
Oliver Becker commented
I recently fell again in the trap described in this issue (with database entity objects instead of numbers).
Since Spring 4.0 aims at a stronger Groovy integration I think it is time to bring this issue back on the table. Thanks.
spring-projects-issues commentedon Nov 5, 2013
Martin Frey commented
Definately vote up. This is a common pitfall which is not necessary. Jboss EL implements it too, as does thymeleaf.
spring-projects-issues commentedon Nov 6, 2013
Andy Clement commented
@Oliver: I'm taking a look at this. I just want to understand your proposal - you want to remove everything in the implementation of OpEq and replace it with:This removes the ability for a SpEL consumer to plug in their own type comparator - not necessarily a problem. But if it were removed from here it makes sense to also remove it from the other mathematical operators (otherwise we could get into some weirdness where == returns one answer but is contradicted by the <,<=,>=,> answer for the same operands). We are then left with: if the user needs objects to be comparable they should ensure the classes for those objects implement Comparable.
spring-projects-issues commentedon Nov 6, 2013
Andy Clement commented
Or were you just proposing to change the == line to use .equals?
spring-projects-issues commentedon Nov 7, 2013
Andy Clement commented
Removing the whole body of OpEq won't do the right thing for numbers. They may implement Comparable but they don't do the widening for us. Without the special handling will mean checks like this fail "2.0d == 2" which currently work. So I propose under this bug simply to change the == to use .equals in the final clause of the comparison method.
spring-projects-issues commentedon Nov 7, 2013
Martin Frey commented
Why not do the most obvious tests upfront?
After that I would put this code into an utility class and reuse the code from the "not equals" processor and just negate the outcome.
What do you think?
spring-projects-issues commentedon Nov 7, 2013
Andy Clement commented
My only hesitation with that is that it disturbs the ordering where right now Comparable is consulted ahead of falling back on equals. Someone may have built a system that depends on it being like that. Maybe they implemented Comparable but didn't wire up equals to use compareTo so it does something different... It may be a bug in their app but the important thing is that the behaviour will change when they update to a new spring level and it may not be obvious why.
Of course you could argue a similar point about switching from == to equals...
If you need the expressions to go fast, you want the JIT that I have in a branch which compiles SpEL expressions to bytecode.
spring-projects-issues commentedon Nov 7, 2013
Andy Clement commented
Pull request submitted to Spring-Framework: #406
I made the change I proposed but also did as Martin suggested and pulled it up into a common utility method in the superclass for use in == and !=
spring-projects-issues commentedon Nov 7, 2013
Martin Frey commented
Thanks for the work on it.
I still dont see the point on not calling == and .equals upfront. A comparable should still have a valid equals implementation, right? Also from the performance point of view this would be better i would say.
In the end this pull request should solve the pitfall.
Thank you
spring-projects-issues commentedon Nov 8, 2013
Oliver Becker commented
Looks fine to with one exception: the order of Number class checks. BigDecimals won't be compared correctly. I suppose
new BigDecimal("1.2") == new BigDecimal("1.3")would returntrue(fallback to int).I wonder if both are numbers then why not simply compare double values (for all number types). Would this break anything?
Do you have a unit test that fails with this simplification?
spring-projects-issues commentedon Nov 8, 2013
Andy Clement commented
Basically in an attempt to get the simplest thing in, I did the minimum in the PR - switch from == to .equals(). Given the bug has been open for 18months I thought I'd do something we could just get in and then we can move on. If we want further jiras to explore changing the ordering of trying comparable then equals by promoting equals to be tried first, we can perhaps look at that. I'm pretty sure there are many areas of SpEL that could be optimized.
Does the other big PR related to BigDecimal address what you are describing Oliver? I haven't looked at that one yet, it seemed to include handling of the BigX types everywhere.
spring-projects-issues commentedon Nov 8, 2013
Andy Clement commented
@Oliverthat alternative version of checking the numbers does behave for the regression tests I have but it will also break down if there is precision loss when the BigDecimal is converted to a double. I suspect proper Big handling as in that other PR is the right thing to do to support these types properly.spring-projects-issues commentedon Nov 11, 2013
Phil Webb commented
#406