Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X fd81ec1fe -> 369a42e0e


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/369a42e0
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/369a42e0
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/369a42e0

Branch: refs/heads/GROOVY_2_6_X
Commit: 369a42e0e9ea059629554e095c4e6a15871ef215
Parents: fd81ec1
Author: sunlan <sun...@apache.org>
Authored: Fri Feb 2 17:44:20 2018 +0800
Committer: sunlan <sun...@apache.org>
Committed: Fri Feb 2 17:58:40 2018 +0800

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


http://git-wip-us.apache.org/repos/asf/groovy/blob/369a42e0/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 60eaa47..98a1ba2 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
@@ -399,7 +399,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/369a42e0/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/369a42e0/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/369a42e0/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 1bf3d68..b810748 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -1254,6 +1254,28 @@ class MethodCallsSTCTest extends 
StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-8445
+    void testGroovy8445() {
+        if (IS_PRE_8) return
+
+        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 } }

Reply via email to