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.Reflect;
31 import jdk.incubator.code.Op;
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 @Reflect
50 List<Object> methodRefLambdas() {
51 return List.of(
52 (IntUnaryOperator) TestMethodRefLambda::m1,
53 (IntUnaryOperator) TestMethodRefLambda::m2,
54 (Function<Integer, Integer>) TestMethodRefLambda::m1,
55 (Function<Integer, Integer>) TestMethodRefLambda::m2,
56 (IntUnaryOperator) this::m3,
57 (BiFunction<TestMethodRefLambda, Integer, Integer>) TestMethodRefLambda::m4
58 );
59 }
60
61 @ParameterizedTest
62 @MethodSource("methodRefLambdas")
63 public void testIsMethodReference(Object q) {
64 Quoted quoted = Op.ofQuotable(q).get();
65 JavaOp.LambdaOp lop = (JavaOp.LambdaOp) quoted.op();
66 Assertions.assertTrue(lop.methodReference().isPresent());
67 }
68
69 static int m1(int i) {
70 return i;
71 }
72
73 static Integer m2(Integer i) {
74 return i;
75 }
76
77 int m3(int i) {
78 return i;
79 }
80
81 static int m4(TestMethodRefLambda tl, int i) {
82 return i;
83 }
84 }