test/jdk/java/lang/invoke/InvokeMethodHandleWithBadArgument.java
changeset 47856 76519338df34
equal deleted inserted replaced
47855:72b6d674bde2 47856:76519338df34
       
     1 /*
       
     2  * Copyright (c) 2017, 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 /*
       
    27  * @test
       
    28  * @bug 8157246
       
    29  * @summary Tests invocation of MethodHandle with invalid leading argument
       
    30  * @run testng/othervm test.java.lang.invoke.InvokeMethodHandleWithBadArgument
       
    31  */
       
    32 
       
    33 package test.java.lang.invoke;
       
    34 
       
    35 import java.lang.invoke.MethodHandle;
       
    36 import java.lang.invoke.MethodHandles;
       
    37 import java.lang.invoke.MethodHandles.Lookup;
       
    38 import java.lang.invoke.MethodType;
       
    39 import java.lang.invoke.VarHandle;
       
    40 
       
    41 import static java.lang.invoke.MethodType.methodType;
       
    42 
       
    43 import static org.testng.AssertJUnit.*;
       
    44 
       
    45 import org.testng.annotations.*;
       
    46 
       
    47 /**
       
    48  * Tests invocation of MethodHandle with invalid leading argument such as
       
    49  * MethodHandle, VarHandle, and array object
       
    50  */
       
    51 public class InvokeMethodHandleWithBadArgument {
       
    52     // ---- null array reference ----
       
    53 
       
    54     @Test(expectedExceptions = {NullPointerException.class})
       
    55     public static void testAsSpreaderPosInvokeWithNull() throws Throwable {
       
    56         MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3);
       
    57         spreader.invoke("A", null, "B");
       
    58     }
       
    59 
       
    60     @Test(expectedExceptions = {NullPointerException.class})
       
    61     public static void testAsSpreaderInvokeWithNull() throws Throwable {
       
    62         MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2);
       
    63         assert ((boolean) spreader.invokeExact(new String[]{"me", "me"}));
       
    64         boolean eq = (boolean) spreader.invokeExact((String[]) null);
       
    65     }
       
    66 
       
    67     // ---- incorrect array element count ----
       
    68     @Test(expectedExceptions = {IllegalArgumentException.class})
       
    69     public static void testAsSpreaderPosInvokeWithBadElementCount() throws Throwable {
       
    70         MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3);
       
    71         spreader.invoke("A", new int[]{1, 2}, "B");
       
    72     }
       
    73 
       
    74     @Test(expectedExceptions = {IllegalArgumentException.class})
       
    75     public static void testAsSpreaderInvokeWithBadElementCount() throws Throwable {
       
    76         MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2);
       
    77         assert (!(boolean) spreader.invokeExact(new String[]{"me", "thee"}));
       
    78         boolean eq = (boolean) spreader.invokeExact(new String[0]);
       
    79     }
       
    80 
       
    81     // ---- spread no argument ----
       
    82     @Test
       
    83     public static void testAsSpreaderPosInvokeWithZeroLength() throws Throwable {
       
    84         MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 0);
       
    85         assert("A123B".equals(spreader.invoke("A", (int[])null, 1, 2, 3, "B")));
       
    86     }
       
    87 
       
    88     @Test
       
    89     public static void testAsSpreaderInvokeWithZeroLength() throws Throwable {
       
    90         MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 0);
       
    91         assert ((boolean) spreader.invokeExact("me", (Object)"me", new String[0]));
       
    92         boolean eq = (boolean) spreader.invokeExact("me", (Object)"me", (String[]) null);
       
    93     }
       
    94 
       
    95     // ---- invokers with null method/var handle argument ----
       
    96     @Test(expectedExceptions = {NullPointerException.class})
       
    97     public static void testInvokerWithNull() throws Throwable {
       
    98         MethodType type = methodType(int.class, int.class, int.class);
       
    99         MethodHandle invoker = MethodHandles.invoker(type);
       
   100         assert((int) invoker.invoke(MH_add, 1, 2) == 3);
       
   101         int sum = (int)invoker.invoke((MethodHandle)null, 1, 2);
       
   102     }
       
   103 
       
   104     @Test(expectedExceptions = {NullPointerException.class})
       
   105     public static void testExactInvokerWithNull() throws Throwable {
       
   106         MethodType type = methodType(int.class, int.class, int.class);
       
   107         MethodHandle invoker = MethodHandles.exactInvoker(type);
       
   108         assert((int) invoker.invoke(MH_add, 1, 2) == 3);
       
   109         int sum = (int)invoker.invokeExact((MethodHandle)null, 1, 2);
       
   110     }
       
   111 
       
   112     @Test(expectedExceptions = {NullPointerException.class})
       
   113     public static void testSpreadInvokerWithNull() throws Throwable {
       
   114         MethodType type = methodType(boolean.class, String.class, String.class);
       
   115         MethodHandle invoker = MethodHandles.spreadInvoker(type, 0);
       
   116         assert ((boolean) invoker.invoke(MH_String_equals, new String[]{"me", "me"}));
       
   117         boolean eq = (boolean) invoker.invoke((MethodHandle)null, new String[]{"me", "me"});
       
   118     }
       
   119 
       
   120     @Test(expectedExceptions = {NullPointerException.class})
       
   121     public static void testVarHandleInvokerWithNull() throws Throwable {
       
   122         VarHandle.AccessMode am = VarHandle.AccessMode.GET;
       
   123         MethodHandle invoker = MethodHandles.varHandleInvoker(am, VH_array.accessModeType(am));
       
   124         assert ((int) invoker.invoke(VH_array, array, 3) == 3);
       
   125         int value = (int)invoker.invoke((VarHandle)null, array, 3);
       
   126     }
       
   127 
       
   128     @Test(expectedExceptions = {NullPointerException.class})
       
   129     public static void testVarHandleExactInvokerWithNull() throws Throwable {
       
   130         VarHandle.AccessMode am = VarHandle.AccessMode.GET;
       
   131         MethodHandle invoker = MethodHandles.varHandleExactInvoker(am, VH_array.accessModeType(am));
       
   132         assert ((int) invoker.invoke(VH_array, array, 3) == 3);
       
   133         int value = (int)invoker.invokeExact((VarHandle)null, array, 3);
       
   134     }
       
   135 
       
   136     static final Lookup LOOKUP = MethodHandles.lookup();
       
   137     static final MethodHandle MH_add;
       
   138     static final MethodHandle MH_spread;
       
   139     static final MethodHandle MH_String_equals;
       
   140     static final VarHandle VH_array;
       
   141 
       
   142     static final int[] array = new int[] { 0, 1, 2, 3, 4, 5};
       
   143     static {
       
   144         try {
       
   145             Class<?> arrayClass = Class.forName("[I");
       
   146             VH_array = MethodHandles.arrayElementVarHandle(arrayClass);
       
   147             MH_add = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "add",
       
   148                 methodType(int.class, int.class, int.class));
       
   149             MH_spread = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "spread",
       
   150                 methodType(String.class, String.class, int.class, int.class, int.class, String.class));
       
   151             MH_String_equals = LOOKUP.findVirtual(String.class, "equals", methodType(boolean.class, Object.class));
       
   152         } catch (Exception e) {
       
   153             throw new ExceptionInInitializerError(e);
       
   154         }
       
   155     }
       
   156 
       
   157     static String spread(String s1, int i1, int i2, int i3, String s2) {
       
   158         return s1 + i1 + i2 + i3 + s2;
       
   159     }
       
   160 
       
   161     static int add(int x, int y) {
       
   162         return x+y;
       
   163     }
       
   164 }