jdk/test/java/lang/invoke/TestCatchExceptionWithVarargs.java
author twisti
Wed, 03 Jul 2013 11:35:06 -0700
changeset 18769 53b3406abedf
child 21596 0e3a39f29dbc
permissions -rw-r--r--
8019184: MethodHandles.catchException() fails when methods have 8 args + varargs Reviewed-by: jrose
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
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    10
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * 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
    14
 * 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
    15
 * accompanied this code).
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    16
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * 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
    20
 *
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    21
 * 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
    22
 * 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
    23
 * questions.
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
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    26
/*
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    27
 * @test
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    28
 * @bug 8019184
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    29
 * @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
    30
 */
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    31
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    32
import java.util.*;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    33
import java.lang.invoke.*;
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
public class TestCatchExceptionWithVarargs {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    36
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    37
    private static final Class<?> CLASS = TestCatchExceptionWithVarargs.class;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    38
    private static final int MAX_MH_ARITY = 254;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    39
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    40
    public static MethodHandle target;
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    41
    public static MethodHandle handler;
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
    private static Object firstArg;
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
    static class MyException extends Exception {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    46
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    47
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    48
    public static Object target(Object... a) throws Exception {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    49
        if (a[0] != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    50
            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
    51
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    52
        throw new MyException();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    53
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    54
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    55
    public static Object handler(Object... a) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    56
        if (a[0] != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    57
            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
    58
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    59
        return a[0];
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    60
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    61
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    62
    static {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    63
        try {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    64
            MethodType mtype = MethodType.methodType(Object.class, Object[].class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    65
            target = MethodHandles.lookup().findStatic(CLASS, "target", mtype);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    66
            handler = MethodHandles.lookup().findStatic(CLASS, "handler", mtype);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    67
        } catch (Exception e) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    68
            throw new AssertionError(e);
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
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    71
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    72
    public static void main(String[] args) throws Throwable {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    73
        List<Class<?>> ptypes = new LinkedList<>();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    74
        ptypes.add(Object[].class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    75
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    76
        // 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
    77
        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
    78
            ptypes.add(0, Object.class);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    79
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    80
            MethodHandle targetWithArgs = target.asType(MethodType.methodType(Object.class, ptypes));
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    81
            MethodHandle handlerWithArgs = handler.asType(MethodType.methodType(Object.class, ptypes));
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    82
            handlerWithArgs = MethodHandles.dropArguments(handlerWithArgs, 0, MyException.class);
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
            MethodHandle gwc1 = MethodHandles.catchException(targetWithArgs, MyException.class, handlerWithArgs);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    85
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    86
            // 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
    87
            MethodHandle gwc2 = MethodHandles.catchException(gwc1, MyException.class, handlerWithArgs);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    88
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    89
            // 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
    90
            firstArg = new Object();
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    91
            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
    92
            if (o != firstArg) {
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    93
                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
    94
            }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    95
        }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    96
    }
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents:
diff changeset
    97
}