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 import jdk.incubator.code.Reflect;
25 import jdk.incubator.code.Op;
26 import jdk.incubator.code.dialect.core.CoreOp;
27 import jdk.incubator.code.interpreter.Interpreter;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.MethodSource;
31
32 import java.lang.invoke.MethodHandles;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.util.List;
36
37 /*
38 * @test
39 * @modules jdk.incubator.code
40 * @run junit TestOverloads
41 * @run main Unreflect TestOverloads
42 * @run junit TestOverloads
43 */
44
45 public class TestOverloads {
46
47 @Reflect
48 static int f() {
49 return 0;
50 }
51
52 @Reflect
53 static int f(int i) {
54 return 1;
55 }
56
57 @Reflect
58 static int f(Integer i) {
59 return 2;
60 }
61
62 @Reflect
63 static int f(Object o) {
64 return 3;
65 }
66
67 @Reflect
68 static int f(List<Integer> l) {
69 return 4;
70 }
71
72 @Reflect
73 static int f(List<Integer> l, Object o) {
74 return 5;
75 }
76
77 public static Object[][] testData() {
78 return new Object[][]{
79 new Object[] {new Class[]{}, new Object[]{}},
80 new Object[] {new Class[]{int.class}, new Object[]{-1}},
81 new Object[] {new Class[]{Integer.class}, new Object[]{-1}},
82 new Object[] {new Class[]{Object.class}, new Object[]{"hello"}},
83 new Object[] {new Class[]{List.class}, new Object[]{List.of()}},
84 new Object[] {new Class[]{List.class, Object.class}, new Object[]{List.of(), -1}}
85 };
86 }
87
88 @ParameterizedTest
89 @MethodSource("testData")
90 public static void testOverloads(Class<?>[] paramTypes, Object[] params) {
91 try {
92 Class<TestOverloads> clazz = TestOverloads.class;
93 Method method = clazz.getDeclaredMethod("f", paramTypes);
94 CoreOp.FuncOp f = Op.ofMethod(method).orElseThrow();
95 var res1 = Interpreter.invoke(MethodHandles.lookup(), f, params);
96 var res2 = method.invoke(null, params);
97
98 Assertions.assertEquals(res2, res1);
99
100 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
101 throw new RuntimeException(e);
102 }
103 }
104 }