Path: blob/master/test/jdk/java/lang/invoke/MethodHandlesCollectArgsTest.java
41149 views
/*1* Copyright (c) 2021, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 825992225* @run testng/othervm MethodHandlesCollectArgsTest26*/2728import org.testng.annotations.Test;29import org.testng.annotations.DataProvider;3031import java.lang.invoke.MethodHandle;32import java.lang.invoke.MethodHandles;33import java.lang.invoke.MethodType;34import static java.lang.invoke.MethodType.methodType;3536import static org.testng.Assert.*;3738public class MethodHandlesCollectArgsTest {3940private static final MethodHandle TARGET_II_I = MethodHandles.empty(methodType(int.class, int.class, int.class));41private static final MethodHandle TARGET__V = MethodHandles.empty(methodType(void.class));42private static final MethodHandle FILTER_INT = MethodHandles.empty(methodType(int.class, String.class));43private static final MethodHandle FILTER_VOID = MethodHandles.empty(methodType(void.class, String.class));4445@DataProvider(name = "illegalPos")46public static Object[][] illegalPos() {47return new Object[][] {48{TARGET_II_I, 2, FILTER_INT},49{TARGET_II_I, 3, FILTER_VOID},50{TARGET_II_I, -1, FILTER_INT},51{TARGET_II_I, -1, FILTER_VOID},52{TARGET__V, 0, FILTER_INT},53{TARGET__V, 1, FILTER_VOID},54{TARGET__V, -1, FILTER_VOID},55{TARGET__V, -1, FILTER_VOID}56};57}5859@DataProvider(name = "validPos")60public static Object[][] validPos() {61return new Object[][] {62{TARGET_II_I, 0, FILTER_INT, methodType(int.class, String.class, int.class)},63{TARGET_II_I, 1, FILTER_INT, methodType(int.class, int.class, String.class)},64{TARGET_II_I, 0, FILTER_VOID, methodType(int.class, String.class, int.class, int.class)},65{TARGET_II_I, 1, FILTER_VOID, methodType(int.class, int.class, String.class, int.class)},66{TARGET_II_I, 2, FILTER_VOID, methodType(int.class, int.class, int.class, String.class)},67{TARGET__V, 0, FILTER_VOID, methodType(void.class, String.class)}68};69}7071@Test(dataProvider="illegalPos", expectedExceptions = {IllegalArgumentException.class})72public void illegalPosition(MethodHandle target, int position, MethodHandle filter) {73MethodHandles.collectArguments(target, position, filter);74}7576@Test(dataProvider="validPos")77public void legalPosition(MethodHandle target, int position, MethodHandle filter, MethodType expectedType) {78MethodHandle result = MethodHandles.collectArguments(target, position, filter);79assertEquals(result.type(), expectedType);80}81}828384