groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Make `CorrectAccessedVariableVisitor` extends `CodeVisitorSupport`
Date Wed, 31 Jan 2018 09:17:16 GMT
Repository: groovy
Updated Branches:
  refs/heads/native-lambda dbcac60f6 -> 37dc84543


Make `CorrectAccessedVariableVisitor` extends `CodeVisitorSupport`


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

Branch: refs/heads/native-lambda
Commit: 37dc8454315615d8f02609d50c0a5ce935e36cab
Parents: dbcac60
Author: sunlan <sunlan@apache.org>
Authored: Wed Jan 31 17:17:10 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Wed Jan 31 17:17:10 2018 +0800

----------------------------------------------------------------------
 .../groovy/classgen/asm/ClosureWriter.java      | 10 +----
 .../asm/sc/StaticTypesLambdaWriter.java         | 17 +++++++-
 src/test/groovy/transform/stc/LambdaTest.groovy | 44 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/37dc8454/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
index 343b714..7c44599 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
@@ -19,9 +19,9 @@
 package org.codehaus.groovy.classgen.asm;
 
 import org.codehaus.groovy.GroovyBugError;
-import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.CodeVisitorSupport;
 import org.codehaus.groovy.ast.ConstructorNode;
 import org.codehaus.groovy.ast.FieldNode;
 import org.codehaus.groovy.ast.InnerClassNode;
@@ -44,7 +44,6 @@ import org.codehaus.groovy.ast.stmt.ExpressionStatement;
 import org.codehaus.groovy.ast.stmt.ReturnStatement;
 import org.codehaus.groovy.classgen.AsmClassGenerator;
 import org.codehaus.groovy.classgen.Verifier;
-import org.codehaus.groovy.control.SourceUnit;
 import org.objectweb.asm.MethodVisitor;
 
 import java.util.HashMap;
@@ -332,7 +331,7 @@ public class ClosureWriter {
                 + controller.getContext().getNextClosureInnerName(outerClass, classNode,
methodNode);
     }
 
-    protected static class CorrectAccessedVariableVisitor extends ClassCodeVisitorSupport
{
+    protected static class CorrectAccessedVariableVisitor extends CodeVisitorSupport {
         private InnerClassNode icn;
 
         public CorrectAccessedVariableVisitor(InnerClassNode icn) {
@@ -350,11 +349,6 @@ public class ClosureWriter {
                 expression.setAccessedVariable(fn);
             }
         }
-
-        @Override
-        protected SourceUnit getSourceUnit() {
-            return null;
-        }
     }
 
     private static void correctAccessedVariable(final InnerClassNode closureClass, ClosureExpression
ce) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/37dc8454/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java
----------------------------------------------------------------------
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 a1a797c..60f8f7d 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
@@ -20,6 +20,7 @@
 package org.codehaus.groovy.classgen.asm.sc;
 
 import org.codehaus.groovy.GroovyBugError;
+import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.ConstructorNode;
@@ -39,6 +40,7 @@ import org.codehaus.groovy.classgen.asm.LambdaWriter;
 import org.codehaus.groovy.classgen.asm.OperandStack;
 import org.codehaus.groovy.classgen.asm.WriterController;
 import org.codehaus.groovy.classgen.asm.WriterControllerFactory;
+import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.transform.stc.StaticTypesMarker;
 import org.objectweb.asm.Handle;
 import org.objectweb.asm.MethodVisitor;
@@ -410,15 +412,21 @@ java.lang.ArrayIndexOutOfBoundsException: size==0
         return staticTypesClosureWriter.createClosureClass(expression, mods);
     }
 
-    private static final class TransformationVisitor extends CorrectAccessedVariableVisitor
{
+    private static final class TransformationVisitor extends ClassCodeVisitorSupport {
+        private CorrectAccessedVariableVisitor correctAccessedVariableVisitor;
         private Parameter enclosingThisParameter;
 
         public TransformationVisitor(InnerClassNode icn, Parameter enclosingThisParameter)
{
-            super(icn);
+            correctAccessedVariableVisitor = new CorrectAccessedVariableVisitor(icn);
             this.enclosingThisParameter = enclosingThisParameter;
         }
 
         @Override
+        public void visitVariableExpression(VariableExpression expression) {
+            correctAccessedVariableVisitor.visitVariableExpression(expression);
+        }
+
+        @Override
         public void visitMethodCallExpression(MethodCallExpression call) {
             if (!call.getMethodTarget().isStatic()) {
                 Expression objectExpression = call.getObjectExpression();
@@ -437,5 +445,10 @@ java.lang.ArrayIndexOutOfBoundsException: size==0
 
             super.visitMethodCallExpression(call);
         }
+
+        @Override
+        protected SourceUnit getSourceUnit() {
+            return null;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/37dc8454/src/test/groovy/transform/stc/LambdaTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy b/src/test/groovy/transform/stc/LambdaTest.groovy
index 560ab77..9bf9774 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -444,6 +444,27 @@ TestScript0.groovy: 14: [Static type checking] - Cannot find matching
method jav
         '''
     }
 
+    void testFunctionCall3() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        import java.util.stream.Collectors
+        import java.util.stream.Stream
+        import java.util.function.Function
+        
+        @CompileStatic
+        public class Test1 {
+            public static void main(String[] args) {
+                p();
+            }
+        
+            public static void p() {
+                Function<Integer, Integer> f = (Integer e) -> (Integer) (e + 1)
// Casting is required...  [Static type checking] - Incompatible generic argument types. Cannot
assign java.util.function.Function <java.lang.Integer, int> to: java.util.function.Function
<Integer, Integer>
+                assert 2 == f.apply(1)
+            }
+        }
+        '''
+    }
+
     void testConsumerCall() {
         assertScript '''
         import groovy.transform.CompileStatic
@@ -490,6 +511,29 @@ TestScript0.groovy: 14: [Static type checking] - Cannot find matching
method jav
         '''
     }
 
+    void testConsumerCall3() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        import java.util.stream.Collectors
+        import java.util.stream.Stream
+        import java.util.function.Consumer
+        
+        @CompileStatic
+        public class Test1 {
+            public static void main(String[] args) {
+                p();
+            }
+        
+            public static void p() {
+                int r = 1
+                Consumer<Integer> c = (Integer e) -> { r += e }
+                c.accept(2)
+                assert 3 == r
+            }
+        }
+        '''
+    }
+
     void testFunctionWithUpdatingLocalVariable() {
         assertScript '''
         import groovy.transform.CompileStatic


Mime
View raw message