Path: blob/master/test/jdk/java/lang/invoke/MethodHandlesArityLimitsTest.java
41149 views
/*1* Copyright (c) 2018, 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* @summary unit tests for arity limits of methods in java.lang.invoke.MethodHandles25* @run junit/othervm test.java.lang.invoke.MethodHandlesArityLimitsTest26**/2728package test.java.lang.invoke;2930import org.junit.*;3132import java.lang.invoke.MethodHandles;33import java.lang.invoke.MethodHandle;34import java.lang.invoke.WrongMethodTypeException;35import java.lang.invoke.MethodType;36import java.lang.invoke.VarHandle;37import java.util.List;3839import java.util.stream.IntStream;4041import static org.junit.Assert.*;4243public class MethodHandlesArityLimitsTest {4445private static MethodType mt254 = null;46private static MethodType mt255 = null;4748static {49Class<?>[] classes254 = IntStream.range(0, 254)50.mapToObj(i -> int.class)51.toArray(Class[]::new);52mt254 = MethodType.methodType(void.class, classes254);53mt255 = mt254.appendParameterTypes(int.class);54}5556@Test(expected = IllegalArgumentException.class)57public void testDropArgumentsToMatch() {58MethodHandles.dropArgumentsToMatch(MethodHandles.empty(mt254), 0, mt255.parameterList(), 0);59}6061@Test(expected = IllegalArgumentException.class)62public void testEmpty() {63MethodHandles.empty(mt255);64}6566@Test(expected = IllegalArgumentException.class)67public void testExplicitCastArguments() {68MethodHandles.explicitCastArguments(69MethodHandles.empty(mt254),70mt254.dropParameterTypes(0, 1).insertParameterTypes(0, long.class) );71}7273@Test(expected = IllegalArgumentException.class)74public void testPermuteArguments() {75MethodHandles.permuteArguments(76MethodHandles.empty(MethodType.methodType(void.class)),77mt255);78}7980@Test(expected = IllegalArgumentException.class)81public void testVarHandleInvoker() {82MethodHandles.varHandleInvoker(VarHandle.AccessMode.GET, mt254);83}8485@Test(expected = IllegalArgumentException.class)86public void testVarHandleExactInvoker() {87MethodHandles.varHandleExactInvoker(VarHandle.AccessMode.GET, mt254);88}8990@Test(expected = IllegalArgumentException.class)91public void testMHExactInvoker() {92MethodHandles.exactInvoker(mt255);93}9495@Test(expected = IllegalArgumentException.class)96public void testMHInvoker() {97MethodHandles.invoker(mt255);98}99100@Test(expected = IllegalArgumentException.class)101public void testMHSpreadInvoker() {102MethodHandles.spreadInvoker(mt255, 255);103}104105@Test(expected = WrongMethodTypeException.class)106public void testAsType() throws ReflectiveOperationException {107MethodHandle asList = MethodHandles.lookup().findStatic(108java.util.Arrays.class,109"asList",110MethodType.methodType(List.class, Object[].class));111try {112asList.asType(MethodType.genericMethodType(254));//does not throw IAE or WMTE113}114catch(WrongMethodTypeException wmte) {115throw new AssertionError("Unexpected WrongMethodTypeException thrown", wmte);116}117asList.asType(MethodType.genericMethodType(255));//throws WMTE118}119}120121122