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/05/23 05:19:19 UTC

[groovy] branch master updated (2221ba0 -> 375b105)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 2221ba0  GROOVY-9135: Additional support for @Testable annotation in JUnit5Runner
     new b1e1c8d  GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (Add test cases - closes #287)
     new 375b105  GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (add doco)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/spec/doc/core-operators.adoc              | 17 +++++++++++++++++
 src/spec/test/OperatorsTest.groovy            | 15 +++++++++++++++
 src/test/groovy/bugs/MethodClosureTest.groovy | 23 +++++++++++++++++++----
 3 files changed, 51 insertions(+), 4 deletions(-)


[groovy] 01/02: GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (Add test cases - closes #287)

Posted by pa...@apache.org.
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

commit b1e1c8d975b18bfa2bf42688d45c9bb3d2a7eb1f
Author: UEHARA Junji <ue...@jggug.org>
AuthorDate: Wed Mar 9 05:00:44 2016 +0900

    GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (Add test cases - closes #287)
---
 src/test/groovy/bugs/MethodClosureTest.groovy | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/test/groovy/bugs/MethodClosureTest.groovy b/src/test/groovy/bugs/MethodClosureTest.groovy
index 317ef7f..1ce27fc 100644
--- a/src/test/groovy/bugs/MethodClosureTest.groovy
+++ b/src/test/groovy/bugs/MethodClosureTest.groovy
@@ -29,26 +29,41 @@ class MethodClosureTest extends GroovyTestCase {
     static bb(it) { it}
 
     void testMethodClosure() {
+        def cl2 = String.&toUpperCase // Class.instanceMethod
+        assert cl2 instanceof Closure
+        assert cl2 instanceof MethodClosure
+
+        assert ["xx", "yy"].collect(cl2) == ["XX","YY"]
+
         Class[] c1 = [ Exception.class, Throwable.class ]
         Class[] c2 = [ IllegalStateException.class ]
 
-        def cl = this.&aa
+        def cl = this.&aa // instance.instanceMethod
 
         assert cl instanceof Closure
         assert cl instanceof MethodClosure
 
         assert [c1, c2].collect(cl) == [c1,c2]
+
     }
     
     void testStaticMethodAccess() {
        def list = [1].collect (this.&bb)
        assert list == [1]
-       list = [1].collect (MethodClosureTest.&bb)
+       list = [1].collect (MethodClosureTest.&bb) // Class.staticMethod
        assert list == [1]
        def mct = new MethodClosureTest()
-       list = [1].collect (mct.&bb)
+       list = [1].collect (mct.&bb) // instance.staticMethod
        assert list == [1]
     }
-}
 
 
+    void testShellVariable() {
+        def shell = new GroovyShell()
+        assert shell.evaluate("x = String.&toUpperCase; x('abc')") == "ABC"
+        assert shell.evaluate("x = 'abc'.&toUpperCase; x()") == "ABC"
+        assert shell.evaluate("x = Integer.&parseInt; x('123')") == 123
+        assert shell.evaluate("x = 3.&parseInt; x('123')") == 123
+    }
+}
+


[groovy] 02/02: GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (add doco)

Posted by pa...@apache.org.
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

commit 375b105dd745b3543d6b374c34fa5ed57eaa8ed6
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu May 23 15:18:44 2019 +1000

    GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (add doco)
---
 src/spec/doc/core-operators.adoc   | 17 +++++++++++++++++
 src/spec/test/OperatorsTest.groovy | 15 +++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/spec/doc/core-operators.adoc b/src/spec/doc/core-operators.adoc
index 7fa2566..e1a64f5 100644
--- a/src/spec/doc/core-operators.adoc
+++ b/src/spec/doc/core-operators.adoc
@@ -366,6 +366,23 @@ include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_dis
 <4> using the method pointer with a `String` calls the `String` version of `doSomething`
 <5> using the method pointer with an `Integer` calls the `Integer` version of `doSomething`
 
+To align with Java 8 method reference expectations, in Groovy 3 and above, you can use `new` as the
+method name to obtain a method pointer to the constructor:
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_new,indent=0]
+----
+
+Also in Groovy 3 and above, you can obtain a method pointer to an instance method of a class.
+This method pointer takes an additional parameter being the receiver instance to
+invoke the method on:
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_class_instance,indent=0]
+----
+For backwards compatibility, any static methods that happen to have the correct
+parameters for the call will be given precedence over instance methods for this case.
+
 [[method-reference-operator]]
 === Method reference operator
 
diff --git a/src/spec/test/OperatorsTest.groovy b/src/spec/test/OperatorsTest.groovy
index ca0831b..e26c73c 100644
--- a/src/spec/test/OperatorsTest.groovy
+++ b/src/spec/test/OperatorsTest.groovy
@@ -280,6 +280,21 @@ assert user.@name == 'Bob'                   // <1>
             assert reference(123)   == 246                       // <5>
             // end::method_pointer_dispatch[]
         '''
+
+        assertScript '''
+            // tag::method_pointer_new[]
+            def foo  = BigInteger.&new
+            def fortyTwo = foo('42')
+            assert fortyTwo == 42G
+            // end::method_pointer_new[]
+        '''
+
+        assertScript '''
+            // tag::method_pointer_class_instance[]
+            def instanceMethod = String.&toUpperCase
+            assert instanceMethod('foo') == 'FOO'
+            // end::method_pointer_class_instance[]
+        '''
     }
 
     void testMethodReference() {