Skip to content

Commit

Permalink
Merge pull request orphan-oss#78 from peteruhnak/master
Browse files Browse the repository at this point in the history
Compare non-comparable objects by equals only

(cherry picked from commit 937fd07)
  • Loading branch information
lukaszlenart authored and JCgH4164838Gh792C124B5 committed Sep 29, 2019
1 parent e4cc4d5 commit f921376
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/java/ognl/OgnlOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public static boolean isEqual(Object object1, Object object2)

if (object1 == object2) {
result = true;
} else {
if ((object1 != null) && object1.getClass().isArray()) {
if ((object2 != null) && object2.getClass().isArray() && (object2.getClass() == object1.getClass())) {
} else if (object1 != null && object2 != null) {
if (object1.getClass().isArray()) {
if (object2.getClass().isArray() && (object2.getClass() == object1.getClass())) {
result = (Array.getLength(object1) == Array.getLength(object2));
if (result) {
for(int i = 0, icount = Array.getLength(object1); result && (i < icount); i++) {
Expand All @@ -139,9 +139,15 @@ public static boolean isEqual(Object object1, Object object2)
}
}
} else {
// Check for converted equivalence first, then equals() equivalence
result = (object1 != null) && (object2 != null)
&& (object1.equals(object2) || (compareWithConversion(object1, object2) == 0));
int t1 = getNumericType(object1);
int t2 = getNumericType(object2);

// compare non-comparable non-numeric types by equals only
if (t1 == NONNUMERIC && t2 == NONNUMERIC && (!(object1 instanceof Comparable) || !(object2 instanceof Comparable))) {
result = object1.equals(object2);
} else {
result = compareWithConversion(object1, object2) == 0;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public class ArithmeticAndLogicalOperatorsTest extends OgnlTestCase
{ "1 shl 2", new Integer(4) }, { "4 shr 2", new Integer(1) }, { "4 ushr 2", new Integer(1) },
{ "not null", Boolean.TRUE }, { "not 1", Boolean.FALSE },

// Equality on identity; Object does not implement Comparable
{ "#a = new java.lang.Object(), #a == #a", Boolean.TRUE},
{ "#a = new java.lang.Object(), #b = new java.lang.Object(), #a == #b", Boolean.FALSE},

// Comparable and non-Comparable
{ "#a = new java.lang.Object(), #a == ''", Boolean.FALSE},
{ "#a = new java.lang.Object(), '' == #a", Boolean.FALSE},

{ "#x > 0", Boolean.TRUE },
{ "#x < 0", Boolean.FALSE },
{ "#x == 0", Boolean.FALSE },
Expand Down

0 comments on commit f921376

Please sign in to comment.