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