1 /*
2 * Copyright (c) 2026, 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 import java.lang.invoke.MethodHandles;
25 import java.lang.reflect.Method;
26 import java.util.Optional;
27 import java.util.function.Supplier;
28 import java.util.stream.Stream;
29 import jdk.incubator.code.Reflect;
30 import jdk.incubator.code.Op;
31 import jdk.incubator.code.bytecode.BytecodeGenerator;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.Test;
34 import resolve.Parent;
35
36 /*
37 * @test
38 * @bug 8392556 8397256
39 * @summary Test for code reflection with method reference expressions.
40 * @library ../lib/
41 * @library ../
42 * @modules jdk.incubator.code
43 * @enablePreview
44 * @run junit TestMethodReferenceAccess
45 */
46 public class TestMethodReferenceAccess {
47
48 @Reflect
49 public static Parent direct(Parent p) {
50 return p.setValue("ok");
51 }
52
53 @Reflect
54 public static Parent genericReference(Parent p) {
55 Optional.of("ok").ifPresent(p::setValue);
56 return p;
57 }
58
59 @Reflect
60 public static int nonGenericReference(Parent p) {
61 return Optional.of("ok").map(p::length).get();
62 }
63
64 @Reflect
65 public static <T extends Parent> T genericReferenceWithTypeVarReceiver(T p) {
66 Optional.of("ok").ifPresent(p::setValue);
67 return p;
68 }
69
70 @Reflect
71 public static <T extends Parent & Runnable> T genericReferenceWithIntersectionTypeVarReceiver(T p) {
72 Optional.of("ok").ifPresent(p::setValue);
73 return p;
74 }
75
76 @Reflect
77 public static Supplier<String> nullCheck(Object receiver) {
78 return receiver::toString;
79 }
80
81 @Test
82 public void testDirectCall() throws Throwable {
83 var parent = new Parent();
84 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "direct").orElseThrow()).orElseThrow();
85 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
86 Assertions.assertEquals(TestMethodReferenceAccess.direct(parent), handle.invoke(parent));
87 Assertions.assertEquals(TestMethodReferenceAccess.direct(parent), Interpreter.invoke(MethodHandles.lookup(), op, parent));
88 }
89
90 @Test
91 public void testGenericMethodReference() throws Throwable {
92 var parent = new Parent();
93 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "genericReference").orElseThrow()).orElseThrow();
94 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
95 Assertions.assertEquals(TestMethodReferenceAccess.genericReference(parent), handle.invoke(parent));
96 Assertions.assertEquals(TestMethodReferenceAccess.genericReference(parent), Interpreter.invoke(MethodHandles.lookup(), op, parent));
97 }
98
99 @Test
100 public void testNonGenericMethodReference() throws Throwable {
101 var parent = new Parent();
102 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "nonGenericReference").orElseThrow()).orElseThrow();
103 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
104 Assertions.assertEquals(TestMethodReferenceAccess.nonGenericReference(parent), handle.invoke(parent));
105 Assertions.assertEquals(TestMethodReferenceAccess.nonGenericReference(parent), Interpreter.invoke(MethodHandles.lookup(), op, parent));
106 }
107
108 @Test
109 public void testGenericMethodReferenceWithTypeVarReceiver() throws Throwable {
110 var parent = new Parent();
111 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "genericReferenceWithTypeVarReceiver").orElseThrow()).orElseThrow();
112 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
113 Assertions.assertEquals(TestMethodReferenceAccess.genericReferenceWithTypeVarReceiver(parent), handle.invoke(parent));
114 Assertions.assertEquals(TestMethodReferenceAccess.genericReferenceWithTypeVarReceiver(parent), Interpreter.invoke(MethodHandles.lookup(), op, parent));
115 }
116
117 @Test
118 public void testGenericMethodReferenceWithIntersectionTypeVarReceiver() throws Throwable {
119 var parent = new Parent();
120 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "genericReferenceWithIntersectionTypeVarReceiver").orElseThrow()).orElseThrow();
121 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
122 Assertions.assertEquals(TestMethodReferenceAccess.genericReferenceWithIntersectionTypeVarReceiver(parent), handle.invoke(parent));
123 Assertions.assertEquals(TestMethodReferenceAccess.genericReferenceWithIntersectionTypeVarReceiver(parent), Interpreter.invoke(MethodHandles.lookup(), op, parent));
124 }
125
126 @Test
127 public void testNullCheck() throws Throwable {
128 var op = Op.ofMethod(getMethod(TestMethodReferenceAccess.class, "nullCheck").orElseThrow()).orElseThrow();
129 var handle = BytecodeGenerator.generate(MethodHandles.lookup(), op);
130 Assertions.assertThrows(NullPointerException.class, () -> TestMethodReferenceAccess.nullCheck(null));
131 Assertions.assertThrows(NullPointerException.class, () -> handle.invoke(null));
132 Assertions.assertThrows(NullPointerException.class, () -> Interpreter.invoke(MethodHandles.lookup(), op, (Object) null));
133 }
134
135 static Optional<Method> getMethod(Class<?> c, String name) {
136 return Stream.of(c.getDeclaredMethods())
137 .filter(m -> m.getName().equals(name))
138 .findFirst();
139 }
140 }