groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [2/2] groovy git commit: GROOVY-8881: Inner class - qualified "this" reference cast exception inside 2 or more closures
Date Fri, 16 Nov 2018 10:50:16 GMT
GROOVY-8881: Inner class - qualified "this" reference cast exception inside 2 or more closures


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 4912730ba1643644b989208e604195871da37e49
Parents: eaae558
Author: Paul King <paulk@asert.com.au>
Authored: Fri Nov 16 20:49:09 2018 +1000
Committer: Paul King <paulk@asert.com.au>
Committed: Fri Nov 16 20:50:04 2018 +1000

----------------------------------------------------------------------
 .../groovy/classgen/AsmClassGenerator.java      |  4 ++
 src/test/groovy/bugs/Groovy8881Bug.groovy       | 47 ++++++++++++++++++++
 2 files changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/4912730b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 5390614..cd200ff 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -1061,6 +1061,10 @@ public class AsmClassGenerator extends ClassGenerator {
                 iterType = iterType.getOuterClass();
                 if (thisField == null) {
                     // closure within inner class
+                    while (iterType.isDerivedFrom(ClassHelper.CLOSURE_TYPE)) {
+                        // GROOVY-8881: cater for closures within closures - getThisObject
is already outer class of all closures
+                        iterType = iterType.getOuterClass();
+                    }
                     mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE),
"getThisObject", "()Ljava/lang/Object;", false);
                     mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
                 } else {

http://git-wip-us.apache.org/repos/asf/groovy/blob/4912730b/src/test/groovy/bugs/Groovy8881Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8881Bug.groovy b/src/test/groovy/bugs/Groovy8881Bug.groovy
new file mode 100644
index 0000000..0e1d2e2
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8881Bug.groovy
@@ -0,0 +1,47 @@
+/*
+ *  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 Groovy8881Bug extends GroovyTestCase {
+    void testClosureWithinClosureInInnerClassReferencesThisCorrectly() {
+        assertScript '''
+            class OuterClass {
+                class InnerClass {
+                    def innerMethod() {
+                        Closure closureOne = {
+                            Closure closureTwo = {
+                                'outer=' + OuterClass.this.class.name + ', inner=' + InnerClass.this.class.name
+                            }
+                            return closureTwo.call()
+                        }
+                        return closureOne.call()
+                    }
+                }
+
+                void doit() {
+                    assert new InnerClass().innerMethod() == 'outer=OuterClass, inner=OuterClass$InnerClass'
+                }
+
+                static void main(args) {
+                    new OuterClass().doit()
+                }
+            }
+        '''
+    }
+}


Mime
View raw message