jdk/test/java/lang/invoke/MethodHandles/CatchExceptionTest.java
author iignatyev
Mon, 01 Dec 2014 21:58:46 +0300
changeset 27781 eb64f15b3085
parent 26599 e82af948593b
child 29528 d1a052f7ccef
permissions -rw-r--r--
8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java Reviewed-by: vlivanov, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     1
/*
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     4
 *
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     8
 *
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    14
 *
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    18
 *
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    21
 * questions.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    22
 */
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    23
package test.java.lang.invoke.MethodHandles;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    24
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    25
import com.oracle.testlibrary.jsr292.Helper;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    26
import jdk.testlibrary.Asserts;
27781
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
    27
import jdk.testlibrary.TimeLimitedRunner;
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
    28
import jdk.testlibrary.Utils;
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    29
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    30
import java.lang.invoke.MethodHandle;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    31
import java.lang.invoke.MethodHandles;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    32
import java.lang.invoke.MethodType;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    33
import java.lang.reflect.Array;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    34
import java.util.*;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    35
import java.util.function.BiFunction;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    36
import java.util.function.Function;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    37
import java.util.function.Supplier;
27781
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
    38
import java.util.concurrent.TimeUnit;
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    39
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    40
/* @test
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    41
 * @library /lib/testlibrary/jsr292 /lib/testlibrary/
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    42
 * @compile CatchExceptionTest.java
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    43
 * @run main/othervm -esa test.java.lang.invoke.MethodHandles.CatchExceptionTest
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    44
 */
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    45
public class CatchExceptionTest {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    46
    private static final List<Class<?>> ARGS_CLASSES;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    47
    protected static final int MAX_ARITY = Helper.MAX_ARITY - 1;
26599
e82af948593b 8057707: TEST library enhancement in lib/testlibrary/jsr292/com/oracle/testlibrary/jsr292/Helper.java
kshefov
parents: 23594
diff changeset
    48
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    49
    static {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    50
        Class<?> classes[] = {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    51
                Object.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    52
                long.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    53
                int.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    54
                byte.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    55
                Integer[].class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    56
                double[].class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    57
                String.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    58
        };
26599
e82af948593b 8057707: TEST library enhancement in lib/testlibrary/jsr292/com/oracle/testlibrary/jsr292/Helper.java
kshefov
parents: 23594
diff changeset
    59
        ARGS_CLASSES = Collections.unmodifiableList(
e82af948593b 8057707: TEST library enhancement in lib/testlibrary/jsr292/com/oracle/testlibrary/jsr292/Helper.java
kshefov
parents: 23594
diff changeset
    60
                Helper.randomClasses(classes, MAX_ARITY));
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    61
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    62
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    63
    private final TestCase testCase;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    64
    private final int nargs;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    65
    private final int argsCount;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    66
    private final MethodHandle catcher;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    67
    private int dropped;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    68
    private MethodHandle thrower;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    69
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    70
    public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    71
            final int catchDrops) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    72
        this.testCase = testCase;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    73
        this.dropped = catchDrops;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    74
        if (Helper.IS_VERBOSE) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    75
            System.out.printf("CatchException::CatchException(%s, isVararg=%b " +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    76
                            "argsCount=%d catchDrops=%d)%n",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    77
                    testCase, isVararg, argsCount, catchDrops
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    78
            );
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    79
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    80
        MethodHandle thrower = testCase.thrower;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    81
        int throwerLen = thrower.type().parameterCount();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    82
        List<Class<?>> classes;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    83
        int extra = Math.max(0, argsCount - throwerLen);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    84
        classes = getThrowerParams(isVararg, extra);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    85
        this.argsCount = throwerLen + classes.size();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    86
        thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    87
        if (isVararg && argsCount > throwerLen) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    88
            MethodType mt = thrower.type();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    89
            Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    90
            thrower = thrower.asVarargsCollector(lastParam);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    91
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    92
        this.thrower = thrower;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    93
        this.dropped = Math.min(this.argsCount, catchDrops);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    94
        catcher = testCase.getCatcher(getCatcherParams());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    95
        nargs = Math.max(2, this.argsCount);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    96
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    97
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
    98
    public static void main(String[] args) throws Throwable {
27781
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
    99
        TestFactory factory = new TestFactory();
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   100
        long timeout = Helper.IS_THOROUGH ? 0L : Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT);
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   101
        // substract vm init time and reserve time for vm exit
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   102
        timeout *= 0.9;
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   103
        TimeLimitedRunner runner = new TimeLimitedRunner(timeout, 2.0d,
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   104
                () -> {
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   105
                    CatchExceptionTest test = factory.nextTest();
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   106
                    if (test != null) {
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   107
                        test.runTest();
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   108
                        return true;
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   109
                    }
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   110
                    return false;
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   111
                });
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   112
        for (CatchExceptionTest test : TestFactory.MANDATORY_TEST_CASES) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   113
            test.runTest();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   114
        }
