1 /* 2 * Copyright (c) 2014, 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 package java.lang.invoke; 25 26 import sun.invoke.util.Wrapper; 27 28 /* @test 29 * @summary unit tests for MethodHandles.explicitCastArguments() 30 * 31 * @run main/bootclasspath java.lang.invoke.ExplicitCastArgumentsTest 32 */ 33 public class ExplicitCastArgumentsTest { 34 private static final boolean VERBOSE = Boolean.getBoolean("verbose"); 35 private static final Class<?> THIS_CLASS = ExplicitCastArgumentsTest.class; 36 37 public static void main(String[] args) throws Throwable { 38 testVarargsCollector(); 39 testRef2Prim(); 40 System.out.println("TEST PASSED"); 41 } 42 43 public static String[] f(String... args) { return args; } 44 45 public static void testVarargsCollector() throws Throwable { 46 MethodType mt = MethodType.methodType(String[].class, String[].class); 47 MethodHandle mh = MethodHandles.publicLookup().findStatic(THIS_CLASS, "f", mt); 48 mh = MethodHandles.explicitCastArguments(mh, MethodType.methodType(Object.class, Object.class)); 49 mh.invokeWithArguments((Object)(new String[] {"str1", "str2"})); 50 } 51 52 public static void testRef2Prim() throws Throwable { 53 for (Wrapper from : Wrapper.values()) { 54 for (Wrapper to : Wrapper.values()) { 55 if (from == Wrapper.VOID || to == Wrapper.VOID) continue; 56 testRef2Prim(from, to); 57 } 58 } 59 } 60 61 public static void testRef2Prim(Wrapper from, Wrapper to) throws Throwable { 62 // MHs.eCA javadoc: 63 // If T0 is a reference and T1 a primitive, and if the reference is null at runtime, a zero value is introduced. 64 test(from.wrapperType(), to.primitiveType(), null, false); 65 } 66 67 public static void test(Class<?> from, Class<?> to, Object param, boolean failureExpected) throws Throwable { 68 if (VERBOSE) System.out.printf("%-10s => %-10s: %5s: ", from.getSimpleName(), to.getSimpleName(), param); 69 70 MethodHandle original = MethodHandles.identity(from); 71 MethodType newType = original.type().changeReturnType(to); 72 73 try { 74 MethodHandle target = MethodHandles.explicitCastArguments(original, newType); 75 Object result = target.invokeWithArguments(param); 76 77 if (VERBOSE) { 78 String resultStr; 79 if (result != null) { 80 resultStr = String.format("%10s (%10s)", "'"+result+"'", result.getClass().getSimpleName()); 81 } else { 82 resultStr = String.format("%10s", result); 83 } 84 System.out.println(resultStr); 85 } 86 87 if (failureExpected) { 88 String msg = String.format("No exception thrown: %s => %s; parameter: %s", from, to, param); 89 throw new AssertionError(msg); 90 } 91 } catch (AssertionError e) { 92 throw e; // report test failure 93 } catch (Throwable e) { 94 if (VERBOSE) System.out.printf("%s: %s\n", e.getClass(), e.getMessage()); 95 if (!failureExpected) { 96 String msg = String.format("Unexpected exception was thrown: %s => %s; parameter: %s", from, to, param); 97 throw new AssertionError(msg, e); 98 } 99 } 100 } 101 }