1 /*
2 * Copyright (c) 2024, 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 jdk.incubator.code.Reflect;
25
26 /*
27 * @test
28 * @summary Smoke test for code reflection with intersection type conversions.
29 * @modules jdk.incubator.code
30 * @build IntersectionTypeTest
31 * @build CodeReflectionTester
32 * @run main CodeReflectionTester IntersectionTypeTest
33 */
34
35 class IntersectionTypeTest {
36 interface A {
37 Object f_A = 5;
38 void m_A();
39 }
40
41 interface B {
42 Object f_B = 5;
43 void m_B();
44 }
45
46 interface C {
47 Object f_C = 5;
48 void m_C();
49 }
50
51 @Reflect
52 @IR("""
53 func @"test1" (%0 : java.type:"&IntersectionTypeTest::test1(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)")java.type:"void" -> {
54 %1 : Var<java.type:"&IntersectionTypeTest::test1(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>"> = var %0 @"x";
55 %2 : java.type:"&IntersectionTypeTest::test1(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
56 invoke %2 @java.ref:"IntersectionTypeTest$A::m_A():void";
57 %3 : java.type:"&IntersectionTypeTest::test1(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
58 %4 : java.type:"IntersectionTypeTest$B" = cast %3 @java.type:"IntersectionTypeTest$B";
59 invoke %4 @java.ref:"IntersectionTypeTest$B::m_B():void";
60 %5 : java.type:"&IntersectionTypeTest::test1(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
61 %6 : java.type:"IntersectionTypeTest$C" = cast %5 @java.type:"IntersectionTypeTest$C";
62 invoke %6 @java.ref:"IntersectionTypeTest$C::m_C():void";
63 return;
64 };
65 """)
66 static <X extends A & B & C> void test1(X x) {
67 x.m_A();
68 x.m_B();
69 x.m_C();
70 }
71
72 // #X<&m<IntersectionTypeTest, test2, func<void, IntersectionTypeTest$A>>, IntersectionTypeTest$A>
73 // #X<&m<IntersectionTypeTest, test2, func<void, IntersectionTypeTest$A>, IntersectionTypeTest$A>
74 @Reflect
75 @IR("""
76 func @"test2" (%0 : java.type:"&IntersectionTypeTest::test2(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)")java.type:"void" -> {
77 %1 : Var<java.type:"&IntersectionTypeTest::test2(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)"> = var %0 @"x";
78 %2 : java.type:"IntersectionTypeTest::test2(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
79 %3 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$A::f_A:java.lang.Object";
80 %4 : Var<java.type:"java.lang.Object"> = var %3 @"oA";
81 %5 : java.type:"&IntersectionTypeTest::test2(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
82 %6 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$B::f_B:java.lang.Object";
83 %7 : Var<java.type:"java.lang.Object"> = var %6 @"oB";
84 %8 : java.type:"&IntersectionTypeTest::test2(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
85 %9 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$C::f_C:java.lang.Object";
86 %10 : Var<java.type:"java.lang.Object"> = var %9 @"oC";
87 return;
88 };
89 """)
90 static <X extends A & B & C> void test2(X x) {
91 Object oA = x.f_A;
92 Object oB = x.f_B;
93 Object oC = x.f_C;
94 }
95
96 @Reflect
97 @IR("""
98 func @"test3" (%0 : java.type:"&IntersectionTypeTest::test3(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>")java.type:"void" -> {
99 %1 : Var<java.type:"&IntersectionTypeTest::test3(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>"> = var %0 @"x";
100 %2 : java.type:"&IntersectionTypeTest::test3(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
101 %3 : java.type:"java.lang.Object" = invoke %2 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
102 %4 : Var<java.type:"IntersectionTypeTest$A"> = var %2 @"rec$";
103 %5 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
104 %6 : java.type:"IntersectionTypeTest$A" = var.load %4;
105 invoke %6 @java.ref:"IntersectionTypeTest$A::m_A():void";
106 return;
107 };
108 %7 : Var<java.type:"java.lang.Runnable"> = var %5 @"rA";
109 %8 : java.type:"&IntersectionTypeTest::test3(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
110 %9 : java.type:"java.lang.Object" = invoke %8 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
111 %10 : java.type:"IntersectionTypeTest$B" = cast %8 @java.type:"IntersectionTypeTest$B";
112 %11 : Var<java.type:"IntersectionTypeTest$B"> = var %10 @"rec$";
113 %12 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
114 %13 : java.type:"IntersectionTypeTest$B" = var.load %11;
115 invoke %13 @java.ref:"IntersectionTypeTest$B::m_B():void";
116 return;
117 };
118 %14 : Var<java.type:"java.lang.Runnable"> = var %12 @"rB";
119 %15 : java.type:"&IntersectionTypeTest::test3(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>" = var.load %1;
120 %16 : java.type:"java.lang.Object" = invoke %15 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
121 %17 : java.type:"IntersectionTypeTest$C" = cast %15 @java.type:"IntersectionTypeTest$C";
122 %18 : Var<java.type:"IntersectionTypeTest$C"> = var %17 @"rec$";
123 %19 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
124 %20 : java.type:"IntersectionTypeTest$C" = var.load %18;
125 invoke %20 @java.ref:"IntersectionTypeTest$C::m_C():void";
126 return;
127 };
128 %21 : Var<java.type:"java.lang.Runnable"> = var %19 @"rC";
129 return;
130 };
131 """)
132 static <X extends A & B & C> void test3(X x) {
133 Runnable rA = x::m_A;
134 Runnable rB = x::m_B;
135 Runnable rC = x::m_C;
136 }
137
138 static void g_A(A a) { }
139 static void g_B(B a) { }
140 static void g_C(C a) { }
141
142 @Reflect
143 @IR("""
144 func @"test4" (%0 : java.type:"&IntersectionTypeTest::test4(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)")java.type:"void" -> {
145 %1 : Var<java.type:"&IntersectionTypeTest::test4(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)"> = var %0 @"x";
146 %2 : java.type:"&IntersectionTypeTest::test4(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
147 invoke %2 @java.ref:"IntersectionTypeTest::g_A(IntersectionTypeTest$A):void";
148 %3 : java.type:"&IntersectionTypeTest::test4(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
149 %4 : java.type:"IntersectionTypeTest$B" = cast %3 @java.type:"IntersectionTypeTest$B";
150 invoke %4 @java.ref:"IntersectionTypeTest::g_B(IntersectionTypeTest$B):void";
151 %5 : java.type:"&IntersectionTypeTest::test4(IntersectionTypeTest$A):void::<X extends IntersectionTypeTest$A>)" = var.load %1;
152 %6 : java.type:"IntersectionTypeTest$C" = cast %5 @java.type:"IntersectionTypeTest$C";
153 invoke %6 @java.ref:"IntersectionTypeTest::g_C(IntersectionTypeTest$C):void";
154 return;
155 };
156 """)
157 static <X extends A & B & C> void test4(X x) {
158 g_A(x);
159 g_B(x);
160 g_C(x);
161 }
162
163 static <X extends A & B & C> X makeIntersection(X x1, X x2) {
164 return null;
165 }
166
167 static class E1 implements A, B, C {
168 @Override
169 public void m_A() { }
170 @Override
171 public void m_B() { }
172 @Override
173 public void m_C() { }
174 }
175
176 static class E2 implements A, B, C {
177 @Override
178 public void m_A() { }
179 @Override
180 public void m_B() { }
181 @Override
182 public void m_C() { }
183 }
184
185 @Reflect
186 @IR("""
187 func @"test5" (%0 : java.type:"IntersectionTypeTest$E1", %1 : java.type:"IntersectionTypeTest$E2")java.type:"void" -> {
188 %2 : Var<java.type:"IntersectionTypeTest$E1"> = var %0 @"e1";
189 %3 : Var<java.type:"IntersectionTypeTest$E2"> = var %1 @"e2";
190 %4 : java.type:"IntersectionTypeTest$E1" = var.load %2;
191 %5 : java.type:"IntersectionTypeTest$E2" = var.load %3;
192 %6 : java.type:"IntersectionTypeTest$A" = invoke %4 %5 @java.ref:"IntersectionTypeTest::makeIntersection(IntersectionTypeTest$A, IntersectionTypeTest$A):IntersectionTypeTest$A";
193 %7 : Var<java.type:"IntersectionTypeTest$A"> = var %6 @"x";
194 %8 : java.type:"IntersectionTypeTest$A" = var.load %7;
195 invoke %8 @java.ref:"IntersectionTypeTest$A::m_A():void";
196 %9 : java.type:"IntersectionTypeTest$A" = var.load %7;
197 %10 : java.type:"IntersectionTypeTest$B" = cast %9 @java.type:"IntersectionTypeTest$B";
198 invoke %10 @java.ref:"IntersectionTypeTest$B::m_B():void";
199 %11 : java.type:"IntersectionTypeTest$A" = var.load %7;
200 %12 : java.type:"IntersectionTypeTest$C" = cast %11 @java.type:"IntersectionTypeTest$C";
201 invoke %12 @java.ref:"IntersectionTypeTest$C::m_C():void";
202 return;
203 };
204 """)
205 static void test5(E1 e1, E2 e2) {
206 var x = makeIntersection(e1, e2);
207 x.m_A();
208 x.m_B();
209 x.m_C();
210 }
211
212 @Reflect
213 @IR("""
214 func @"test6" (%0 : java.type:"IntersectionTypeTest$E1", %1 : java.type:"IntersectionTypeTest$E2")java.type:"void" -> {
215 %2 : Var<java.type:"IntersectionTypeTest$E1"> = var %0 @"e1";
216 %3 : Var<java.type:"IntersectionTypeTest$E2"> = var %1 @"e2";
217 %4 : java.type:"IntersectionTypeTest$E1" = var.load %2;
218 %5 : java.type:"IntersectionTypeTest$E2" = var.load %3;
219 %6 : java.type:"IntersectionTypeTest$A" = invoke %4 %5 @java.ref:"IntersectionTypeTest::makeIntersection(IntersectionTypeTest$A, IntersectionTypeTest$A):IntersectionTypeTest$A";
220 %7 : Var<java.type:"IntersectionTypeTest$A"> = var %6 @"x";
221 %8 : java.type:"IntersectionTypeTest$A" = var.load %7;
222 %9 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$A::f_A:java.lang.Object";
223 %10 : Var<java.type:"java.lang.Object"> = var %9 @"oA";
224 %11 : java.type:"IntersectionTypeTest$A" = var.load %7;
225 %12 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$B::f_B:java.lang.Object";
226 %13 : Var<java.type:"java.lang.Object"> = var %12 @"oB";
227 %14 : java.type:"IntersectionTypeTest$A" = var.load %7;
228 %15 : java.type:"java.lang.Object" = field.load @java.ref:"IntersectionTypeTest$C::f_C:java.lang.Object";
229 %16 : Var<java.type:"java.lang.Object"> = var %15 @"oC";
230 return;
231 };
232 """)
233 static void test6(E1 e1, E2 e2) {
234 var x = makeIntersection(e1, e2);
235 Object oA = x.f_A;
236 Object oB = x.f_B;
237 Object oC = x.f_C;
238 }
239
240 @Reflect
241 @IR("""
242 func @"test7" (%0 : java.type:"IntersectionTypeTest$E1", %1 : java.type:"IntersectionTypeTest$E2")java.type:"void" -> {
243 %2 : Var<java.type:"IntersectionTypeTest$E1"> = var %0 @"e1";
244 %3 : Var<java.type:"IntersectionTypeTest$E2"> = var %1 @"e2";
245 %4 : java.type:"IntersectionTypeTest$E1" = var.load %2;
246 %5 : java.type:"IntersectionTypeTest$E2" = var.load %3;
247 %6 : java.type:"IntersectionTypeTest$A" = invoke %4 %5 @java.ref:"IntersectionTypeTest::makeIntersection(IntersectionTypeTest$A, IntersectionTypeTest$A):IntersectionTypeTest$A";
248 %7 : Var<java.type:"IntersectionTypeTest$A"> = var %6 @"x";
249 %8 : java.type:"IntersectionTypeTest$A" = var.load %7;
250 %9 : java.type:"java.lang.Object" = invoke %8 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
251 %10 : Var<java.type:"IntersectionTypeTest$A"> = var %8 @"rec$";
252 %11 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
253 %12 : java.type:"IntersectionTypeTest$A" = var.load %10;
254 invoke %12 @java.ref:"IntersectionTypeTest$A::m_A():void";
255 return;
256 };
257 %13 : Var<java.type:"java.lang.Runnable"> = var %11 @"rA";
258 %14 : java.type:"IntersectionTypeTest$A" = var.load %7;
259 %15 : java.type:"java.lang.Object" = invoke %14 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
260 %16 : java.type:"IntersectionTypeTest$B" = cast %14 @java.type:"IntersectionTypeTest$B";
261 %17 : Var<java.type:"IntersectionTypeTest$B"> = var %16 @"rec$";
262 %18 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
263 %19 : java.type:"IntersectionTypeTest$B" = var.load %17;
264 invoke %19 @java.ref:"IntersectionTypeTest$B::m_B():void";
265 return;
266 };
267 %20 : Var<java.type:"java.lang.Runnable"> = var %18 @"rB";
268 %21 : java.type:"IntersectionTypeTest$A" = var.load %7;
269 %22 : java.type:"java.lang.Object" = invoke %21 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
270 %23 : java.type:"IntersectionTypeTest$C" = cast %21 @java.type:"IntersectionTypeTest$C";
271 %24 : Var<java.type:"IntersectionTypeTest$C"> = var %23 @"rec$";
272 %25 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
273 %26 : java.type:"IntersectionTypeTest$C" = var.load %24;
274 invoke %26 @java.ref:"IntersectionTypeTest$C::m_C():void";
275 return;
276 };
277 %27 : Var<java.type:"java.lang.Runnable"> = var %25 @"rC";
278 return;
279 };
280 """)
281 static void test7(E1 e1, E2 e2) {
282 var x = makeIntersection(e1, e2);
283 Runnable rA = x::m_A;
284 Runnable rB = x::m_B;
285 Runnable rC = x::m_C;
286 }
287
288 @Reflect
289 @IR("""
290 func @"test8" (%0 : java.type:"IntersectionTypeTest$E1", %1 : java.type:"IntersectionTypeTest$E2")java.type:"void" -> {
291 %2 : Var<java.type:"IntersectionTypeTest$E1"> = var %0 @"e1";
292 %3 : Var<java.type:"IntersectionTypeTest$E2"> = var %1 @"e2";
293 %4 : java.type:"IntersectionTypeTest$E1" = var.load %2;
294 %5 : java.type:"IntersectionTypeTest$E2" = var.load %3;
295 %6 : java.type:"IntersectionTypeTest$A" = invoke %4 %5 @java.ref:"IntersectionTypeTest::makeIntersection(IntersectionTypeTest$A, IntersectionTypeTest$A):IntersectionTypeTest$A";
296 %7 : Var<java.type:"IntersectionTypeTest$A"> = var %6 @"x";
297 %8 : java.type:"IntersectionTypeTest$A" = var.load %7;
298 invoke %8 @java.ref:"IntersectionTypeTest::g_A(IntersectionTypeTest$A):void";
299 %9 : java.type:"IntersectionTypeTest$A" = var.load %7;
300 %10 : java.type:"IntersectionTypeTest$B" = cast %9 @java.type:"IntersectionTypeTest$B";
301 invoke %10 @java.ref:"IntersectionTypeTest::g_B(IntersectionTypeTest$B):void";
302 %11 : java.type:"IntersectionTypeTest$A" = var.load %7;
303 %12 : java.type:"IntersectionTypeTest$C" = cast %11 @java.type:"IntersectionTypeTest$C";
304 invoke %12 @java.ref:"IntersectionTypeTest::g_C(IntersectionTypeTest$C):void";
305 return;
306 };
307 """)
308 static void test8(E1 e1, E2 e2) {
309 var x = makeIntersection(e1, e2);
310 g_A(x);
311 g_B(x);
312 g_C(x);
313 }
314
315 static class F {
316 void m_F() {
317 }
318 }
319
320 static class G extends F {
321 }
322
323 static <X extends G & A> X makeIntersection(G g, A a) {
324 return null;
325 }
326
327 @Reflect
328 @IR("""
329 func @"test9" (%0 : java.type:"IntersectionTypeTest$G", %1 : java.type:"IntersectionTypeTest$A")java.type:"void" -> {
330 %2 : Var<java.type:"IntersectionTypeTest$G"> = var %0 @"g";
331 %3 : Var<java.type:"IntersectionTypeTest$A"> = var %1 @"a";
332 %4 : java.type:"IntersectionTypeTest$G" = var.load %2;
333 %5 : java.type:"IntersectionTypeTest$A" = var.load %3;
334 %6 : java.type:"IntersectionTypeTest$G" = invoke %4 %5 @java.ref:"IntersectionTypeTest::makeIntersection(IntersectionTypeTest$G, IntersectionTypeTest$A):IntersectionTypeTest$G";
335 %7 : Var<java.type:"IntersectionTypeTest$G"> = var %6 @"x";
336 %8 : java.type:"IntersectionTypeTest$G" = var.load %7;
337 %9 : java.type:"java.lang.Object" = invoke %8 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
338 %10 : Var<java.type:"IntersectionTypeTest$G"> = var %8 @"rec$";
339 %11 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
340 %12 : java.type:"IntersectionTypeTest$G" = var.load %10;
341 invoke %12 @java.ref:"IntersectionTypeTest$G::m_F():void";
342 return;
343 };
344 %13 : Var<java.type:"java.lang.Runnable"> = var %11 @"rG";
345 %14 : java.type:"IntersectionTypeTest$G" = var.load %7;
346 %15 : java.type:"java.lang.Object" = invoke %14 @java.ref:"java.util.Objects::requireNonNull(java.lang.Object):java.lang.Object";
347 %16 : java.type:"IntersectionTypeTest$A" = cast %14 @java.type:"IntersectionTypeTest$A";
348 %17 : Var<java.type:"IntersectionTypeTest$A"> = var %16 @"rec$";
349 %18 : java.type:"java.lang.Runnable" = lambda @lambda.isReflectable=true ()java.type:"void" -> {
350 %19 : java.type:"IntersectionTypeTest$A" = var.load %17;
351 invoke %19 @java.ref:"IntersectionTypeTest$A::m_A():void";
352 return;
353 };
354 %20 : Var<java.type:"java.lang.Runnable"> = var %18 @"rA";
355 return;
356 };
357 """)
358 static void test9(G g, A a) {
359 var x = makeIntersection(g, a);
360 Runnable rG = x::m_F;
361 Runnable rA = x::m_A;
362 }
363 }