groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 04/05: GROOVY-9342: load static "this" type for static access of properties
Date Thu, 19 Dec 2019 03:09:33 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

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

    GROOVY-9342: load static "this" type for static access of properties
    
    (cherry picked from commit 152bda0c62ea060a40de21abd46b5ed28f907b0b)
---
 .../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 67cf98e..b582e2f 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 {
             loadThis();
         }
@@ -209,9 +213,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 ac3f138..d7b9553 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 '''


Mime
View raw message