Path: blob/master/test/jdk/java/lang/invoke/DropArgumentsTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 815816927* @summary unit tests for java.lang.invoke.MethodHandles28* @run testng test.java.lang.invoke.DropArgumentsTest29*/30package test.java.lang.invoke;3132import java.lang.invoke.MethodHandle;33import java.lang.invoke.MethodHandles;34import java.lang.invoke.MethodType;35import java.util.Collections;36import java.util.List;37import static java.lang.invoke.MethodHandles.*;38import static java.lang.invoke.MethodType.*;39import static org.testng.AssertJUnit.*;40import org.testng.annotations.*;4142public class DropArgumentsTest {4344@Test45public void testDropArgumentsToMatch() throws Throwable {46MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));47MethodType bigType = cat.type().insertParameterTypes(0, String.class, String.class, int.class);48MethodHandle d0 = MethodHandles.dropArgumentsToMatch(cat, 0, bigType.parameterList(), 3);49assertEquals("xy",(String)d0.invokeExact("m", "n", 1, "x", "y"));50MethodHandle d1 = MethodHandles.dropArgumentsToMatch(cat, 0, bigType.parameterList(), 0);51assertEquals("mn",(String)d1.invokeExact("m", "n", 1, "x", "y"));52MethodHandle d2 = MethodHandles.dropArgumentsToMatch(cat, 1, bigType.parameterList(), 4);53assertEquals("xy",(String)d2.invokeExact("x", "b", "c", 1, "a", "y"));5455}5657@DataProvider(name = "dropArgumentsToMatchNPEData")58private Object[][] dropArgumentsToMatchNPEData()59throws NoSuchMethodException, IllegalAccessException {60MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));61return new Object[][] {62{ (MethodHandle) null, 0, cat.type().parameterList(), 0 },63{ cat, 0, null, 0 }64};65}6667@Test(dataProvider = "dropArgumentsToMatchNPEData", expectedExceptions = { NullPointerException.class })68public void dropArgumentsToMatchNPE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) {69MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip);70}7172@DataProvider(name = "dropArgumentsToMatchIAEData")73private Object[][] dropArgumentsToMatchIAEData()74throws NoSuchMethodException, IllegalAccessException {75MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));76MethodType bigType = cat.type().insertParameterTypes(0, String.class, String.class, int.class);77return new Object[][] {78{cat, -1, bigType.parameterList(), 0},79{cat, 0, bigType.parameterList(), -1},80{cat, 3, bigType.parameterList(), 0},81{cat, 0, bigType.parameterList(), 6},82{cat, 0, bigType.parameterList(), 2}83};84}8586@Test(dataProvider = "dropArgumentsToMatchIAEData", expectedExceptions = { IllegalArgumentException.class })87public void dropArgumentsToMatchIAE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) {88MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip);89}9091@Test(expectedExceptions = { IllegalArgumentException.class })92public void dropArgumentsToMatchTestWithVoid() throws Throwable {93MethodHandle cat = lookup().findVirtual(String.class, "concat",94MethodType.methodType(String.class, String.class));95MethodType bigTypewithVoid = cat.type().insertParameterTypes(0, void.class, String.class, int.class);96MethodHandle handle2 = MethodHandles.dropArgumentsToMatch(cat, 0, bigTypewithVoid.parameterList(), 1);97}9899public static class MethodSet {100101static void mVoid() {102103}104105static void mVoid(int t) {106107}108}109110@Test111public void dropArgumentsToMatchPosSkipRange() throws Throwable {112// newTypes.size() == 1, pos == 1 && target.paramSize() == 0, skip == 0113MethodHandle mh1 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid",114MethodType.methodType(void.class));115MethodHandle handle1 = dropArgumentsToMatch(mh1, 0, Collections.singletonList(int.class), 1);116assertEquals(1, handle1.type().parameterList().size());117118// newTypes.size() == 1, pos == 0 && target.paramSize() == 1, skip == 1119MethodHandle mh2 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid",120MethodType.methodType(void.class, int.class));121MethodHandle handle2 = dropArgumentsToMatch(mh2, 1, Collections.singletonList(int.class), 0);122assertEquals(2, handle2.type().parameterList().size());123}124}125126127