You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/12/13 19:43:10 UTC

[groovy] branch master updated: GROOVY-3493: MetaMethodIndex: closure method overrides interface method

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 55fb428  GROOVY-3493: MetaMethodIndex: closure method overrides interface method
55fb428 is described below

commit 55fb428c4f6dfad37be5f752282a97089cb1b5ad
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 13 13:42:59 2021 -0600

    GROOVY-3493: MetaMethodIndex: closure method overrides interface method
---
 .../groovy/runtime/metaclass/MetaMethodIndex.java  |  5 +-
 src/test/groovy/bugs/Groovy2391.groovy             | 62 ++++++++++++++++++++++
 src/test/groovy/bugs/Groovy2391Bug.groovy          | 36 -------------
 3 files changed, 65 insertions(+), 38 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaMethodIndex.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaMethodIndex.java
index 154253e..e7dba41 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaMethodIndex.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaMethodIndex.java
@@ -412,8 +412,9 @@ public class MetaMethodIndex {
         }
 
         // interface vs instance method; be careful...
-        if (!inIndex.isStatic() && !toIndex.isStatic()
-                && inIndexDC.isInterface() != toIndexDC.isInterface()) {
+        if (!inIndex.isStatic() && !toIndex.isStatic() // GROOVY-9815
+                && inIndexDC.isInterface() != toIndexDC.isInterface()
+                && !(toIndex instanceof ClosureMetaMethod || toIndex instanceof ClosureStaticMetaMethod)) { // GROOVY-3493
             // this is the old logic created for GROOVY-2391 and GROOVY-7879, which was labeled as "do not overwrite interface methods with instance methods"
             return (isNonRealMethod(inIndex) || !inIndexDC.isInterface() || toIndexDC.isInterface()) && !toIndexDC.isAssignableFrom(inIndexDC.getTheClass());
         }
diff --git a/src/test/groovy/bugs/Groovy2391.groovy b/src/test/groovy/bugs/Groovy2391.groovy
new file mode 100644
index 0000000..dcc24f9
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy2391.groovy
@@ -0,0 +1,62 @@
+/*
+ *  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
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy2391 {
+
+    @Test
+    void testOverrideExtensionMethod() {
+        assertScript '''
+            ArrayList.metaClass.asType = { Class c ->
+                if (c.isInstance(delegate[1])) {
+                    return delegate[1]
+                }
+                assert false
+            }
+            ArrayList.metaClass.initialize()
+            try {
+                String result = [1,"Two",3] as String
+                assert result == "Two"
+            } finally {
+                GroovySystem.metaClassRegistry.removeMetaClass(ArrayList)
+            }
+        '''
+    }
+
+    @Test // GROOVY-3493
+    void testOverrideOverriddenMethod() {
+        assertScript '''
+            interface I {
+                def m()
+            }
+            class C implements I {
+                def m() { 'C' }
+            }
+
+            x = new C()
+            assert x.m() == 'C'
+            x.metaClass.m = { -> 'Override' }
+            assert x.m() == 'Override'
+        '''
+    }
+}
diff --git a/src/test/groovy/bugs/Groovy2391Bug.groovy b/src/test/groovy/bugs/Groovy2391Bug.groovy
deleted file mode 100644
index 6cf4d60..0000000
--- a/src/test/groovy/bugs/Groovy2391Bug.groovy
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- *  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
-
-import groovy.test.GroovyTestCase
-
-class Groovy2391Bug extends GroovyTestCase{
-    void testBug () {
-      ArrayList.metaClass.asType = { Class clazz ->
-          if (clazz.isInstance(delegate[1]))
-            return delegate[1]
-          fail ()
-      }
-      ArrayList.metaClass.initialize()
-
-      assertEquals("Boom", [1,"Boom",3] as String)
-
-      GroovySystem.metaClassRegistry.removeMetaClass ArrayList
-    }
-}
\ No newline at end of file