You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/10/09 05:10:43 UTC
[groovy] branch master updated: GROOVY-9762: if not ClassExpression,
use TypeChooser to find target type (closes #1397)
This is an automated email from the ASF dual-hosted git repository.
paulk 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 770ae5f GROOVY-9762: if not ClassExpression, use TypeChooser to find target type (closes #1397)
770ae5f is described below
commit 770ae5ffc9b861baadfc52447c64e45591a70b37
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 (closes #1397)
---
.../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