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.MethodHandle;
25 import java.lang.invoke.MethodHandles;
26 import java.lang.reflect.Method;
27 import java.util.function.Function;
28 import jdk.incubator.code.Reflect;
29 import jdk.incubator.code.Op;
30 import jdk.incubator.code.bytecode.BytecodeGenerator;
31 import jdk.incubator.code.dialect.core.CoreOp;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.Test;
34
35 /*
36 * @test
37 * @bug 8392557
38 * @modules jdk.incubator.code
39 * @library ../lib/
40 * @library ../
41 * @enablePreview
42 * @run junit TestIntersectionTargetLambda
43 */
44 public class TestIntersectionTargetLambda {
45
46 interface Marker {}
47
48 @Reflect
49 public static String functionFirst() {
50 var f = (Function<String, String> & Marker) x -> x;
51 return f.apply("functionFirst");
52 }
53
54 @Test
55 public void testFunctionFirst() throws Throwable {
56 Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("functionFirst");
57 CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
58 MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
59 Assertions.assertEquals(functionFirst(), (String) handle.invokeExact());
60 }
61
62 @Reflect
63 public static String markerFirst() {
64 var f = (Marker & Function<String, String>) x -> x;
65 return f.apply("markerFirst");
66 }
67
68 @Test
69 public void testMarkerFirst() throws Throwable {
70 Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("markerFirst");
71 CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
72 MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
73 Assertions.assertEquals(markerFirst(), (String) handle.invokeExact());
74 }
75
76 interface Base {
77 String a();
78 String b();
79 String c();
80 }
81
82 interface A extends Base {
83 default String a() { return "A"; }
84 }
85
86 interface B extends Base {
87 default String b() { return "B"; }
88 }
89
90 @Reflect
91 public static String complex() {
92 var f = (A & B) () -> { return "C"; };
93 return f.c();
94 }
95
96 @Test
97 public void testComplex() throws Throwable {
98 Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("complex");
99 CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
100 MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
101 Assertions.assertEquals(complex(), (String) handle.invokeExact());
102 }
103
104 interface X {
105 String get();
106 }
107
108 interface Y {
109 String get();
110 }
111
112 interface Z extends X, Y {}
113
114 @Reflect
115 public static String overrideEquivalent() {
116 Z f = () -> "overrideEquivalent";
117 return f.get();
118 }
119
120 @Test
121 public void testOverrideEquivalent() throws Throwable {
122 Method accessMethod = TestIntersectionTargetLambda.class.getDeclaredMethod("overrideEquivalent");
123 CoreOp.FuncOp model = Op.ofMethod(accessMethod).orElseThrow();
124 MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(), model);
125 Assertions.assertEquals(overrideEquivalent(), (String) handle.invokeExact());
126 }
127 }