Skip to content

Commit 838855b

Browse files
committed
Fixed accidental use of JDK 1.7+ Integer/Long.compare methods
Issue: SPR-11319
1 parent 11bc9d0 commit 838855b

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.util.NumberUtils;
2525

2626
/**
27-
* A simple basic TypeComparator implementation. It supports comparison of numbers and
28-
* types implementing Comparable.
27+
* A simple basic {@link TypeComparator} implementation.
28+
* It supports comparison of Numbers and types implementing Comparable.
2929
*
3030
* @author Andy Clement
3131
* @author Juergen Hoeller
@@ -34,15 +34,29 @@
3434
*/
3535
public class StandardTypeComparator implements TypeComparator {
3636

37+
@Override
38+
public boolean canCompare(Object left, Object right) {
39+
if (left == null || right == null) {
40+
return true;
41+
}
42+
if (left instanceof Number && right instanceof Number) {
43+
return true;
44+
}
45+
if (left instanceof Comparable) {
46+
return true;
47+
}
48+
return false;
49+
}
50+
3751
@Override
3852
@SuppressWarnings("unchecked")
3953
public int compare(Object left, Object right) throws SpelEvaluationException {
4054
// If one is null, check if the other is
4155
if (left == null) {
42-
return right == null ? 0 : -1;
56+
return (right == null ? 0 : -1);
4357
}
4458
else if (right == null) {
45-
return 1; // left cannot be null
59+
return 1; // left cannot be null at this point
4660
}
4761

4862
// Basic number comparisons
@@ -55,48 +69,41 @@ else if (right == null) {
5569
BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
5670
return leftBigDecimal.compareTo(rightBigDecimal);
5771
}
58-
59-
if (leftNumber instanceof Double || rightNumber instanceof Double) {
72+
else if (leftNumber instanceof Double || rightNumber instanceof Double) {
6073
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
6174
}
62-
63-
if (leftNumber instanceof Float || rightNumber instanceof Float) {
75+
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
6476
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
6577
}
66-
67-
if (leftNumber instanceof Long || rightNumber instanceof Long) {
68-
return Long.compare(leftNumber.longValue(), rightNumber.longValue());
78+
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
79+
// Don't call Long.compare here - only available on JDK 1.7+
80+
return compare(leftNumber.longValue(), rightNumber.longValue());
81+
}
82+
else {
83+
// Don't call Integer.compare here - only available on JDK 1.7+
84+
return compare(leftNumber.intValue(), rightNumber.intValue());
6985
}
70-
71-
return Integer.compare(leftNumber.intValue(), rightNumber.intValue());
7286
}
7387

7488
try {
7589
if (left instanceof Comparable) {
76-
return ((Comparable<Object>) left).compareTo(right);
90+
return ((Comparable) left).compareTo(right);
7791
}
78-
} catch (ClassCastException cce) {
79-
throw new SpelEvaluationException(cce, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
92+
}
93+
catch (ClassCastException ex) {
94+
throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
8095
}
8196

8297
throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
8398
}
8499

85-
@Override
86-
public boolean canCompare(Object left, Object right) {
87-
if (left == null || right == null) {
88-
return true;
89-
}
90100

91-
if (left instanceof Number && right instanceof Number) {
92-
return true;
93-
}
94-
95-
if (left instanceof Comparable) {
96-
return true;
97-
}
101+
private static int compare(int x, int y) {
102+
return (x < y ? -1 : (x > y ? 1 : 0));
103+
}
98104

99-
return false;
105+
private static int compare(long x, long y) {
106+
return (x < y ? -1 : (x > y ? 1 : 0));
100107
}
101108

102109
}

0 commit comments

Comments
 (0)