groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [1/2] groovy git commit: GROOVY-8445: Static analysis does not work properly when coercing Closure to FunctionInterface
Date Fri, 02 Feb 2018 10:15:14 GMT
Repository: groovy
Updated Branches:
  refs/heads/native-lambda 13eed82a7 -> 68df3e2b0


GROOVY-8445: Static analysis does not work properly when coercing Closure to FunctionInterface


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

Branch: refs/heads/native-lambda
Commit: 9a21c9eb16d53b071a18d1eb3806c51afe332c2b
Parents: 13eed82
Author: sunlan <sunlan@apache.org>
Authored: Fri Feb 2 17:44:20 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Fri Feb 2 18:05:55 2018 +0800

----------------------------------------------------------------------
 .../org/codehaus/groovy/ast/ClassHelper.java    |  9 ++++++++-
 .../groovy/ast/tools/GenericsUtils.java         |  7 +++++++
 .../stc/StaticTypeCheckingSupport.java          |  8 ++++++++
 .../transform/stc/MethodCallsSTCTest.groovy     | 20 ++++++++++++++++++++
 4 files changed, 43 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9a21c9eb/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
index 7ce72a3..57537fa 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
@@ -410,7 +410,14 @@ public class ClassHelper {
     public static MethodNode findSAM(ClassNode type) {
         if (!Modifier.isAbstract(type.getModifiers())) return null;
         if (type.isInterface()) {
-            List<MethodNode> methods = type.getMethods();
+            List<MethodNode> methods;
+            if (type.isInterface()) {
+                // e.g. BinaryOperator extends BiFunction, BinaryOperator contains no abstract
method, but it is really a SAM
+                methods = type.redirect().getAllDeclaredMethods();
+            } else {
+                methods = type.getMethods();
+            }
+
             MethodNode found = null;
             for (MethodNode mi : methods) {
                 // ignore methods, that are not abstract and from Object

http://git-wip-us.apache.org/repos/asf/groovy/blob/9a21c9eb/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index 653f327..a7cf7c0 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -444,6 +444,13 @@ public class GenericsUtils {
             // have first to find matching super class or interface
             Map <String,ClassNode> genSpec = createGenericsSpec(type);
             ClassNode superClass = ClassHelper.getNextSuperClass(type,target);
+
+            if (superClass == null) {
+                if (ClassHelper.isPrimitiveType(type)) {
+                    superClass = ClassHelper.getNextSuperClass(ClassHelper.getWrapper(type),
target);
+                }
+            }
+
             if (superClass!=null){
                 ClassNode corrected = GenericsUtils.correctToGenericsSpecRecurse(genSpec,
superClass);
                 extractSuperClassGenerics(corrected, target, spec);

http://git-wip-us.apache.org/repos/asf/groovy/blob/9a21c9eb/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index f83e32c..126cc41 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -20,6 +20,7 @@
 package org.codehaus.groovy.transform.stc;
 
 import org.codehaus.groovy.GroovyBugError;
+import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.GenericsType;
 import org.codehaus.groovy.ast.MethodNode;
@@ -1768,6 +1769,13 @@ public abstract class StaticTypeCheckingSupport {
             // have first to find matching super class or interface
             Map <String,ClassNode> genSpec = GenericsUtils.createGenericsSpec(type);
             ClassNode superClass = getNextSuperClass(type,target);
+
+            if (superClass == null) {
+                if (ClassHelper.isPrimitiveType(type)) {
+                    superClass = ClassHelper.getNextSuperClass(ClassHelper.getWrapper(type),
target);
+                }
+            }
+
             if (superClass!=null){
                 ClassNode corrected;
                 if (missesGenericsTypes(type)) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/9a21c9eb/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index 74c2095..4afc665 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -1238,6 +1238,26 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-8445
+    void testGroovy8445() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        import java.util.stream.Collectors
+        import java.util.stream.Stream
+        
+        @CompileStatic
+        public class Test1 {
+            public static void main(String[] args) {
+                p();
+            }
+            
+            public static void p() {
+                assert 13 == Stream.of(1, 2, 3).reduce(7, {Integer r, Integer e -> r +
e});
+            }
+        }
+        '''
+    }
+
     static class MyMethodCallTestClass {
 
         static int mul(int... args) { args.toList().inject(1) { x,y -> x*y } }


Mime
View raw message