1 /* 2 * Copyright (c) 2018, 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 /* @test 25 * @summary unit tests for java.lang.invoke.MethodHandles 26 * @library /test/lib /java/lang/invoke/common 27 * @compile MethodHandlesTest.java MethodHandlesInsertArgumentsTest.java remote/RemoteExample.java 28 * @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions 29 * -XX:-VerifyDependencies 30 * -esa 31 * test.java.lang.invoke.MethodHandlesInsertArgumentsTest 32 */ 33 34 package test.java.lang.invoke; 35 36 import org.junit.*; 37 import test.java.lang.invoke.lib.CodeCacheOverflowProcessor; 38 39 import java.lang.invoke.MethodHandle; 40 import java.lang.invoke.MethodHandles; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 45 import static java.lang.invoke.MethodType.methodType; 46 47 import static org.junit.Assert.*; 48 49 public class MethodHandlesInsertArgumentsTest extends MethodHandlesTest { 50 51 @Test // SLOW 52 public void testInsertArguments() throws Throwable { 53 CodeCacheOverflowProcessor.runMHTest(this::testInsertArguments0); 54 } 55 56 public void testInsertArguments0() throws Throwable { 57 if (CAN_SKIP_WORKING) return; 58 startTest("insertArguments"); 59 for (int nargs = 0; nargs < 50; nargs++) { 60 if (CAN_TEST_LIGHTLY && nargs > 11) break; 61 for (int ins = 0; ins <= nargs; ins++) { 62 if (nargs > 10 && ins > 4 && ins < nargs-4 && ins % 10 != 3) 63 continue; 64 for (int pos = 0; pos <= nargs; pos++) { 65 if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3) 66 continue; 67 if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue; 68 testInsertArguments(nargs, pos, ins); 69 } 70 } 71 } 72 } 73 74 void testInsertArguments(int nargs, int pos, int ins) throws Throwable { 75 countTest(); 76 MethodHandle target = varargsArray(nargs + ins); 77 Object[] args = randomArgs(target.type().parameterArray()); 78 List<Object> resList = Arrays.asList(args); 79 List<Object> argsToPass = new ArrayList<>(resList); 80 List<Object> argsToInsert = argsToPass.subList(pos, pos + ins); 81 if (verbosity >= 3) 82 System.out.println("insert: "+argsToInsert+" @"+pos+" into "+target); 83 @SuppressWarnings("cast") // cast to spread Object... is helpful 84 MethodHandle target2 = MethodHandles.insertArguments(target, pos, 85 (Object[]/*...*/) argsToInsert.toArray()); 86 argsToInsert.clear(); // remove from argsToInsert 87 Object res2 = target2.invokeWithArguments(argsToPass); 88 Object res2List = Arrays.asList((Object[])res2); 89 if (verbosity >= 3) 90 System.out.println("result: "+res2List); 91 assertEquals(resList, res2List); 92 } 93 94 private static MethodHandle methodHandle = null; 95 static { 96 try { 97 methodHandle = MethodHandles.lookup().findVirtual( 98 MethodHandlesInsertArgumentsTest.class, 99 "testMethod", 100 methodType(void.class, String.class, String.class)); 101 } catch(ReflectiveOperationException ex) { 102 throw new InternalError(ex); 103 } 104 } 105 106 @Test(expected = IllegalArgumentException.class) 107 public void testInsertArgumentsInvalidPos() { 108 countTest(); 109 MethodHandles.insertArguments(methodHandle, -1, "First", "Second"); 110 } 111 112 @Test(expected = IllegalArgumentException.class) 113 public void testInsertArgumentsTooManyParams() { 114 countTest(); 115 MethodHandles.insertArguments(methodHandle, 1, "First", "Second", "Third"); 116 } 117 118 @Test(expected = ClassCastException.class) 119 public void testInsertArgumentsPosZero() { 120 countTest(); 121 MethodHandles.insertArguments(methodHandle, 0, "First"); 122 } 123 124 @Test(expected = ClassCastException.class) 125 public void testInsertArgumentsIncorrectParam() { 126 countTest(); 127 MethodHandles.insertArguments(methodHandle, 1, "First", new Object()); 128 } 129 130 void testMethod(String a, String b) { 131 } 132 }