Path: blob/master/test/jdk/java/lang/invoke/InvokeWithArgumentsTest.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.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 basic tests for MethodHandle.invokeWithArguments25* @run testng test.java.lang.invoke.InvokeWithArgumentsTest26*/2728package test.java.lang.invoke;2930import org.testng.Assert;31import org.testng.annotations.Test;3233import java.lang.invoke.MethodHandle;34import java.lang.invoke.MethodHandles;35import java.lang.invoke.WrongMethodTypeException;3637import static java.lang.invoke.MethodType.methodType;3839public class InvokeWithArgumentsTest {40static final MethodHandles.Lookup L = MethodHandles.lookup();4142static Object[] arity(Object o1, Object o2, Object... a) {43return a;44}4546@Test47public void testArity() throws Throwable {48MethodHandle mh = L.findStatic(L.lookupClass(), "arity",49methodType(Object[].class, Object.class, Object.class, Object[].class));5051try {52mh.invokeWithArguments("");53Assert.fail("WrongMethodTypeException expected");54} catch (WrongMethodTypeException e) {}55}5657static Object[] passThrough(String... a) {58return a;59}6061static Object[] pack(Object o, Object... a) {62return a;63}6465@Test66public void testArrayNoPassThrough() throws Throwable {67String[] actual = {"A", "B"};6869MethodHandle mh = L.findStatic(L.lookupClass(), "passThrough",70methodType(Object[].class, String[].class));7172// Note: the actual array is not preserved, the elements will be73// unpacked and then packed into a new array before invoking the method74String[] expected = (String[]) mh.invokeWithArguments(actual);7576Assert.assertTrue(actual != expected, "Array should not pass through");77Assert.assertEquals(actual, expected, "Array contents should be equal");78}7980@Test81public void testArrayPack() throws Throwable {82String[] actual = new String[]{"A", "B"};8384MethodHandle mh = L.findStatic(L.lookupClass(), "pack",85methodType(Object[].class, Object.class, Object[].class));8687// Note: since String[] can be cast to Object, the actual String[] array88// will cast to Object become the single element of a new Object[] array89Object[] expected = (Object[]) mh.invokeWithArguments("", actual);9091Assert.assertEquals(1, expected.length, "Array should contain just one element");92Assert.assertTrue(actual == expected[0], "Array should pass through");93}9495static void intArray(int... a) {96}9798@Test99public void testPrimitiveArrayWithNull() throws Throwable {100MethodHandle mh = L.findStatic(L.lookupClass(), "intArray",101methodType(void.class, int[].class));102try {103mh.invokeWithArguments(null, null);104Assert.fail("NullPointerException expected");105} catch (NullPointerException e) {}106}107108@Test109public void testPrimitiveArrayWithRef() throws Throwable {110MethodHandle mh = L.findStatic(L.lookupClass(), "intArray",111methodType(void.class, int[].class));112try {113mh.invokeWithArguments("A", "B");114Assert.fail("ClassCastException expected");115} catch (ClassCastException e) {}116}117118119static void numberArray(Number... a) {120}121122@Test123public void testRefArrayWithCast() throws Throwable {124MethodHandle mh = L.findStatic(L.lookupClass(), "numberArray",125methodType(void.class, Number[].class));126// All numbers, should not throw127mh.invokeWithArguments(1, 1.0, 1.0F, 1L);128129try {130mh.invokeWithArguments("A");131Assert.fail("ClassCastException expected");132} catch (ClassCastException e) {}133}134}135136137