ExplicitCastArgumentsTest.java 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package java.lang.invoke;

import sun.invoke.util.Wrapper;

/* @test
 * @summary unit tests for MethodHandles.explicitCastArguments()
 *
 * @run main/bootclasspath java.lang.invoke.ExplicitCastArgumentsTest
 */
public class ExplicitCastArgumentsTest {
    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 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        testVarargsCollector();
        testRef2Prim();
        System.out.println("TEST PASSED");
    }

    public static String[] f(String... args) { return args; }

    public static void testVarargsCollector() throws Throwable {
        MethodType mt = MethodType.methodType(String[].class, String[].class);
        MethodHandle mh = MethodHandles.publicLookup().findStatic(THIS_CLASS, "f", mt);
        mh = MethodHandles.explicitCastArguments(mh, MethodType.methodType(Object.class, Object.class));
        mh.invokeWithArguments((Object)(new String[] {"str1", "str2"}));
    }

    public static void testRef2Prim() throws Throwable {
53 54 55
        for (Wrapper from : Wrapper.values()) {
            for (Wrapper to : Wrapper.values()) {
                if (from == Wrapper.VOID || to == Wrapper.VOID) continue;
56
                testRef2Prim(from, to);
57 58 59 60 61 62 63
            }
        }
    }

    public static void testRef2Prim(Wrapper from, Wrapper to) throws Throwable {
        // MHs.eCA javadoc:
        //    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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }

    public static void test(Class<?> from, Class<?> to, Object param, boolean failureExpected) throws Throwable {
        if (VERBOSE) System.out.printf("%-10s => %-10s: %5s: ", from.getSimpleName(), to.getSimpleName(), param);

        MethodHandle original = MethodHandles.identity(from);
        MethodType newType = original.type().changeReturnType(to);

        try {
            MethodHandle target = MethodHandles.explicitCastArguments(original, newType);
            Object result = target.invokeWithArguments(param);

            if (VERBOSE) {
                String resultStr;
                if (result != null) {
                    resultStr = String.format("%10s (%10s)", "'"+result+"'", result.getClass().getSimpleName());
                } else {
                    resultStr = String.format("%10s", result);
                }
                System.out.println(resultStr);
            }

            if (failureExpected) {
                String msg = String.format("No exception thrown: %s => %s; parameter: %s", from, to, param);
                throw new AssertionError(msg);
            }
        } catch (AssertionError e) {
            throw e; // report test failure
        } catch (Throwable e) {
            if (VERBOSE) System.out.printf("%s: %s\n", e.getClass(), e.getMessage());
            if (!failureExpected) {
                String msg = String.format("Unexpected exception was thrown: %s => %s; parameter: %s", from, to, param);
                throw new AssertionError(msg, e);
            }
        }
    }
}