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 2019/07/06 09:20:40 UTC

[groovy] branch master updated: GROOVY-9140: Class.&instanceMethod with incorrect first param (improve error message)

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 acef2e9  GROOVY-9140: Class.&instanceMethod with incorrect first param (improve error message)
acef2e9 is described below

commit acef2e91e69627ac3387ac15f3405a98a7d221c5
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Jul 6 19:20:27 2019 +1000

    GROOVY-9140: Class.&instanceMethod with incorrect first param (improve error message)
---
 src/main/java/groovy/lang/MetaClassImpl.java |  4 ++--
 src/test/groovy/ClosureMethodCallTest.groovy | 29 ++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index b0ab132..949279b 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -1059,7 +1059,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         // To conform to "Least Surprise" principle, try to invoke method with original arguments first, which can match most of use cases
         try {
             return ownerMetaClass.invokeMethod(ownerClass, owner, methodName, arguments, false, false);
-        } catch (MissingMethodExceptionNoStack e) {
+        } catch (MissingMethodExceptionNoStack | InvokerInvocationException e) {
             // CONSTRUCTOR REFERENCE
             if (owner instanceof Class && MethodClosure.NEW.equals(methodName)) {
                 if (ownerClass.isArray()) {
@@ -1105,7 +1105,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 throw e;
             }
 
-            if (arguments.length <= 0) {
+            if (arguments.length <= 0 || !(arguments[0].getClass().equals(ownerClass))) {
                 return invokeMissingMethod(object, methodName, arguments);
             }
 
diff --git a/src/test/groovy/ClosureMethodCallTest.groovy b/src/test/groovy/ClosureMethodCallTest.groovy
index 046aebf..e9a24b5 100644
--- a/src/test/groovy/ClosureMethodCallTest.groovy
+++ b/src/test/groovy/ClosureMethodCallTest.groovy
@@ -88,4 +88,33 @@ class ClosureMethodCallTest extends GroovyTestCase {
             assert Foo.bar() {} == 2
         '''
     }
+
+    //GROOVY-9140
+    void testCorrectErrorForClassInstanceMethodReference() {
+        assertScript '''
+            class Y {
+                def m() {1}
+            }
+            
+            ref = Y.&m
+            assert ref(new Y()) == 1
+        '''
+        shouldFail MissingMethodException, '''
+            class Y {
+                def m() {1}
+            }
+
+            ref = Y.&m
+            assert ref(new Y()) == 1
+            assert ref() == 1
+        '''
+        shouldFail MissingMethodException, '''
+            class Y {
+                def m() {1}
+            }
+
+            ref = Y.&m
+            assert ref(1) == 1
+        '''
+    }
 }