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/11/24 17:35:24 UTC
[groovy] 01/01: GROOVY-9814: MethodClosure can run on sub-types for
Type::name reference
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9814
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit ebb457545caa8abe50981472b53d5bba6f8a78f3
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Nov 24 11:35:04 2020 -0600
GROOVY-9814: MethodClosure can run on sub-types for Type::name reference
---
src/main/java/groovy/lang/MetaClassImpl.java | 6 +-
.../transform/stc/MethodReferenceTest.groovy | 64 +++++++++++++++-------
2 files changed, 46 insertions(+), 24 deletions(-)
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 0990d3d..5eeef0e 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -1094,13 +1094,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
throw e;
}
- if (arguments.length <= 0 || !(arguments[0].getClass().equals(ownerClass))) {
+ if (arguments.length < 1 || !ownerClass.isAssignableFrom(arguments[0].getClass())) {
return invokeMissingMethod(object, methodName, arguments);
}
- Object newOwner = arguments[0];
+ Object newReceiver = arguments[0];
Object[] newArguments = Arrays.copyOfRange(arguments, 1, arguments.length);
- return ownerMetaClass.invokeMethod(ownerClass, newOwner, methodName, newArguments, false, false);
+ return ownerMetaClass.invokeMethod(ownerClass, newReceiver, methodName, newArguments, false, false);
}
}
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index ffa0d92..80b261a 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -38,8 +38,7 @@ final class MethodReferenceTest {
@CompileStatic
void p() {
def result = [1, 2, 3].stream().map(Object::toString).collect(Collectors.toList())
- assert 3 == result.size()
- assert ['1', '2', '3'] == result
+ assert result == ['1', '2', '3']
}
p()
@@ -47,13 +46,12 @@ final class MethodReferenceTest {
}
@Test // class::instanceMethod
- void testFunctionCI_MULTI_MATCHED_METHODS() {
+ void testFunctionCI2() {
assertScript shell, '''
@CompileStatic
void p() {
def result = [1, 2, 3].stream().map(Integer::toString).collect(Collectors.toList())
- assert 3 == result.size()
- assert ['1', '2', '3'] == result
+ assert result ['1', '2', '3']
}
p()
@@ -61,6 +59,46 @@ final class MethodReferenceTest {
}
@Test // class::instanceMethod
+ void testFunctionCI3() {
+ def err = shouldFail shell, '''
+ @CompileStatic
+ void p() {
+ def result = [1, 2, 3].stream().map(String::toString).collect(Collectors.toList())
+ }
+
+ p()
+ '''
+
+ assert err =~ /Invalid receiver type: java.lang.Integer is not compatible with java.lang.String/
+ }
+
+ @Test // class::instanceMethod -- GROOVY-9814
+ void testFunctionCI4() {
+ assertScript shell, '''
+ @CompileStatic
+ class One { String id }
+
+ @CompileStatic
+ class Two extends One { }
+
+ @CompileStatic @groovy.transform.Immutable(knownImmutableClasses=[Function])
+ class FunctionHolder<T> {
+ Function<T, ?> extractor
+
+ def apply(T t) {
+ extractor.apply(t)
+ }
+ }
+
+ def fh = new FunctionHolder(One::getId)
+ assert fh.apply(new One(id:'abc')) == 'abc'
+
+ fh = new FunctionHolder(One::getId)
+ assert fh.apply(new Two(id:'xyz')) == 'xyz' // sub-type argument
+ '''
+ }
+
+ @Test // class::instanceMethod
void testBinaryOperatorCI() {
assertScript shell, '''
@CompileStatic
@@ -373,22 +411,6 @@ final class MethodReferenceTest {
'''
}
- @Test // class::instanceMethod
- void testFunctionCI_WRONGTYPE() {
- def err = shouldFail shell, '''
- @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 err =~ 'Invalid receiver type: java.lang.Integer is not compatible with java.lang.String'
- }
-
@Test // class::instanceMethod, actually class::staticMethod
void testFunctionCI_DGM() {
assertScript shell, '''