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 */ 31 package test.java.lang.invoke; 32 33 import java.lang.invoke.MethodHandle; 34 import java.lang.invoke.MethodHandles; 35 import java.lang.invoke.MethodType; 36 import java.util.Collections; 37 import java.util.List; 38 import static java.lang.invoke.MethodHandles.*; 39 import static java.lang.invoke.MethodType.*; 40 import static org.testng.AssertJUnit.*; 41 import org.testng.annotations.*; 42 43 public class DropArgumentsTest { 44 45 @Test testDropArgumentsToMatch()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") 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", expectedExceptions = { NullPointerException.class }) dropArgumentsToMatchNPE(MethodHandle target, int pos, List<Class<?>> valueType, int skip)69 public void dropArgumentsToMatchNPE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) { 70 MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip); 71 } 72 73 @DataProvider(name = "dropArgumentsToMatchIAEData") dropArgumentsToMatchIAEData()74 private Object[][] dropArgumentsToMatchIAEData() 75 throws NoSuchMethodException, IllegalAccessException { 76 MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class)); 77 MethodType bigType = cat.type().insertParameterTypes(0, String.class, String.class, int.class); 78 return new Object[][] { 79 {cat, -1, bigType.parameterList(), 0}, 80 {cat, 0, bigType.parameterList(), -1}, 81 {cat, 3, bigType.parameterList(), 0}, 82 {cat, 0, bigType.parameterList(), 6}, 83 {cat, 0, bigType.parameterList(), 2} 84 }; 85 } 86 87 @Test(dataProvider = "dropArgumentsToMatchIAEData", expectedExceptions = { IllegalArgumentException.class }) dropArgumentsToMatchIAE(MethodHandle target, int pos, List<Class<?>> valueType, int skip)88 public void dropArgumentsToMatchIAE(MethodHandle target, int pos, List<Class<?>> valueType, int skip) { 89 MethodHandles.dropArgumentsToMatch(target, pos, valueType , skip); 90 } 91 92 @Test(expectedExceptions = { IllegalArgumentException.class }) dropArgumentsToMatchTestWithVoid()93 public void dropArgumentsToMatchTestWithVoid() throws Throwable { 94 MethodHandle cat = lookup().findVirtual(String.class, "concat", 95 MethodType.methodType(String.class, String.class)); 96 MethodType bigTypewithVoid = cat.type().insertParameterTypes(0, void.class, String.class, int.class); 97 MethodHandle handle2 = MethodHandles.dropArgumentsToMatch(cat, 0, bigTypewithVoid.parameterList(), 1); 98 } 99 100 public static class MethodSet { 101 mVoid()102 static void mVoid() { 103 104 } 105 mVoid(int t)106 static void mVoid(int t) { 107 108 } 109 } 110 111 @Test dropArgumentsToMatchPosSkipRange()112 public void dropArgumentsToMatchPosSkipRange() throws Throwable { 113 // newTypes.size() == 1, pos == 1 && target.paramSize() == 0, skip == 0 114 MethodHandle mh1 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid", 115 MethodType.methodType(void.class)); 116 MethodHandle handle1 = dropArgumentsToMatch(mh1, 0, Collections.singletonList(int.class), 1); 117 assertEquals(1, handle1.type().parameterList().size()); 118 119 // newTypes.size() == 1, pos == 0 && target.paramSize() == 1, skip == 1 120 MethodHandle mh2 = MethodHandles.lookup().findStatic(MethodSet.class, "mVoid", 121 MethodType.methodType(void.class, int.class)); 122 MethodHandle handle2 = dropArgumentsToMatch(mh2, 1, Collections.singletonList(int.class), 0); 123 assertEquals(2, handle2.type().parameterList().size()); 124 } 125 } 126