jdk/test/java/lang/invoke/TestCatchExceptionWithVarargs.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 21596 0e3a39f29dbc
child 31178 6ad68e35506d
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18769
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     1
/*
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     4
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 18769
diff changeset
     7
 * published by the Free Software Foundation.
18769
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     8
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    13
 * accompanied this code).
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    14
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    18
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    21
 * questions.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    22
 */
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    23
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    24
/*
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    25
 * @test
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    26
 * @bug 8019184
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    27
 * @summary MethodHandles.catchException() fails when methods have 8 args + varargs
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    28
 */
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    29
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    30
import java.util.*;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    31
import java.lang.invoke.*;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    32
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    33
public class TestCatchExceptionWithVarargs {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    34
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    35
    private static final Class<?> CLASS = TestCatchExceptionWithVarargs.class;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    36
    private static final int MAX_MH_ARITY = 254;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    37
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    38
    public static MethodHandle target;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    39
    public static MethodHandle handler;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    40
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    41
    private static Object firstArg;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    42
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    43
    static class MyException extends Exception {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    44
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    45
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    46
    public static Object target(Object... a) throws Exception {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    47
        if (a[0] != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    48
            throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    49
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    50
        throw new MyException();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    51
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    52
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    53
    public static Object handler(Object... a) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    54
        if (a[0] != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    55
            throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    56
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    57
        return a[0];
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    58
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    59
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    60
    static {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    61
        try {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    62
            MethodType mtype = MethodType.methodType(Object.class, Object[].class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    63
            target = MethodHandles.lookup().findStatic(CLASS, "target", mtype);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    64
            handler = MethodHandles.lookup().findStatic(CLASS, "handler", mtype);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    65
        } catch (Exception e) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    66
            throw new AssertionError(e);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    67
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    68
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    69
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    70
    public static void main(String[] args) throws Throwable {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    71
        List<Class<?>> ptypes = new LinkedList<>();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    72
        ptypes.add(Object[].class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    73
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    74
        // We use MAX_MH_ARITY - 1 here to account for the Object[] argument.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    75
        for (int i = 1; i < MAX_MH_ARITY - 1; i++) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    76
            ptypes.add(0, Object.class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    77
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    78
            MethodHandle targetWithArgs = target.asType(MethodType.methodType(Object.class, ptypes));
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    79
            MethodHandle handlerWithArgs = handler.asType(MethodType.methodType(Object.class, ptypes));
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    80
            handlerWithArgs = MethodHandles.dropArguments(handlerWithArgs, 0, MyException.class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    81
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    82
            MethodHandle gwc1 = MethodHandles.catchException(targetWithArgs, MyException.class, handlerWithArgs);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    83
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    84
            // The next line throws an IllegalArgumentException if there is a bug.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    85
            MethodHandle gwc2 = MethodHandles.catchException(gwc1, MyException.class, handlerWithArgs);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    86
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    87
            // This is only to verify that the method handles can actually be invoked and do the right thing.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    88
            firstArg = new Object();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    89
            Object o = gwc2.asSpreader(Object[].class, ptypes.size() - 1).invoke(firstArg, new Object[i]);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    90
            if (o != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    91
                throw new AssertionError("return value different than expected: " + o + " != " + firstArg);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    92
            }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    93
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    94
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    95
}