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