groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-7876: ClassCastException when calling DefaultTypeTransformation#compareEqual (closes #368)
Date Thu, 21 Jul 2016 23:30:33 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 858a6fd2e -> 9159673d6


GROOVY-7876: ClassCastException when calling DefaultTypeTransformation#compareEqual (closes
#368)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9159673d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9159673d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9159673d

Branch: refs/heads/master
Commit: 9159673d6f9e12dc4eefcd9bfffa2c030ce1748f
Parents: 858a6fd
Author: paulk <paulk@asert.com.au>
Authored: Tue Jul 19 22:02:01 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Jul 22 09:29:28 2016 +1000

----------------------------------------------------------------------
 .../typehandling/DefaultTypeTransformation.java |  9 ++++-
 src/test/groovy/bugs/Groovy7876Bug.groovy       | 39 ++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9159673d/src/main/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
b/src/main/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
index a0d53a2..41484d8 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
@@ -582,7 +582,14 @@ public class DefaultTypeTransformation {
                     || (right.getClass() != Object.class && right.getClass().isAssignableFrom(left.getClass()))
//GROOVY-4046
                     || (left instanceof GString && right instanceof String)) {
                 Comparable comparable = (Comparable) left;
-                return comparable.compareTo(right);
+                // GROOVY-7876: when comparing for equality we try to only call compareTo
when an assignable
+                // relationship holds but with a container/holder class and because of erasure,
we might still end
+                // up with the prospect of a ClassCastException which we want to ignore but
only if testing equality
+                try {
+                    return comparable.compareTo(right);
+                } catch (ClassCastException cce) {
+                    if (!equalityCheckOnly) throw cce;
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/9159673d/src/test/groovy/bugs/Groovy7876Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy7876Bug.groovy b/src/test/groovy/bugs/Groovy7876Bug.groovy
new file mode 100644
index 0000000..5ec368e
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy7876Bug.groovy
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package groovy.bugs
+
+class Groovy7876Bug extends GroovyTestCase {
+    void testClassCastExceptionsFromCompareToShouldNotLeakOutOfEqualityCheck() {
+        assertScript '''
+            enum E1 {A, B, C}
+            enum E2 {D, E, F}
+            class Holder<T> implements Comparable<T> {
+                final T thing
+                Holder(T thing) { this.thing = thing }
+                int compareTo(T other) { thing.compareTo(other.thing) }
+            }
+            def a = new Holder<E1>(E1.A)
+            def d = new Holder<E2>(E2.D)
+            assert E1.A != E2.D // control
+            assert a != d // invokes compareTo
+        '''
+    }
+}


Mime
View raw message