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 2019/12/19 01:40:14 UTC

[groovy] branch GROOVY-9342 created (now be25d28)

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

emilles pushed a change to branch GROOVY-9342
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at be25d28  GROOVY-9342: load static "this" type for static access of properties

This branch includes the following new commits:

     new be25d28  GROOVY-9342: load static "this" type for static access of properties

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9342: load static "this" type for static access of properties

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit be25d286951d4eb28ae654367671fbf10baa9b1d
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Dec 18 19:30:11 2019 -0600

    GROOVY-9342: load static "this" type for static access of properties
---
 .../org/codehaus/groovy/classgen/asm/WriterController.java |  5 ++---
 .../groovy/classgen/asm/sc/StaticTypesLambdaWriter.java    | 12 ++++++++----
 src/test/groovy/transform/stc/LambdaTest.groovy            | 14 +++++++++++++-
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
index 1db4a89..6eae8ab 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
@@ -19,7 +19,6 @@
 package org.codehaus.groovy.classgen.asm;
 
 import org.codehaus.groovy.GroovyBugError;
-import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.ConstructorNode;
 import org.codehaus.groovy.ast.InterfaceHelperClassNode;
@@ -41,6 +40,7 @@ import java.util.List;
 import java.util.Map;
 
 import static org.apache.groovy.util.SystemUtil.getBooleanSafe;
+import static org.codehaus.groovy.ast.ClassHelper.isGeneratedFunction;
 
 public class WriterController {
 
@@ -283,8 +283,7 @@ public class WriterController {
     }
 
     public boolean isInClosure() {
-        return classNode.getOuterClass() != null
-                && ClassHelper.isGeneratedFunction(classNode);
+        return classNode.getOuterClass() != null && isGeneratedFunction(classNode);
     }
 
     public boolean isInClosureConstructor() {
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java
index cfce5c5..a47131c 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java
@@ -28,7 +28,6 @@ import org.codehaus.groovy.ast.MethodNode;
 import org.codehaus.groovy.ast.Parameter;
 import org.codehaus.groovy.ast.builder.AstStringCompiler;
 import org.codehaus.groovy.ast.expr.ClosureExpression;
-import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.LambdaExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
@@ -60,6 +59,7 @@ import static org.codehaus.groovy.ast.ClassHelper.SERIALIZABLE_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.SERIALIZEDLAMBDA_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.VOID_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.findSAM;
+import static org.codehaus.groovy.ast.ClassHelper.isGeneratedFunction;
 import static org.codehaus.groovy.ast.ClassHelper.long_TYPE;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
@@ -175,7 +175,11 @@ public class StaticTypesLambdaWriter extends LambdaWriter implements AbstractFun
         mv.visitInsn(DUP);
 
         if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall() || !accessingInstanceMembers) {
-            operandStack.pushConstant(ConstantExpression.NULL);
+            ClassNode classNode = controller.getClassNode();
+            while (isGeneratedFunction(classNode)) {
+                classNode = classNode.getOuterClass();
+            }
+            classX(classNode).visit(controller.getAcg());
         } else {
             mv.visitVarInsn(ALOAD, 0);
             operandStack.push(controller.getClassNode());
@@ -210,9 +214,9 @@ public class StaticTypesLambdaWriter extends LambdaWriter implements AbstractFun
         return lambdaSharedVariableParameters;
     }
 
-    private String createAbstractMethodDesc(final ClassNode functionalInterface, final ClassNode lambdaClassNode) {
+    private String createAbstractMethodDesc(final ClassNode functionalInterface, final ClassNode lambdaClass) {
         List<Parameter> lambdaSharedVariables = new LinkedList<>();
-        prependParameter(lambdaSharedVariables, "__lambda_this", lambdaClassNode);
+        prependParameter(lambdaSharedVariables, "__lambda_this", lambdaClass);
         return BytecodeHelper.getMethodDescriptor(functionalInterface, lambdaSharedVariables.toArray(Parameter.EMPTY_ARRAY));
     }
 
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy b/src/test/groovy/transform/stc/LambdaTest.groovy
index 48cf528..eb0af34 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -1053,7 +1053,7 @@ final class LambdaTest {
     }
 
     @Test // GROOVY-9332
-    void testStaticInitializeBlocks() {
+    void testStaticInitializeBlocks1() {
         assertScript '''
             @groovy.transform.CompileStatic
             class Test1 {
@@ -1078,6 +1078,18 @@ final class LambdaTest {
         '''
     }
 
+    @Test // GROOVY-9342
+    void testStaticInitializeBlocks3() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            class Test1 {
+                static int acc = 1
+                static { [1, 2, 3].forEach((Integer i) -> acc += i) }
+            }
+            assert Test1.acc == 7
+        '''
+    }
+
     @Test
     void testAccessingThis1() {
         assertScript '''