You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/10/06 16:16:08 UTC

[groovy] branch GROOVY-9762 created (now 40e952c)

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

emilles pushed a change to branch GROOVY-9762
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 40e952c  GROOVY-9762: if not ClassExpression, use TypeChooser to find target type

This branch includes the following new commits:

     new 40e952c  GROOVY-9762: if not ClassExpression, use TypeChooser to find target type

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9762: if not ClassExpression, use TypeChooser to find target type

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9762
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 40e952c7ac81a795a8f8bb5b27d52d99ae9ed795
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 6 11:11:06 2020 -0500

    GROOVY-9762: if not ClassExpression, use TypeChooser to find target type
---
 .../sc/StaticTypesMethodReferenceExpressionWriter.java  |  7 ++++---
 src/test/groovy/transform/stc/GenericsSTCTest.groovy    | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
index 02f28db..1e7ecb2 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
@@ -89,7 +89,8 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
         ClassNode classNode = controller.getClassNode();
         Expression typeOrTargetRef = methodReferenceExpression.getExpression();
         boolean isClassExpression = (typeOrTargetRef instanceof ClassExpression);
-        ClassNode typeOrTargetRefType = typeOrTargetRef.getType(); // TODO: GROOVY-9762
+        ClassNode typeOrTargetRefType = isClassExpression ? typeOrTargetRef.getType()
+                : controller.getTypeChooser().resolveType(typeOrTargetRef, classNode);
 
         ClassNode[] methodReferenceParamTypes = methodReferenceExpression.getNodeMetaData(CLOSURE_ARGUMENTS);
         Parameter[] parametersWithExactType = createParametersWithExactType(abstractMethodNode, methodReferenceParamTypes);
@@ -241,8 +242,8 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
         List<Parameter> methodReferenceSharedVariableList = new ArrayList<>();
 
         if (!(methodRef instanceof ClassExpression)) {
-            ClassNode methodRefTargetType = methodRef.getType();
-            prependParameter(methodReferenceSharedVariableList, METHODREF_EXPR_INSTANCE, methodRefTargetType);
+            prependParameter(methodReferenceSharedVariableList, METHODREF_EXPR_INSTANCE,
+                controller.getTypeChooser().resolveType(methodRef, controller.getClassNode()));
         }
 
         return BytecodeHelper.getMethodDescriptor(functionalInterfaceType.redirect(), methodReferenceSharedVariableList.toArray(Parameter.EMPTY_ARRAY));
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index a22e6ac..b18d878 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -686,6 +686,23 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-9762
+    void testShouldUseMethodGenericType7() {
+        for (toList in ['{ list(it) }', 'o -> list(o)', 'this.&list', 'this::list']) {
+            assertScript """
+                def <T> List<T> list(T item) {
+                    return [item]
+                }
+                def test() {
+                    Optional<Integer> opt = Optional.ofNullable(1)
+                    List<Integer> ret = opt.map($toList).get()
+                    return ret
+                }
+                assert test() == [1]
+            """
+        }
+    }
+
     // GROOVY-5516
     void testAddAllWithCollectionShouldBeAllowed() {
         assertScript '''import org.codehaus.groovy.transform.stc.ExtensionMethodNode