Change SpEL equality operators to use .equals

Prior to this commit the SpEL operators `==` and `!=` were using the
Java `==` comparison operator as part of their equality checking. It is
more flexible to use the equals() method on Object.

Under this commit the change to .equals() has been made and the equality
checking code has been pushed into a common method in the Operator
superclass. This commit also makes some tweaks to the other operator
classes - the Float case was missing from OpGT.

Issue: SPR-9194
This commit is contained in:
Andy Clement
2013-11-07 13:50:40 -08:00
committed by Phillip Webb
parent 7c3cdf82cc
commit 2a05e6afa1
6 changed files with 69 additions and 52 deletions

View File

@@ -1839,6 +1839,19 @@ public class SpelReproTests extends ExpressionTestCase {
equalTo((Object) "name"));
}
@Test
public void testOperatorEq_SPR9194() {
TestClass2 one = new TestClass2("abc");
TestClass2 two = new TestClass2("abc");
Map<String,TestClass2> map = new HashMap<String,TestClass2>();
map.put("one",one);
map.put("two",two);
SpelExpressionParser parser = new SpelExpressionParser();
Expression classNameExpression = parser.parseExpression("['one'] == ['two']");
assertTrue(classNameExpression.getValue(map,Boolean.class));
}
private static enum ABC {A, B, C}
@@ -1922,4 +1935,20 @@ public class SpelReproTests extends ExpressionTestCase {
}
}
static class TestClass2 { // SPR-9194
String string;
public TestClass2(String string) {
this.string = string;
}
public boolean equals(Object o) {
if (o instanceof TestClass2) {
return string.equals(((TestClass2)o).string);
}
return false;
}
}
}