Path: blob/master/test/jdk/java/lang/invoke/InvokeMethodHandleWithBadArgument.java
41149 views
/*1* Copyright (c) 2017, 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/*26* @test27* @bug 815724628* @summary Tests invocation of MethodHandle with invalid leading argument29* @run testng/othervm test.java.lang.invoke.InvokeMethodHandleWithBadArgument30*/3132package test.java.lang.invoke;3334import java.lang.invoke.MethodHandle;35import java.lang.invoke.MethodHandles;36import java.lang.invoke.MethodHandles.Lookup;37import java.lang.invoke.MethodType;38import java.lang.invoke.VarHandle;3940import static java.lang.invoke.MethodType.methodType;4142import static org.testng.AssertJUnit.*;4344import org.testng.annotations.*;4546/**47* Tests invocation of MethodHandle with invalid leading argument such as48* MethodHandle, VarHandle, and array object49*/50public class InvokeMethodHandleWithBadArgument {51// ---- null array reference ----5253@Test(expectedExceptions = {NullPointerException.class})54public static void testAsSpreaderPosInvokeWithNull() throws Throwable {55MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3);56spreader.invoke("A", null, "B");57}5859@Test(expectedExceptions = {NullPointerException.class})60public static void testAsSpreaderInvokeWithNull() throws Throwable {61MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2);62assert ((boolean) spreader.invokeExact(new String[]{"me", "me"}));63boolean eq = (boolean) spreader.invokeExact((String[]) null);64}6566// ---- incorrect array element count ----67@Test(expectedExceptions = {IllegalArgumentException.class})68public static void testAsSpreaderPosInvokeWithBadElementCount() throws Throwable {69MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3);70spreader.invoke("A", new int[]{1, 2}, "B");71}7273@Test(expectedExceptions = {IllegalArgumentException.class})74public static void testAsSpreaderInvokeWithBadElementCount() throws Throwable {75MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2);76assert (!(boolean) spreader.invokeExact(new String[]{"me", "thee"}));77boolean eq = (boolean) spreader.invokeExact(new String[0]);78}7980// ---- spread no argument ----81@Test82public static void testAsSpreaderPosInvokeWithZeroLength() throws Throwable {83MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 0);84assert("A123B".equals(spreader.invoke("A", (int[])null, 1, 2, 3, "B")));85}8687@Test88public static void testAsSpreaderInvokeWithZeroLength() throws Throwable {89MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 0);90assert ((boolean) spreader.invokeExact("me", (Object)"me", new String[0]));91boolean eq = (boolean) spreader.invokeExact("me", (Object)"me", (String[]) null);92}9394// ---- invokers with null method/var handle argument ----95@Test(expectedExceptions = {NullPointerException.class})96public static void testInvokerWithNull() throws Throwable {97MethodType type = methodType(int.class, int.class, int.class);98MethodHandle invoker = MethodHandles.invoker(type);99assert((int) invoker.invoke(MH_add, 1, 2) == 3);100int sum = (int)invoker.invoke((MethodHandle)null, 1, 2);101}102103@Test(expectedExceptions = {NullPointerException.class})104public static void testExactInvokerWithNull() throws Throwable {105MethodType type = methodType(int.class, int.class, int.class);106MethodHandle invoker = MethodHandles.exactInvoker(type);107assert((int) invoker.invoke(MH_add, 1, 2) == 3);108int sum = (int)invoker.invokeExact((MethodHandle)null, 1, 2);109}110111@Test(expectedExceptions = {NullPointerException.class})112public static void testSpreadInvokerWithNull() throws Throwable {113MethodType type = methodType(boolean.class, String.class, String.class);114MethodHandle invoker = MethodHandles.spreadInvoker(type, 0);115assert ((boolean) invoker.invoke(MH_String_equals, new String[]{"me", "me"}));116boolean eq = (boolean) invoker.invoke((MethodHandle)null, new String[]{"me", "me"});117}118119@Test(expectedExceptions = {NullPointerException.class})120public static void testVarHandleInvokerWithNull() throws Throwable {121VarHandle.AccessMode am = VarHandle.AccessMode.GET;122MethodHandle invoker = MethodHandles.varHandleInvoker(am, VH_array.accessModeType(am));123assert ((int) invoker.invoke(VH_array, array, 3) == 3);124int value = (int)invoker.invoke((VarHandle)null, array, 3);125}126127@Test(expectedExceptions = {NullPointerException.class})128public static void testVarHandleExactInvokerWithNull() throws Throwable {129VarHandle.AccessMode am = VarHandle.AccessMode.GET;130MethodHandle invoker = MethodHandles.varHandleExactInvoker(am, VH_array.accessModeType(am));131assert ((int) invoker.invoke(VH_array, array, 3) == 3);132int value = (int)invoker.invokeExact((VarHandle)null, array, 3);133}134135static final Lookup LOOKUP = MethodHandles.lookup();136static final MethodHandle MH_add;137static final MethodHandle MH_spread;138static final MethodHandle MH_String_equals;139static final VarHandle VH_array;140141static final int[] array = new int[] { 0, 1, 2, 3, 4, 5};142static {143try {144Class<?> arrayClass = Class.forName("[I");145VH_array = MethodHandles.arrayElementVarHandle(arrayClass);146MH_add = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "add",147methodType(int.class, int.class, int.class));148MH_spread = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "spread",149methodType(String.class, String.class, int.class, int.class, int.class, String.class));150MH_String_equals = LOOKUP.findVirtual(String.class, "equals", methodType(boolean.class, Object.class));151} catch (Exception e) {152throw new ExceptionInInitializerError(e);153}154}155156static String spread(String s1, int i1, int i2, int i3, String s2) {157return s1 + i1 + i2 + i3 + s2;158}159160static int add(int x, int y) {161return x+y;162}163}164165166