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.reflect.Method;
25 import java.util.Arrays;
26 import jdk.incubator.code.Reflect;
27
28 /*
29 * @test
30 * @summary Verify reflected bridge generation
31 * @modules jdk.incubator.code
32 * @build BridgeTest
33 * @build CodeReflectionTester
34 * @run main BridgeTest
35 * @run main CodeReflectionTester BridgeTest$StringValue
36 */
37 public class BridgeTest {
38
39 interface Value<T> {
40 T value();
41 }
42
43 static final class StringValue implements Value<String> {
44 @Override
45 @Reflect
46 @IR("""
47 func @"value" (%0 : java.type:"BridgeTest$StringValue")java.type:"java.lang.String" -> {
48 %1 : java.type:"java.lang.String" = constant @"string value";
49 return %1;
50 };
51 """)
52 public String value() {
53 return "string value";
54 }
55 }
56
57 public static void main(String[] args) throws Exception {
58 var val = new StringValue().value();
59 if (!"string value".equals(val)) {
60 throw new AssertionError("Unexpected bridge invocation result: " + val);
61 }
62 Method bridge = Arrays.stream(StringValue.class.getDeclaredMethods())
63 .filter(Method::isBridge)
64 .findFirst()
65 .orElseThrow();
66 if (bridge.getReturnType() != Object.class) {
67 throw new AssertionError("Unexpected bridge return type: " + bridge.getReturnType());
68 }
69 }
70 }