groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: Validate parameter types of method reference(class::instanceMethod)(closes #930)
Date Sun, 19 May 2019 13:12:12 GMT
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 11c6aab  Validate parameter types of method reference(class::instanceMethod)(closes
#930)
11c6aab is described below

commit 11c6aaba3a8f2ab4bfbececf7af21e733bf71ae8
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sun May 19 21:10:06 2019 +0800

    Validate parameter types of method reference(class::instanceMethod)(closes #930)
---
 ...StaticTypesMethodReferenceExpressionWriter.java | 34 +++++++++++-----------
 .../transform/stc/MethodReferenceTest.groovy       | 18 ++++++++++++
 2 files changed, 35 insertions(+), 17 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 1fe3772..a89e7a3 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
@@ -240,28 +240,28 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
 
         List<MethodNode> candidates = new LinkedList<>();
         for (MethodNode mn : filterMethodsByVisibility(methodNodeList, classNode)) {
-            if (mn.isStatic()) {
-                if (ParameterUtils.parametersCompatible(abstractMethodParameters, mn.getParameters()))
{
-                    candidates.add(mn);
-                }
-            } else {
+            Parameter[] parameters = abstractMethodParameters;
+            if (!mn.isStatic() && isClassExpr(typeOrTargetRef)) { // class::instanceMethod
                 if (0 == abstractMethodParameters.length) {
-                    break;
+                    continue;
                 }
 
-                Parameter[] parameters;
-                if (isClassExpr(typeOrTargetRef)) {
-                    parameters =
-                            new ArrayList<>(Arrays.asList(abstractMethodParameters))
-                                    .subList(1, abstractMethodParameters.length)
-                                    .toArray(Parameter.EMPTY_ARRAY);
-                } else {
-                    parameters = abstractMethodParameters;
+                Parameter firstParameter = abstractMethodParameters[0];
+                Class<?> typeOrTargetClass = typeOrTargetRef.getType().getTypeClass();
+                Class<?> firstParameterClass = firstParameter.getType().getTypeClass();
+                if (!typeOrTargetClass.isAssignableFrom(firstParameterClass)) {
+                    continue;
                 }
 
-                if (ParameterUtils.parametersCompatible(parameters, mn.getParameters()))
{
-                    candidates.add(mn);
-                }
+                parameters =
+                        new ArrayList<>(Arrays.asList(abstractMethodParameters))
+                                .subList(1, abstractMethodParameters.length)
+                                .toArray(Parameter.EMPTY_ARRAY);
+
+            }
+
+            if (ParameterUtils.parametersCompatible(parameters, mn.getParameters())) {
+                candidates.add(mn);
             }
         }
 
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index 1680ea4..0fe07ca 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -395,4 +395,22 @@ class MethodReferenceTest extends GroovyTestCase {
 
         assert errMsg.contains('Failed to find the expected method[addx(java.math.BigDecimal,java.math.BigDecimal)]
in the type[java.math.BigDecimal]')
     }
+
+    // class::instanceMethod
+    void testFunctionCI_WRONGTYPE() {
+        def errMsg = shouldFail '''
+            import java.util.stream.Collectors
+            
+            @groovy.transform.CompileStatic
+            void p() {
+                def result = [1, 2, 3].stream().map(String::toString).collect(Collectors.toList())
+                assert 3 == result.size()
+                assert ['1', '2', '3'] == result
+            }
+            
+            p()
+        '''
+
+        assert errMsg.contains('Failed to find the expected method[toString(java.lang.Integer)]
in the type[java.lang.String]')
+    }
 }


Mime
View raw message