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