27781
eb64f15b3085 8039953: [TESTBUG] Timeout java/lang/invoke/MethodHandles/CatchExceptionTest.java
iignatyev
parents: 26599
diff changeset
   115
        runner.call();
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   116
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   117
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   118
    private List<Class<?>> getThrowerParams(boolean isVararg, int argsCount) {
26599
e82af948593b 8057707: TEST library enhancement in lib/testlibrary/jsr292/com/oracle/testlibrary/jsr292/Helper.java
kshefov
parents: 23594
diff changeset
   119
        return Helper.getParams(ARGS_CLASSES, isVararg, argsCount);
23594
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   120
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   121
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   122
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   123
    private List<Class<?>> getCatcherParams() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   124
        int catchArgc = 1 + this.argsCount - dropped;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   125
        List<Class<?>> result = new ArrayList<>(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   126
                thrower.type().parameterList().subList(0, catchArgc - 1));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   127
        // prepend throwable
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   128
        result.add(0, testCase.throwableClass);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   129
        return result;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   130
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   131
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   132
    private void runTest() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   133
        Helper.clear();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   134
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   135
        Object[] args = Helper.randomArgs(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   136
                argsCount, thrower.type().parameterArray());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   137
        Object arg0 = Helper.MISSING_ARG;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   138
        Object arg1 = testCase.thrown;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   139
        if (argsCount > 0) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   140
            arg0 = args[0];
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   141
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   142
        if (argsCount > 1) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   143
            args[1] = arg1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   144
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   145
        Asserts.assertEQ(nargs, thrower.type().parameterCount());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   146
        if (argsCount < nargs) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   147
            Object[] appendArgs = {arg0, arg1};
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   148
            appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   149
            thrower = MethodHandles.insertArguments(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   150
                    thrower, argsCount, appendArgs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   151
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   152
        Asserts.assertEQ(argsCount, thrower.type().parameterCount());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   153
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   154
        MethodHandle target = MethodHandles.catchException(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   155
                testCase.filter(thrower), testCase.throwableClass,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   156
                testCase.filter(catcher));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   157
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   158
        Asserts.assertEQ(thrower.type(), target.type());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   159
        Asserts.assertEQ(argsCount, target.type().parameterCount());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   160
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   161
        Object returned;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   162
        try {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   163
            returned = target.invokeWithArguments(args);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   164
        } catch (Throwable ex) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   165
            testCase.assertCatch(ex);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   166
            returned = ex;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   167
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   168
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   169
        testCase.assertReturn(returned, arg0, arg1, dropped, args);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   170
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   171
}
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   172
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   173
class TestFactory {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   174
    public static final List<CatchExceptionTest> MANDATORY_TEST_CASES = new ArrayList<>();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   175
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   176
    private static final int MIN_TESTED_ARITY = 10;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   177
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   178
    static {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   179
        for (int[] args : new int[][]{
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   180
                {0, 0},
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   181
                {MIN_TESTED_ARITY, 0},
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   182
                {MIN_TESTED_ARITY, MIN_TESTED_ARITY},
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   183
                {CatchExceptionTest.MAX_ARITY, 0},
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   184
                {CatchExceptionTest.MAX_ARITY, CatchExceptionTest.MAX_ARITY},
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   185
        }) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   186
                MANDATORY_TEST_CASES.addAll(createTests(args[0], args[1]));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   187
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   188
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   189
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   190
    private int count;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   191
    private int args;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   192
    private int dropArgs;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   193
    private int currentMaxDrops;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   194
    private int maxArgs;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   195
    private int maxDrops;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   196
    private int constructor;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   197
    private int constructorSize;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   198
    private boolean isVararg;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   199
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   200
    public TestFactory() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   201
        if (Helper.IS_THOROUGH) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   202
            maxArgs = maxDrops = CatchExceptionTest.MAX_ARITY;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   203
        } else {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   204
            maxArgs = MIN_TESTED_ARITY
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   205
                    + Helper.RNG.nextInt(CatchExceptionTest.MAX_ARITY
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   206
                            - MIN_TESTED_ARITY)
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   207
                    + 1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   208
            maxDrops = MIN_TESTED_ARITY
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   209
                    + Helper.RNG.nextInt(maxArgs - MIN_TESTED_ARITY)
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   210
                    + 1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   211
            args = 1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   212
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   213
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   214
        if (Helper.IS_VERBOSE) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   215
            System.out.printf("maxArgs = %d%nmaxDrops = %d%n",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   216
                    maxArgs, maxDrops);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   217
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   218
        constructorSize = TestCase.CONSTRUCTORS.size();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   219
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   220
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   221
    private static List<CatchExceptionTest> createTests(int argsCount,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   222
            int catchDrops) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   223
        if (catchDrops > argsCount || argsCount < 0 || catchDrops < 0) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   224
            throw new IllegalArgumentException("argsCount = " + argsCount
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   225
                    + ", catchDrops = " + catchDrops
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   226
            );
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   227
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   228
        List<CatchExceptionTest> result = new ArrayList<>(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   229
                TestCase.CONSTRUCTORS.size());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   230
        for (Supplier<TestCase> constructor : TestCase.CONSTRUCTORS) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   231
            result.add(new CatchExceptionTest(constructor.get(),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   232
                    /* isVararg = */ true,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   233
                    argsCount,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   234
                    catchDrops));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   235
            result.add(new CatchExceptionTest(constructor.get(),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   236
                    /* isVararg = */ false,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   237
                    argsCount,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   238
                    catchDrops));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   239
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   240
        return result;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   241
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   242
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   243
    /**
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   244
     * @return next test from test matrix:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   245
     * {varArgs, noVarArgs} x TestCase.rtypes x TestCase.THROWABLES x {1, .., maxArgs } x {1, .., maxDrops}
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   246
     */
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   247
    public CatchExceptionTest nextTest() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   248
        if (constructor < constructorSize) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   249
            return createTest();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   250
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   251
        constructor = 0;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   252
        count++;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   253
        if (!Helper.IS_THOROUGH && count > Helper.TEST_LIMIT) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   254
            System.out.println("test limit is exceeded");
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   255
            return null;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   256
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   257
        if (dropArgs <= currentMaxDrops) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   258
            if (dropArgs == 1) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   259
                if (Helper.IS_THOROUGH || Helper.RNG.nextBoolean()) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   260
                    ++dropArgs;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   261
                    return createTest();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   262
                } else if (Helper.IS_VERBOSE) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   263
                    System.out.printf(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   264
                            "argsCount=%d : \"drop\" scenarios are skipped%n",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   265
                            args);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   266
                }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   267
            } else {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   268
                ++dropArgs;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   269
                return createTest();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   270
            }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   271
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   272
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   273
        if (args <= maxArgs) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   274
            dropArgs = 1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   275
            currentMaxDrops = Math.min(args, maxDrops);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   276
            ++args;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   277
            return createTest();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   278
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   279
        return null;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   280
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   281
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   282
    private CatchExceptionTest createTest() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   283
        if (!Helper.IS_THOROUGH) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   284
            return new CatchExceptionTest(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   285
                    TestCase.CONSTRUCTORS.get(constructor++).get(),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   286
                    Helper.RNG.nextBoolean(), args, dropArgs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   287
        } else {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   288
           if (isVararg) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   289
               isVararg = false;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   290
               return new CatchExceptionTest(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   291
                       TestCase.CONSTRUCTORS.get(constructor++).get(),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   292
                       isVararg, args, dropArgs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   293
           } else {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   294
               isVararg = true;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   295
               return new CatchExceptionTest(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   296
                       TestCase.CONSTRUCTORS.get(constructor).get(),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   297
                       isVararg, args, dropArgs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   298
           }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   299
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   300
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   301
}
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   302
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   303
class TestCase<T> {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   304
    private static enum ThrowMode {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   305
        NOTHING,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   306
        CAUGHT,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   307
        UNCAUGHT,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   308
        ADAPTER
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   309
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   310
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   311
    @SuppressWarnings("unchecked")
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   312
    public static final List<Supplier<TestCase>> CONSTRUCTORS;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   313
    private static final MethodHandle FAKE_IDENTITY;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   314
    private static final MethodHandle THROW_OR_RETURN;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   315
    private static final MethodHandle CATCHER;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   316
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   317
    static {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   318
        try {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   319
            MethodHandles.Lookup lookup = MethodHandles.lookup();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   320
            THROW_OR_RETURN = lookup.findStatic(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   321
                    TestCase.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   322
                    "throwOrReturn",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   323
                    MethodType.methodType(Object.class, Object.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   324
                            Throwable.class)
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   325
            );
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   326
            CATCHER = lookup.findStatic(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   327
                    TestCase.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   328
                    "catcher",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   329
                    MethodType.methodType(Object.class, Object.class));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   330
            FAKE_IDENTITY = lookup.findVirtual(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   331
                    TestCase.class, "fakeIdentity",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   332
                    MethodType.methodType(Object.class, Object.class));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   333
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   334
        } catch (NoSuchMethodException | IllegalAccessException e) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   335
            throw new Error(e);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   336
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   337
        PartialConstructor[] constructors = {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   338
                create(Object.class, Object.class::cast),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   339
                create(String.class, Objects::toString),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   340
                create(int[].class, x -> new int[]{Objects.hashCode(x)}),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   341
                create(long.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   342
                        x -> Objects.hashCode(x) & (-1L >>> 32)),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   343
                create(void.class, TestCase::noop)};
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   344
        Throwable[] throwables = {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   345
                new ClassCastException("testing"),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   346
                new java.io.IOException("testing"),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   347
                new LinkageError("testing")};
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   348
        List<Supplier<TestCase>> list = new ArrayList<>(constructors.length *
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   349
                throwables.length * ThrowMode.values().length);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   350
        //noinspection unchecked
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   351
        for (PartialConstructor f : constructors) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   352
            for (ThrowMode mode : ThrowMode.values()) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   353
                for (Throwable t : throwables) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   354
                    list.add(f.apply(mode, t));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   355
                }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   356
            }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   357
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   358
        CONSTRUCTORS = Collections.unmodifiableList(list);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   359
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   360
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   361
    public final Class<T> rtype;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   362
    public final ThrowMode throwMode;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   363
    public final Throwable thrown;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   364
    public final Class<? extends Throwable> throwableClass;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   365
    /**
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   366
     * MH which takes 2 args (Object,Throwable), 1st is the return value,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   367
     * 2nd is the exception which will be thrown, if it's supposed in current
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   368
     * {@link #throwMode}.
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   369
     */
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   370
    public final MethodHandle thrower;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   371
    private final Function<Object, T> cast;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   372
    protected MethodHandle filter;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   373
    private int fakeIdentityCount;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   374
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   375
    private TestCase(Class<T> rtype, Function<Object, T> cast,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   376
            ThrowMode throwMode, Throwable thrown)
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   377
            throws NoSuchMethodException, IllegalAccessException {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   378
        this.cast = cast;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   379
        filter = MethodHandles.lookup().findVirtual(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   380
                Function.class,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   381
                "apply",
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   382
                MethodType.methodType(Object.class, Object.class))
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   383
                              .bindTo(cast);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   384
        this.rtype = rtype;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   385
        this.throwMode = throwMode;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   386
        this.throwableClass = thrown.getClass();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   387
        switch (throwMode) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   388
            case NOTHING:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   389
                this.thrown = null;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   390
                break;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   391
            case ADAPTER:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   392
            case UNCAUGHT:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   393
                this.thrown = new Error("do not catch this");
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   394
                break;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   395
            default:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   396
                this.thrown = thrown;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   397
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   398
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   399
        MethodHandle throwOrReturn = THROW_OR_RETURN;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   400
        if (throwMode == ThrowMode.ADAPTER) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   401
            MethodHandle fakeIdentity = FAKE_IDENTITY.bindTo(this);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   402
            for (int i = 0; i < 10; ++i) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   403
                throwOrReturn = MethodHandles.filterReturnValue(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   404
                        throwOrReturn, fakeIdentity);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   405
            }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   406
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   407
        thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   408
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   409
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   410
    private static Void noop(Object x) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   411
        return null;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   412
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   413
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   414
    private static <T2> PartialConstructor create(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   415
            Class<T2> rtype, Function<Object, T2> cast) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   416
        return (t, u) -> () -> {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   417
            try {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   418
                return new TestCase<>(rtype, cast, t, u);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   419
            } catch (NoSuchMethodException | IllegalAccessException e) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   420
                throw new Error(e);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   421
            }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   422
        };
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   423
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   424
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   425
    private static <T extends Throwable>
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   426
    Object throwOrReturn(Object normal, T exception) throws T {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   427
        if (exception != null) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   428
            Helper.called("throwOrReturn/throw", normal, exception);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   429
            throw exception;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   430
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   431
        Helper.called("throwOrReturn/normal", normal, exception);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   432
        return normal;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   433
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   434
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   435
    private static <T extends Throwable>
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   436
    Object catcher(Object o) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   437
        Helper.called("catcher", o);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   438
        return o;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   439
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   440
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   441
    public MethodHandle filter(MethodHandle target) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   442
        return MethodHandles.filterReturnValue(target, filter);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   443
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   444
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   445
    public MethodHandle getCatcher(List<Class<?>> classes) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   446
        return MethodHandles.filterReturnValue(Helper.AS_LIST.asType(
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   447
                        MethodType.methodType(Object.class, classes)),
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   448
                CATCHER
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   449
        );
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   450
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   451
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   452
    @Override
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   453
    public String toString() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   454
        return "TestCase{" +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   455
                "rtype=" + rtype +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   456
                ", throwMode=" + throwMode +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   457
                ", throwableClass=" + throwableClass +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   458
                '}';
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   459
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   460
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   461
    public String callName() {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   462
        return "throwOrReturn/" +
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   463
                (throwMode == ThrowMode.NOTHING
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   464
                        ? "normal"
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   465
                        : "throw");
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   466
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   467
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   468
    public void assertReturn(Object returned, Object arg0, Object arg1,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   469
            int catchDrops, Object... args) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   470
        int lag = 0;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   471
        if (throwMode == ThrowMode.CAUGHT) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   472
            lag = 1;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   473
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   474
        Helper.assertCalled(lag, callName(), arg0, arg1);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   475
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   476
        if (throwMode == ThrowMode.NOTHING) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   477
            assertEQ(cast.apply(arg0), returned);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   478
        } else if (throwMode == ThrowMode.CAUGHT) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   479
            List<Object> catchArgs = new ArrayList<>(Arrays.asList(args));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   480
            // catcher receives an initial subsequence of target arguments:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   481
            catchArgs.subList(args.length - catchDrops, args.length).clear();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   482
            // catcher also receives the exception, prepended:
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   483
            catchArgs.add(0, thrown);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   484
            Helper.assertCalled("catcher", catchArgs);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   485
            assertEQ(cast.apply(catchArgs), returned);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   486
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   487
        Asserts.assertEQ(0, fakeIdentityCount);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   488
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   489
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   490
    private void assertEQ(T t, Object returned) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   491
        if (rtype.isArray()) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   492
            Asserts.assertEQ(t.getClass(), returned.getClass());
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   493
            int n = Array.getLength(t);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   494
            Asserts.assertEQ(n, Array.getLength(returned));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   495
            for (int i = 0; i < n; ++i) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   496
                Asserts.assertEQ(Array.get(t, i), Array.get(returned, i));
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   497
            }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   498
        } else {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   499
            Asserts.assertEQ(t, returned);
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   500
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   501
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   502
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   503
    private Object fakeIdentity(Object x) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   504
        System.out.println("should throw through this!");
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   505
        ++fakeIdentityCount;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   506
        return x;
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   507
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   508
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   509
    public void assertCatch(Throwable ex) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   510
        try {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   511
            Asserts.assertSame(thrown, ex,
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   512
                    "must get the out-of-band exception");
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   513
        } catch (Throwable t) {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   514
            ex.printStackTrace();
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   515
        }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   516
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   517
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   518
    public interface PartialConstructor
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   519
            extends BiFunction<ThrowMode, Throwable, Supplier<TestCase>> {
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   520
    }
505c3a4eb0d6 8038186: [TESTBUG] improvements of test j.l.i.MethodHandles
iignatyev
parents:
diff changeset
   521
}