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 
  36     public static void main(String[] args) throws Throwable {
  37         for (Wrapper from : Wrapper.values()) {
  38             for (Wrapper to : Wrapper.values()) {
  39                 if (from == Wrapper.VOID || to == Wrapper.VOID) continue;
  40                 testRef2Prim (from, to);
  41             }
  42         }
  43         System.out.println("TEST PASSED");
  44     }
  45 
  46     public static void testRef2Prim(Wrapper from, Wrapper to) throws Throwable {
  47         // MHs.eCA javadoc:
  48         //    If T0 is a reference and T1 a primitive, and if the reference is null at runtime, a zero value is introduced.
  49         test(from.wrapperType(), to.primitiveType(),        null, false);
  50     }
  51 
  52     public static void test(Class<?> from, Class<?> to, Object param, boolean failureExpected) throws Throwable {
  53         if (VERBOSE) System.out.printf("%-10s => %-10s: %5s: ", from.getSimpleName(), to.getSimpleName(), param);
  54 
  55         MethodHandle original = MethodHandles.identity(from);
  56         MethodType newType = original.type().changeReturnType(to);
  57 
  58         try {
  59             MethodHandle target = MethodHandles.explicitCastArguments(original, newType);
  60             Object result = target.invokeWithArguments(param);
  61 
  62             if (VERBOSE) {
  63                 String resultStr;
  64                 if (result != null) {
  65                     resultStr = String.format("%10s (%10s)", "'"+result+"'", result.getClass().getSimpleName());
  66                 } else {
  67                     resultStr = String.format("%10s", result);
  68                 }
  69                 System.out.println(resultStr);
  70             }
  71 
  72             if (failureExpected) {
  73                 String msg = String.format("No exception thrown: %s => %s; parameter: %s", from, to, param);
  74                 throw new AssertionError(msg);
  75             }
  76         } catch (AssertionError e) {
  77             throw e; // report test failure
  78         } catch (Throwable e) {
  79             if (VERBOSE) System.out.printf("%s: %s\n", e.getClass(), e.getMessage());
  80             if (!failureExpected) {
  81                 String msg = String.format("Unexpected exception was thrown: %s => %s; parameter: %s", from, to, param);
  82                 throw new AssertionError(msg, e);
  83             }
  84         }
  85     }
  86 }