Details
-
Type:
Sub-task
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: 1.5.7, 1.6-rc-3
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Testcase included:yes
-
Patch Submitted:Yes
-
Number of attachments :
Description
Once a class implements Comparable, the == operator does not work anymore!
class X implements Comparable { int compareTo(Object object) { return 0 } boolean equals(Object object) { return true; } } public void testEqualityOperator() { def x = new X() def y = new Y() assert x == x // OK assert x == y // OK assert x == 1 // FAIL - Neither compareTo() nor equals() is called! }
The location of the bug is in
DefaultTypeTransform.compareToWithEqualityCheck(Object left, Object right, boolean equalityCheckOnly): if (equalityCheckOnly) { return -1; // anything other than 0 }
The fix is:
if (equalityCheckOnly) {
return ((Boolean) InvokerHelper.invokeMethod(left, "equals", right)).booleanValue() ? 0 : -1;
}
An even better fix would be to always use equals for the == operator - but this is already discussed in other issues.
I've set this to a BLOCKER beause this issue leads to the fact that I cannot use Groovy for relaxed typing - and that makes it useless for many DSLs!
I added some code text to make the issue more readable.
Then about the issue itself... we have here a case where the left side is implementing Comparable, but the right side is not... that also means that left and right implement different types, unrelated ones on top of that. Currently we have defined, that if the left side is a Comparable and the right side is not, that those two can never be equal. It is kind of strange to go to compareTo, but then to fall back to equals. On top of that a usual implementation of equals returns false if the types are not equal enough... comparing itself with an Integer in equals will lead in any normal java class to false.
If anything then I would mark this report as improvement, because you want to "advance" the current logic. But frankly I would like to know more about your use case, because you are doing things here, that are a bit "out of spec".
Also I wonder why the issue got this many votes, when I hear the first time about that part of the logic being a problem. So I guess they would have some reasons for having such a change and I would like to hear these