"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/transform/stc/MethodReferenceTest.groovy" (3 Sep 2021, 18476 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "MethodReferenceTest.groovy": 3.0.8_vs_3.0.9.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.transform.stc
   20 
   21 import groovy.test.GroovyTestCase
   22 
   23 class MethodReferenceTest extends GroovyTestCase {
   24     // class::instanceMethod
   25     void testFunctionCI() {
   26         assertScript '''
   27             import java.util.stream.Collectors
   28 
   29             @groovy.transform.CompileStatic
   30             void p() {
   31                 def result = [1, 2, 3].stream().map(Object::toString).collect(Collectors.toList())
   32                 assert result == ['1', '2', '3']
   33             }
   34 
   35             p()
   36         '''
   37     }
   38 
   39     // class::instanceMethod
   40     void testFunctionCI2() {
   41         assertScript '''
   42             import java.util.stream.Collectors
   43 
   44             @groovy.transform.CompileStatic
   45             void p() {
   46                 def result = [1, 2, 3].stream().map(Integer::toString).collect(Collectors.toList())
   47                 assert result == ['1', '2', '3']
   48             }
   49 
   50             p()
   51         '''
   52     }
   53 
   54     // class::instanceMethod -- GROOVY-10047
   55     void testFunctionCI3() {
   56         assertScript '''
   57             import java.util.function.Function
   58             import static java.util.stream.Collectors.toMap
   59 
   60             @groovy.transform.CompileStatic
   61             void p() {
   62                 List<String> list = ['a','bc','def']
   63                 Function<String,String> self = str -> str // help for toMap
   64                 def map = list.stream().collect(toMap(self, String::length))
   65                 assert map == [a: 1, bc: 2, 'def': 3]
   66             }
   67 
   68             p()
   69         '''
   70 
   71         assertScript '''
   72             import java.util.function.Function
   73             import static java.util.stream.Collectors.toMap
   74 
   75             @groovy.transform.CompileStatic
   76             void p() {
   77                 List<String> list = ['a','bc','def']
   78                 // TODO: inference for T in toMap(Function<? super T,...>, Function<? super T,...>)
   79                 def map = list.stream().collect(toMap(Function.<String>identity(), String::length))
   80                 assert map == [a: 1, bc: 2, 'def': 3]
   81             }
   82 
   83             p()
   84         '''
   85     }
   86 
   87     // class::instanceMethod
   88     void testFunctionCI4() {
   89         def err = shouldFail '''
   90             import static java.util.stream.Collectors.toList
   91 
   92             @groovy.transform.CompileStatic
   93             void p() {
   94                 def result = [1, 2, 3].stream().map(String::toString).collect(toList())
   95                 assert result == ["1", "2", "3"]
   96             }
   97 
   98             p()
   99         '''
  100 
  101         assert err =~ /Invalid receiver type: java.lang.Integer is not compatible with java.lang.String/
  102     }
  103 
  104     // class::instanceMethod -- GROOVY-9814
  105     void testFunctionCI5() {
  106         assertScript '''
  107             import java.util.function.*
  108             import groovy.transform.*
  109 
  110             @CompileStatic
  111             class One { String id }
  112 
  113             @CompileStatic
  114             class Two extends One { }
  115 
  116             @CompileStatic @Immutable(knownImmutableClasses=[Function])
  117             class FunctionHolder<T> {
  118                 Function<T, ?> extractor
  119 
  120                 def apply(T t) {
  121                     extractor.apply(t)
  122                 }
  123             }
  124 
  125             def fh = new FunctionHolder(One::getId)
  126             assert fh.apply(new One(id:'abc')) == 'abc'
  127 
  128             fh = new FunctionHolder(One::getId)
  129             assert fh.apply(new Two(id:'xyz')) == 'xyz' // sub-type argument
  130         '''
  131     }
  132 
  133     // class::instanceMethod -- GROOVY-9974
  134     void testPredicateCI() {
  135         assertScript '''
  136             @groovy.transform.CompileStatic
  137             void test(List<String> strings = ['']) {
  138                 strings.removeIf(String::isEmpty)
  139                 assert strings.isEmpty()
  140             }
  141             test()
  142         '''
  143     }
  144 
  145     // class::instanceMethod
  146     void testBinaryOperatorCI() {
  147         assertScript '''
  148             @groovy.transform.CompileStatic
  149             void p() {
  150                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, BigDecimal::add)
  151 
  152                 assert 6.0G == result
  153             }
  154 
  155             p()
  156         '''
  157     }
  158 
  159     // instance::instanceMethod
  160     void testBinaryOperatorII() {
  161         assertScript '''
  162             @groovy.transform.CompileStatic
  163             void p() {
  164                 Adder adder = new Adder()
  165                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, adder::add)
  166 
  167                 assert 6.0G == result
  168             }
  169 
  170             p()
  171 
  172             @groovy.transform.CompileStatic
  173             class Adder {
  174                 BigDecimal add(BigDecimal a, BigDecimal b) {
  175                     a.add(b)
  176                 }
  177             }
  178         '''
  179     }
  180 
  181     // instance::instanceMethod
  182     void testBinaryOperatorII_COMPATIBLE() {
  183         assertScript '''
  184             @groovy.transform.CompileStatic
  185             void p() {
  186                 Adder adder = new Adder()
  187                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, adder::add)
  188 
  189                 assert 6.0G == result
  190             }
  191 
  192             p()
  193 
  194             @groovy.transform.CompileStatic
  195             class Adder {
  196                 BigDecimal add(Number a, Number b) {
  197                     ((BigDecimal) a).add((BigDecimal) b)
  198                 }
  199             }
  200         '''
  201     }
  202 
  203     // expression::instanceMethod
  204     void testBinaryOperatorII_EXPRESSION() {
  205         assertScript '''
  206             @groovy.transform.CompileStatic
  207             void p() {
  208                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, new Adder()::add)
  209 
  210                 assert 6.0G == result
  211             }
  212 
  213             p()
  214 
  215             @groovy.transform.CompileStatic
  216             class Adder {
  217                 public BigDecimal add(BigDecimal a, BigDecimal b) {
  218                     a.add(b)
  219                 }
  220             }
  221         '''
  222     }
  223 
  224     // expression::instanceMethod
  225     void testBinaryOperatorII_EXPRESSION2() {
  226         assertScript '''
  227             @groovy.transform.CompileStatic
  228             void p() {
  229                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, new Adder().getThis()::add)
  230 
  231                 assert new BigDecimal(6) == result
  232             }
  233 
  234             p()
  235 
  236             @groovy.transform.CompileStatic
  237             class Adder {
  238                 BigDecimal add(BigDecimal a, BigDecimal b) {
  239                     a.add(b)
  240                 }
  241 
  242                 Adder getThis() {
  243                     return this
  244                 }
  245             }
  246         '''
  247     }
  248 
  249     // instance::staticMethod
  250     void testBinaryOperatorIS() {
  251         assertScript '''
  252             @groovy.transform.CompileStatic
  253             void p() {
  254                 Adder adder = new Adder()
  255                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, adder::add)
  256 
  257                 assert 6.0G == result
  258             }
  259 
  260             p()
  261 
  262             @groovy.transform.CompileStatic
  263             class Adder {
  264                 static BigDecimal add(BigDecimal a, BigDecimal b) {
  265                     a.add(b)
  266                 }
  267             }
  268         '''
  269     }
  270 
  271     // expression::staticMethod
  272     void testBinaryOperatorIS_EXPRESSION() {
  273         assertScript '''
  274             @groovy.transform.CompileStatic
  275             void p() {
  276                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, new Adder()::add)
  277 
  278                 assert 6.0G == result
  279             }
  280 
  281             p()
  282 
  283             @groovy.transform.CompileStatic
  284             class Adder {
  285                 static BigDecimal add(BigDecimal a, BigDecimal b) {
  286                     a.add(b)
  287                 }
  288             }
  289         '''
  290     }
  291 
  292     // expression::staticMethod
  293     void testBinaryOperatorIS_EXPRESSION2() {
  294         assertScript '''
  295             @groovy.transform.CompileStatic
  296             void p() {
  297                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, Adder.newInstance()::add)
  298 
  299                 assert 6.0G == result
  300             }
  301 
  302             p()
  303 
  304             @groovy.transform.CompileStatic
  305             class Adder {
  306                 static BigDecimal add(BigDecimal a, BigDecimal b) {
  307                     a.add(b)
  308                 }
  309 
  310                 static Adder newInstance() {
  311                     new Adder()
  312                 }
  313             }
  314         '''
  315     }
  316 
  317     // arrayClass::new
  318     void testFunctionCN() {
  319         assertScript '''
  320             @groovy.transform.CompileStatic
  321             void p() {
  322                 def result = [1, 2, 3].stream().toArray(Integer[]::new)
  323                 assert result == new Integer[] { 1, 2, 3 }
  324             }
  325 
  326             p()
  327         '''
  328     }
  329 
  330     // class::new
  331     void testFunctionCN2() {
  332         assertScript '''
  333             import static java.util.stream.Collectors.toList
  334 
  335             @groovy.transform.CompileStatic
  336             void p() {
  337                 def result = ["1", "2", "3"].stream().map(Integer::new).collect(toList())
  338                 assert result == [1, 2, 3]
  339             }
  340 
  341             p()
  342         '''
  343     }
  344 
  345     // class::new -- GROOVY-10033
  346     void testFunctionCN3() {
  347         assertScript '''
  348             import java.util.function.Function
  349 
  350             @groovy.transform.CompileStatic
  351             class C {
  352                 C(Function<String,Integer> f) {
  353                     def i = f.apply('42')
  354                     assert i == 42
  355                 }
  356                 static test() {
  357                     new C(Integer::new)
  358                 }
  359             }
  360             C.test()
  361         '''
  362     }
  363 
  364     // class::new -- GROOVY-10033
  365     void testFunctionCN4() {
  366         assertScript '''
  367             import java.util.function.Function
  368 
  369             class A {
  370                 A(Function<A,B> f) {
  371                     B b = f.apply(this)
  372                     assert b instanceof X.Y
  373                 }
  374             }
  375             class B {
  376                 B(A a) {
  377                     assert a != null
  378                 }
  379             }
  380             @groovy.transform.CompileStatic
  381             class X extends A {
  382               public X() {
  383                 super(Y::new)
  384               }
  385               private static class Y extends B {
  386                 Y(A a) {
  387                   super(a)
  388                 }
  389               }
  390             }
  391 
  392             new X()
  393         '''
  394     }
  395 
  396     // class::staticMethod
  397     void testFunctionCS() {
  398         assertScript '''
  399             import static java.util.stream.Collectors.toList
  400 
  401             @groovy.transform.CompileStatic
  402             void p() {
  403                 def result = [1, -2, 3].stream().map(Math::abs).collect(toList())
  404                 assert [1, 2, 3] == result
  405             }
  406 
  407             p()
  408         '''
  409     }
  410 
  411     // class::staticMethod
  412     void testFunctionCS2() {
  413         assertScript '''
  414             import java.util.function.Function
  415             import static java.util.stream.Collectors.toMap
  416 
  417             @groovy.transform.CompileStatic
  418             void p() {
  419                 List<String> list = ['x','y','z']
  420                 def map = list.stream().collect(toMap(Function.identity(), Collections::singletonList))
  421                 assert map == [x: ['x'], y: ['y'], z: ['z']]
  422             }
  423 
  424             p()
  425         '''
  426     }
  427 
  428     // class::staticMethod -- GROOVY-9799
  429     void testFunctionCS3() {
  430         assertScript '''
  431             class C {
  432                 String x
  433             }
  434 
  435             class D {
  436                 String x
  437                 static D from(C c) {
  438                     new D(x: c.x)
  439                 }
  440             }
  441 
  442             @groovy.transform.CompileStatic
  443             def test(C c) {
  444                 Optional.of(c).map(D::from).get()
  445             }
  446 
  447             def d = test(new C(x: 'x'))
  448             assert d.x == 'x'
  449         '''
  450     }
  451 
  452     // class::staticMethod
  453     void testFunctionCS_RHS() {
  454         assertScript '''
  455             import java.util.function.Function
  456             import java.util.stream.Collectors
  457 
  458             @groovy.transform.CompileStatic
  459             void p() {
  460                 Function<Integer, Integer> f = Math::abs
  461                 def result = [1, -2, 3].stream().map(f).collect(Collectors.toList())
  462 
  463                 assert [1, 2, 3] == result
  464             }
  465 
  466             p()
  467         '''
  468     }
  469 
  470     // class::staticMethod
  471     void testFunctionCS_RHS_NOTYPE() {
  472         assertScript '''
  473             import java.util.stream.Collectors
  474 
  475             @groovy.transform.CompileStatic
  476             void p() {
  477                 def f = Math::abs // No explicit type defined, so it is actually a method closure. We can make it smarter in a later version.
  478                 def result = [1, -2, 3].stream().map(f).collect(Collectors.toList())
  479 
  480                 assert [1, 2, 3] == result
  481             }
  482 
  483             p()
  484         '''
  485     }
  486 
  487     // instance::instanceMethod
  488     void testBinaryOperatorII_RHS() {
  489         assertScript '''
  490             import java.util.function.BinaryOperator
  491 
  492             @groovy.transform.CompileStatic
  493             void p() {
  494                 Adder adder = new Adder()
  495                 BinaryOperator<BigDecimal> b = adder::add
  496                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, b)
  497 
  498                 assert 6.0G == result
  499             }
  500 
  501             p()
  502 
  503             @groovy.transform.CompileStatic
  504             class Adder {
  505                 BigDecimal add(BigDecimal a, BigDecimal b) {
  506                     a.add(b)
  507                 }
  508             }
  509         '''
  510     }
  511 
  512     // expression::instanceMethod
  513     void testBinaryOperatorII_RHS2() {
  514         assertScript '''
  515             import java.util.function.BinaryOperator
  516 
  517             @groovy.transform.CompileStatic
  518             void p() {
  519                 BinaryOperator<BigDecimal> b = new Adder()::add
  520                 def result = [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, b)
  521 
  522                 assert 6.0G == result
  523             }
  524 
  525             p()
  526 
  527             @groovy.transform.CompileStatic
  528             class Adder {
  529                 BigDecimal add(BigDecimal a, BigDecimal b) {
  530                     a.add(b)
  531                 }
  532             }
  533         '''
  534     }
  535 
  536     // class::new
  537     void testFunctionCN_RHS() {
  538         assertScript '''
  539             import java.util.function.Function
  540             import java.util.stream.Collectors
  541 
  542             @groovy.transform.CompileStatic
  543             void p() {
  544                 Function<String, Integer> f = Integer::new
  545                 assert [1, 2, 3] == ["1", "2", "3"].stream().map(f).collect(Collectors.toList())
  546             }
  547 
  548             p()
  549         '''
  550     }
  551 
  552     // arrayClass::new
  553     void testIntFunctionCN_RHS() {
  554         assertScript '''
  555             import java.util.function.IntFunction
  556             import java.util.stream.Stream
  557 
  558             @groovy.transform.CompileStatic
  559             void p() {
  560                 IntFunction<Integer[]> f = Integer[]::new
  561                 assert new Integer[] { 1, 2, 3 } == [1, 2, 3].stream().toArray(f)
  562             }
  563 
  564             p()
  565         '''
  566     }
  567 
  568     void testMethodNotFound() {
  569         def errMsg = shouldFail '''
  570             @groovy.transform.CompileStatic
  571             void p() {
  572                 [1.0G, 2.0G, 3.0G].stream().reduce(0.0G, BigDecimal::addx)
  573             }
  574 
  575             p()
  576         '''
  577 
  578         assert errMsg.contains('Failed to find the expected method[addx(java.math.BigDecimal,java.math.BigDecimal)] in the type[java.math.BigDecimal]')
  579     }
  580 
  581     // class::instanceMethod
  582     void testFunctionCI_WRONGTYPE() {
  583         def errMsg = shouldFail '''
  584             import java.util.stream.Collectors
  585 
  586             @groovy.transform.CompileStatic
  587             void p() {
  588                 def result = [1, 2, 3].stream().map(String::toString).collect(Collectors.toList())
  589                 assert 3 == result.size()
  590                 assert ['1', '2', '3'] == result
  591             }
  592 
  593             p()
  594         '''
  595 
  596         assert errMsg.contains('Invalid receiver type: java.lang.Integer is not compatible with java.lang.String')
  597     }
  598 
  599     // class::instanceMethod, actually class::staticMethod
  600     void testFunctionCI_DGM() {
  601         assertScript '''
  602             import java.util.stream.Collectors
  603 
  604             @groovy.transform.CompileStatic
  605             void p() {
  606                 def result = ['a', 'ab', 'abc'].stream().map(String::size).collect(Collectors.toList())
  607                 assert [1, 2, 3] == result
  608             }
  609 
  610             p()
  611         '''
  612     }
  613 
  614     // class::staticMethod
  615     void testFunctionCS_DGSM() {
  616         assertScript '''
  617             import java.util.stream.Collectors
  618 
  619             @groovy.transform.CompileStatic
  620             void p() {
  621                 def result = [{}, {}, {}].stream().map(Thread::startDaemon).collect(Collectors.toList())
  622                 assert result.every(e -> e instanceof Thread)
  623             }
  624 
  625             p()
  626         '''
  627     }
  628 
  629     // class::instanceMethod
  630     void testFunctionCI_SHADOW_DGM() {
  631         assertScript '''
  632             import java.util.stream.Collectors
  633 
  634             @groovy.transform.CompileStatic
  635             void p() {
  636                 def result = [[a:1], [b:2], [c:3]].stream().map(Object::toString).collect(Collectors.toList())
  637                 assert 3 == result.size()
  638                 assert ['[a:1]', '[b:2]', '[c:3]'] == result
  639             }
  640 
  641             p()
  642         '''
  643     }
  644 
  645     // class::staticMethod
  646     void testFunctionCS_MULTI_DGSM() {
  647         assertScript '''
  648             import java.util.stream.Collectors
  649 
  650             @groovy.transform.CompileStatic
  651             void p() {
  652                 def result = [{}, {}, {}].stream().map(Thread::startDaemon).collect(Collectors.toList())
  653                 assert result.every(e -> e instanceof Thread)
  654 
  655                 result = [{}, {}, {}].stream().map(Thread::startDaemon).collect(Collectors.toList())
  656                 assert result.every(e -> e instanceof Thread)
  657             }
  658 
  659             p()
  660         '''
  661     }
  662 }