1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @modules jdk.incubator.code
27  * @run junit TestMethodRefLambda
28  */
29 
30 import jdk.incubator.code.Op;
31 import jdk.incubator.code.Quotable;
32 import jdk.incubator.code.Quoted;
33 import jdk.incubator.code.dialect.java.JavaOp;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.TestInstance;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.MethodSource;
38 
39 import java.util.List;
40 import java.util.function.BiFunction;
41 import java.util.function.Function;
42 import java.util.function.IntUnaryOperator;
43 
44 // Declare a per-class lifecycle, so we can declare an instance factory method
45 // as the source of arguments for parameterized tests
46 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
47 public class TestMethodRefLambda {
48 
49     interface QuotableIntUnaryOperator extends IntUnaryOperator, Quotable {}
50 
51     interface QuotableFunction<T, R> extends Function<T, R>, Quotable {}
52 
53     interface QuotableBiFunction<T, U, R> extends BiFunction<T, U, R>, Quotable {}
54 
55     List<Quotable> methodRefLambdas() {
56         return List.of(
57                 (QuotableIntUnaryOperator) TestMethodRefLambda::m1,
58                 (QuotableIntUnaryOperator) TestMethodRefLambda::m2,
59                 (QuotableFunction<Integer, Integer>) TestMethodRefLambda::m1,
60                 (QuotableFunction<Integer, Integer>) TestMethodRefLambda::m2,
61                 (QuotableIntUnaryOperator) this::m3,
62                 (QuotableBiFunction<TestMethodRefLambda, Integer, Integer>) TestMethodRefLambda::m4
63         );
64     }
65 
66     @ParameterizedTest
67     @MethodSource("methodRefLambdas")
68     public void testIsMethodReference(Quotable q) {
69         Quoted quoted = Op.ofQuotable(q).get();
70         JavaOp.LambdaOp lop = (JavaOp.LambdaOp) quoted.op();
71         Assertions.assertTrue(lop.methodReference().isPresent());
72     }
73 
74     static int m1(int i) {
75         return i;
76     }
77 
78     static Integer m2(Integer i) {
79         return i;
80     }
81 
82     int m3(int i) {
83         return i;
84     }
85 
86     static int m4(TestMethodRefLambda tl, int i) {
87         return i;
88     }
89 }