1/*
2 * Copyright (c) 2016, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* @test
27 * @bug  8158169
28 * @summary unit tests for java.lang.invoke.MethodHandles
29 * @run testng test.java.lang.invoke.DropArgumentsTest
30 */
31package test.java.lang.invoke;
32
33import java.lang.invoke.MethodHandle;
34import java.lang.invoke.MethodHandles;
35import java.lang.invoke.MethodType;
36import java.util.Collections;
37import java.util.List;
38import static java.lang.invoke.MethodHandles.*;
39import static java.lang.invoke.MethodType.*;
40import static org.testng.AssertJUnit.*;
41import org.testng.annotations.*;
42
43public class DropArgumentsTest {
44
45    @Test
46    public void testDropArgumentsToMatch() throws Throwable {
47        MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
48        MethodType bigType = cat.type().insertParameterTypes(0, String.class, String.class, int.class);
49        MethodHandle d0 = MethodHandles.dropArgumentsToMatch(cat, 0, bigType.parameterList(), 3);
50        assertEquals("xy",(String)d0.invokeExact("m", "n", 1, "x", "y"));
51        MethodHandle d1 = MethodHandles.dropArgumentsToMatch(cat, 0, bigType.parameterList(), 0);
52        assertEquals("mn",(String)d1.invokeExact("m", "n", 1, "x", "y"));
53        MethodHandle d2 = MethodHandles.dropArgumentsToMatch(cat, 1, bigType.parameterList(), 4);
54        assertEquals("xy",(String)d2.invokeExact("x", "b", "c", 1, "a", "y"));
55
56    }
57
58    @DataProvider(name = "dropArgumentsToMatchNPEData")
59    private Object[][] dropArgumentsToMatchNPEData()
60            throws NoSuchMethodException, IllegalAccessException {
61        MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
62        return new Object[][] {
63                { (MethodHandle) null, 0, cat.type().parameterList(), 0 },
64                { cat, 0, null, 0 }
65        };
66    }
67
68    @Test(dataProvider = "dropArgumentsToMatchNPEData")
69    @ExpectedExceptions(NullPointerException.class)
70    public void dropArgumentsToMatchNPE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) {
71        MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip);
72    }
73
74    @DataProvider(name = "dropArgumentsToMatchIAEData")
75    private Object[][] dropArgumentsToMatchIAEData()
76        throws NoSuchMethodException, IllegalAccessException {
77        MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
78        MethodType bigType = cat.type().insertParameterTypes(0, String.class, String.class, int.class);
79        return new Object[][] {
80            {cat, -1, bigType.parameterList(), 0},
81            {cat, 0, bigType.parameterList(), -1},
82            {cat, 3, bigType.parameterList(), 0},
83            {cat, 0, bigType.parameterList(), 6},
84            {cat, 0, bigType.parameterList(), 2}
85        };
86    }
87
88    @Test(dataProvider = "dropArgumentsToMatchIAEData")
89    @ExpectedExceptions(IllegalArgumentException.class)
90    public void dropArgumentsToMatchIAE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) {
91        MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip);
92    }
93
94    @Test
95    @ExpectedExceptions(IllegalArgumentException.class)
96    public void dropArgumentsToMatchTestWithVoid() throws Throwable {
97        MethodHandle cat = lookup().findVirtual(String.class, "concat",
98                                   MethodType.methodType(String.class, String.class));
99        MethodType bigTypewithVoid = cat.type().insertParameterTypes(0, void.class, String.class, int.class);
100        MethodHandle handle2 = MethodHandles.dropArgumentsToMatch(cat, 0, bigTypewithVoid.parameterList(), 1);
101    }
102
103    public static class MethodSet {
104
105        static void mVoid() {
106
107        }
108
109        static void mVoid(int t) {
110
111        }
112    }
113
114    @Test
115    public void dropArgumentsToMatchPosSkipRange() throws Throwable {
116        // newTypes.size() == 1, pos == 1   &&   target.paramSize() == 0, skip == 0
117        MethodHandle mh1 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid",
118                                                             MethodType.methodType(void.class));
119        MethodHandle handle1 = dropArgumentsToMatch(mh1, 0, Collections.singletonList(int.class), 1);
120        assertEquals(1, handle1.type().parameterList().size());
121
122        // newTypes.size() == 1, pos == 0   &&   target.paramSize() == 1, skip == 1
123        MethodHandle mh2 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid",
124                                                             MethodType.methodType(void.class, int.class));
125        MethodHandle handle2 = dropArgumentsToMatch(mh2, 1, Collections.singletonList(int.class), 0);
126        assertEquals(2, handle2.type().parameterList().size());
127    }
128}
129