groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject groovy git commit: GROOVY-7709 - ConvertedClosure/ConvertedMap do not work with interface extending GroovyObject (closes #226)
Date Thu, 31 Dec 2015 23:27:22 GMT
Repository: groovy
Updated Branches:
  refs/heads/master a843a40ed -> 0a75ecd61


GROOVY-7709 - ConvertedClosure/ConvertedMap do not work with interface extending GroovyObject
(closes #226)


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

Branch: refs/heads/master
Commit: 0a75ecd613d9873095302fdb911b592a1e995f99
Parents: a843a40
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sat Dec 26 19:17:07 2015 -0800
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Dec 31 15:19:26 2015 -0800

----------------------------------------------------------------------
 .../groovy/runtime/ConversionHandler.java       | 26 +++++++++++++
 src/test/groovy/bugs/Groovy7709Bug.groovy       | 41 ++++++++++++++++++++
 2 files changed, 67 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0a75ecd6/src/main/org/codehaus/groovy/runtime/ConversionHandler.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/ConversionHandler.java b/src/main/org/codehaus/groovy/runtime/ConversionHandler.java
index d628ea1..929642d 100644
--- a/src/main/org/codehaus/groovy/runtime/ConversionHandler.java
+++ b/src/main/org/codehaus/groovy/runtime/ConversionHandler.java
@@ -18,7 +18,11 @@
  */
 package org.codehaus.groovy.runtime;
 
+import groovy.lang.GroovyObject;
 import groovy.lang.GroovyRuntimeException;
+import groovy.lang.GroovySystem;
+import groovy.lang.MetaClass;
+import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
 import org.codehaus.groovy.vmplugin.VMPlugin;
 import org.codehaus.groovy.vmplugin.VMPluginFactory;
 
@@ -45,6 +49,8 @@ public abstract class ConversionHandler implements InvocationHandler, Serializab
         if (VMPluginFactory.getPlugin().getVersion()>=7) handleCache = new ConcurrentHashMap();
     }
 
+    private MetaClass metaClass;
+
     /**
      * Creates a ConversionHandler with an delegate.
      *
@@ -102,6 +108,13 @@ public abstract class ConversionHandler implements InvocationHandler,
Serializab
 
         if (!checkMethod(method)) {
             try {
+                if (method.getDeclaringClass() == GroovyObject.class) {
+                    if ("getMetaClass".equals(method.getName())) {
+                        return getMetaClass(proxy);
+                    } else if ("setMetaClass".equals(method.getName())) {
+                        return setMetaClass((MetaClass) args[0]);
+                    }
+                }
                 return invokeCustom(proxy, method, args);
             } catch (GroovyRuntimeException gre) {
                 throw ScriptBytecodeAdapter.unwrap(gre);
@@ -189,4 +202,17 @@ public abstract class ConversionHandler implements InvocationHandler,
Serializab
         return Object.class.equals(method.getDeclaringClass());
     }
 
+    private MetaClass setMetaClass(MetaClass mc) {
+        metaClass = mc;
+        return mc;
+    }
+
+    private MetaClass getMetaClass(Object proxy) {
+        MetaClass mc = metaClass;
+        if (mc == null) {
+            mc = ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).getMetaClass(proxy);
+            metaClass = mc;
+        }
+        return mc;
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/0a75ecd6/src/test/groovy/bugs/Groovy7709Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy7709Bug.groovy b/src/test/groovy/bugs/Groovy7709Bug.groovy
new file mode 100644
index 0000000..be5e789
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy7709Bug.groovy
@@ -0,0 +1,41 @@
+/*
+ *  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 Groovy7709Bug extends GroovyTestCase {
+
+    void testConvertedClosureAsGroovyObject() {
+        def closure = { 43 }
+        def proxy = closure as Groovy7709BugY
+        assert proxy instanceof GroovyObject
+        assert proxy.foo() == 43
+    }
+
+    void testConvertedMapAsGroovyObject() {
+        def map = [foo: { 43 }]
+        def proxy = map as Groovy7709BugY
+        assert proxy instanceof GroovyObject
+        assert proxy.foo() == 43
+    }
+
+}
+
+interface Groovy7709BugY extends GroovyObject {
+    int foo()
+}


Mime
View raw message