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

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


The following commit(s) were added to refs/heads/master by this push:
     new 35b8d93  Refine "GROOVY-8920: Fails to infer parameter and return type 
of SAM on RHS"
35b8d93 is described below

commit 35b8d939742581425ff590d79e3c3e204c27c619
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Sat Dec 15 14:56:50 2018 +0800

    Refine "GROOVY-8920: Fails to infer parameter and return type of SAM on RHS"
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java    |  3 +--
 src/test/groovy/transform/stc/LambdaTest.groovy     | 21 +++++++++++++++++++++
 .../groovy/ast/tools/GenericsUtilsTest.groovy       | 20 ++++++++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)

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 2f90641..5d2d3d6 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -959,9 +959,8 @@ public class GenericsUtils {
      *
      */
     public static Tuple2<ClassNode[], ClassNode> parameterizeSAM(ClassNode 
sam) {
-        final Map<GenericsType, GenericsType> map = 
makePlaceholderAndParameterizedTypeMap(sam);
-
         MethodNode methodNode = ClassHelper.findSAM(sam);
+        final Map<GenericsType, GenericsType> map = 
makeDeclaringAndActualGenericsTypeMapOfExactType(methodNode.getDeclaringClass(),
 sam);
 
         ClassNode[] parameterTypes =
                 Arrays.stream(methodNode.getParameters())
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy 
b/src/test/groovy/transform/stc/LambdaTest.groovy
index 48d518f..7bf7fff 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -128,6 +128,27 @@ class LambdaTest extends GroovyTestCase {
         '''
     }
 
+    void testBinaryOperatorWithoutExplicitTypeDef2() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        import java.util.stream.Collectors
+        import java.util.stream.Stream
+        import java.util.function.BinaryOperator
+        
+        @CompileStatic
+        public class Test1 {
+            public static void main(String[] args) {
+                p();
+            }
+            
+            public static void p() {
+                BinaryOperator<Integer> accumulator = (r, e) -> r + e
+                assert 13 == [1, 2, 3].stream().reduce(7, accumulator);
+            }
+        }
+        '''
+    }
+
     void testConsumer() {
         assertScript '''
         import groovy.transform.CompileStatic
diff --git a/src/test/org/codehaus/groovy/ast/tools/GenericsUtilsTest.groovy 
b/src/test/org/codehaus/groovy/ast/tools/GenericsUtilsTest.groovy
index dd5220e..1373f4f 100644
--- a/src/test/org/codehaus/groovy/ast/tools/GenericsUtilsTest.groovy
+++ b/src/test/org/codehaus/groovy/ast/tools/GenericsUtilsTest.groovy
@@ -301,4 +301,24 @@ class GenericsUtilsTest extends GroovyTestCase {
         assert ClassHelper.STRING_TYPE == typeInfo.getV1()[0]
         assert ClassHelper.Integer_TYPE == typeInfo.getV2()
     }
+
+    void testParameterizeSAM2() {
+        def code = '''
+        import java.util.function.*
+        interface T extends BinaryOperator<Integer> {}
+        '''
+        def ast = new CompilationUnit().tap {
+            addSource 'hello.groovy', code
+            compile Phases.SEMANTIC_ANALYSIS
+        }.ast
+
+        def classNodeList = ast.getModules()[0].getClasses()
+        ClassNode parameterizedClassNode = findClassNode('T', 
classNodeList).getAllInterfaces().find { 
it.name.equals('java.util.function.BinaryOperator') }
+
+        Tuple2<ClassNode[], ClassNode> typeInfo = 
GenericsUtils.parameterizeSAM(parameterizedClassNode)
+        assert 2 == typeInfo.getV1().length
+        assert ClassHelper.Integer_TYPE == typeInfo.getV1()[0]
+        assert ClassHelper.Integer_TYPE == typeInfo.getV1()[1]
+        assert ClassHelper.Integer_TYPE == typeInfo.getV2()
+    }
 }

Reply via email to