groovy git commit: GROOVY-8764: Closure inside an anonymous class uses wrong outside class instance with @CS (additional case)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: GROOVY-8764: Closure inside an anonymous class uses wrong outside class instance with @CS (additional case)

paulk
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X eee7844bb -> fd21ad4e3


GROOVY-8764: Closure inside an anonymous class uses wrong outside class instance with @CS (additional case)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: fd21ad4e3b6d2a4297944d079ba8948e8d8e6052
Parents: eee7844
Author: Paul King <[hidden email]>
Authored: Wed Oct 10 14:06:17 2018 +1000
Committer: Paul King <[hidden email]>
Committed: Wed Oct 10 14:06:17 2018 +1000

----------------------------------------------------------------------
 .../classgen/asm/sc/StaticInvocationWriter.java |  2 +-
 src/test/groovy/bugs/Groovy8764Bug.groovy       | 26 +++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/fd21ad4e/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
index d480578..abab966 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
@@ -380,7 +380,7 @@ public class StaticInvocationWriter extends InvocationWriter {
                     ClassNode current = classNode.getOuterClass();
                     fixedReceiver = new VariableExpression("thisObject", current);
                     // adjust for multiple levels of nesting if needed
-                    while (current instanceof InnerClassNode && !classNode.equals(current)) {
+                    while (current instanceof InnerClassNode && !target.getDeclaringClass().equals(current)) {
                         FieldNode thisField = current.getField("this$0");
                         current = current.getOuterClass();
                         if (thisField != null) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/fd21ad4e/src/test/groovy/bugs/Groovy8764Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8764Bug.groovy b/src/test/groovy/bugs/Groovy8764Bug.groovy
index 7248667..a092574 100644
--- a/src/test/groovy/bugs/Groovy8764Bug.groovy
+++ b/src/test/groovy/bugs/Groovy8764Bug.groovy
@@ -19,7 +19,7 @@
 package groovy.bugs
 
 class Groovy8764Bug extends GroovyTestCase {
-    void testDgmMethodInClosureInInnerClass() {
+    void testDgmMethodInClosureInAnonymousInnerClass() {
         assertScript '''
             import groovy.transform.*
 
@@ -41,4 +41,28 @@ class Groovy8764Bug extends GroovyTestCase {
             assert gt.hasMeta
         '''
     }
+
+    void testDgmMethodInClosureInInnerClass() {
+        assertScript '''
+        import groovy.transform.*
+        import java.util.function.Function
+
+            @CompileStatic
+            class Outer {
+                static class Inner {
+                    List<Optional<String>> pets = [Optional.of('goldfish'), Optional.of('cat')]
+                    Optional<Integer> test(int index) {
+                        pets[index].flatMap({ String s ->
+                            // sprintf is a DGM method on Object
+                            sprintf('%s', s).size() == 3 ? Optional.of(index) : Optional.empty()
+                        } as Function<String, Optional<Integer>>)
+                    }
+                }
+            }
+
+            def oi = new Outer.Inner()
+            assert !oi.test(0)
+            assert oi.test(1).value == 1
+        '''
+    }
 }