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: Refine the error message for invalid receiver type(closes #931)
Date Fri, 24 May 2019 13:42:22 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 dcf3bcb  Refine the error message for invalid receiver type(closes #931)
dcf3bcb is described below

commit dcf3bcbc65e71ad47bffaca904ab8a58195bb17a
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Fri May 24 21:41:06 2019 +0800

    Refine the error message for invalid receiver type(closes #931)
---
 ...StaticTypesMethodReferenceExpressionWriter.java | 22 ++++++++++++++--------
 .../transform/stc/MethodReferenceTest.groovy       |  2 +-
 2 files changed, 15 insertions(+), 9 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 a89e7a3..110d41f 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
@@ -110,6 +110,15 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
                             .map(e -> e.getType().getName())
                             .collect(Collectors.joining(","))
                     + ")] in the type[" + typeOrTargetRefType.getName() + "]", methodReferenceExpression);
+        } else {
+            if (parametersWithExactType.length > 0 && isTypeReferingInstanceMethod(typeOrTargetRef,
methodRefMethod)) {
+                Parameter firstParameter = parametersWithExactType[0];
+                Class<?> typeOrTargetClass = typeOrTargetRef.getType().getTypeClass();
+                Class<?> firstParameterClass = firstParameter.getType().getTypeClass();
+                if (!typeOrTargetClass.isAssignableFrom(firstParameterClass)) {
+                    throw new RuntimeParserException("Invalid receiver type: " + firstParameterClass
+ " is not compatible with " + typeOrTargetClass, typeOrTargetRef);
+                }
+            }
         }
 
         methodRefMethod.putNodeMetaData(ORIGINAL_PARAMETERS_WITH_EXACT_TYPE, parametersWithExactType);
@@ -241,18 +250,11 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
         List<MethodNode> candidates = new LinkedList<>();
         for (MethodNode mn : filterMethodsByVisibility(methodNodeList, classNode)) {
             Parameter[] parameters = abstractMethodParameters;
-            if (!mn.isStatic() && isClassExpr(typeOrTargetRef)) { // class::instanceMethod
+            if (isTypeReferingInstanceMethod(typeOrTargetRef, mn)) {
                 if (0 == abstractMethodParameters.length) {
                     continue;
                 }
 
-                Parameter firstParameter = abstractMethodParameters[0];
-                Class<?> typeOrTargetClass = typeOrTargetRef.getType().getTypeClass();
-                Class<?> firstParameterClass = firstParameter.getType().getTypeClass();
-                if (!typeOrTargetClass.isAssignableFrom(firstParameterClass)) {
-                    continue;
-                }
-
                 parameters =
                         new ArrayList<>(Arrays.asList(abstractMethodParameters))
                                 .subList(1, abstractMethodParameters.length)
@@ -268,6 +270,10 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
         return chooseMethodRefMethodCandidate(typeOrTargetRef, candidates);
     }
 
+    private static boolean isTypeReferingInstanceMethod(Expression typeOrTargetRef, MethodNode
mn) {  // class::instanceMethod
+        return !mn.isStatic() && isClassExpr(typeOrTargetRef);
+    }
+
     /**
      * Choose the best method node for method reference.
      */
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index 0fe07ca..8f932d8 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -411,6 +411,6 @@ class MethodReferenceTest extends GroovyTestCase {
             p()
         '''
 
-        assert errMsg.contains('Failed to find the expected method[toString(java.lang.Integer)]
in the type[java.lang.String]')
+        assert errMsg.contains('Invalid receiver type: class java.lang.Integer is not compatible
with class java.lang.String')
     }
 }


Mime
View raw message