Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X dbb665df2 -> 4b4a286e0


GROOVY-7330: Incorrect dynamic proxy creation from map when there are default 
methods (closes #785)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 50256d86205932d4fbf6c7586d9eae7836d8dd85
Parents: dbb665d
Author: Paul King <[email protected]>
Authored: Fri Aug 17 17:49:50 2018 +1000
Committer: Paul King <[email protected]>
Committed: Sun Aug 19 17:00:47 2018 +1000

----------------------------------------------------------------------
 .../groovy/runtime/ConversionHandler.java       | 10 ++--
 .../runtime/InterfaceConversionMapTest.java     | 54 ++++++++++++++++++++
 2 files changed, 60 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/50256d86/src/main/java/org/codehaus/groovy/runtime/ConversionHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/ConversionHandler.java 
b/src/main/java/org/codehaus/groovy/runtime/ConversionHandler.java
index 8bf7c69..bed1a48 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ConversionHandler.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ConversionHandler.java
@@ -32,14 +32,12 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Proxy;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * This class is a general adapter to map a call to a Java interface
  * to a given delegate.
- *
- * @author Ben Yu
- * @author <a href="mailto:[email protected]";>Jochen Theodorou</a>
  */
 public abstract class ConversionHandler implements InvocationHandler, 
Serializable {
     private final Object delegate;
@@ -102,7 +100,7 @@ public abstract class ConversionHandler implements 
InvocationHandler, Serializab
      * @see InvocationHandler#invoke(java.lang.Object, 
java.lang.reflect.Method, java.lang.Object[])
      */
     public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
-        if (handleCache != null && isDefaultMethod(method)) {
+        if (handleCache != null && isDefaultMethod(method) && 
!defaultOverridden(method)) {
             VMPlugin plugin = VMPluginFactory.getPlugin();
             Object handle = handleCache.get(method);
             if (handle == null) {
@@ -134,6 +132,10 @@ public abstract class ConversionHandler implements 
InvocationHandler, Serializab
         }
     }
 
+    private boolean defaultOverridden(Method method) {
+        return delegate instanceof Map && ((Map) 
delegate).containsKey(method.getName());
+    }
+
     protected boolean isDefaultMethod(Method method) {
         return ((method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC 
| Modifier.STATIC)) ==
                 Modifier.PUBLIC) && method.getDeclaringClass().isInterface();

http://git-wip-us.apache.org/repos/asf/groovy/blob/50256d86/src/test/org/codehaus/groovy/runtime/InterfaceConversionMapTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/org/codehaus/groovy/runtime/InterfaceConversionMapTest.java 
b/src/test/org/codehaus/groovy/runtime/InterfaceConversionMapTest.java
new file mode 100644
index 0000000..5c67bd0
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/InterfaceConversionMapTest.java
@@ -0,0 +1,54 @@
+/*
+ *  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 org.codehaus.groovy.runtime;
+
+import groovy.lang.GroovyShell;
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+
+public class InterfaceConversionMapTest {
+
+    private final GroovyShell shell;
+
+    public InterfaceConversionMapTest() {
+        final CompilerConfiguration cc = new CompilerConfiguration();
+        cc.setTargetBytecode(CompilerConfiguration.JDK8);
+        shell = new GroovyShell(cc);
+    }
+
+    // GROOVY-7330
+    @Test
+    public void testMapToProxy() {
+        final Map map = (Map) shell.evaluate("[x: {10}, y: {20}]");
+        final SomeInterface si = DefaultGroovyMethods.asType(map, 
SomeInterface.class);
+        Assert.assertEquals(20, si.y());
+        Assert.assertEquals(10, si.x());
+    }
+
+    public interface SomeInterface {
+        default int x() {
+            return 1;
+        }
+
+        int y();
+    }
+}

Reply via email to