jdk/test/java/lang/invoke/ThrowExceptionsTest.java
author jrose
Sat, 16 Jul 2011 15:44:33 -0700
changeset 10081 0f7b9636aa64
child 11534 9949ffb8eb3a
permissions -rw-r--r--
7058651: JSR 292 unit tests need a refresh Summary: Enhancements to unit tests. Reviewed-by: never, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     1
/*
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     4
 *
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    10
 *
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    15
 * accompanied this code).
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    16
 *
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    20
 *
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    23
 * questions.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    24
 */
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    25
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    26
/* @test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    27
 * @summary unit tests for method handles which permute their arguments
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    28
 * @run junit test.java.lang.invoke.ThrowExceptionsTest
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    29
 */
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    30
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    31
package test.java.lang.invoke;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    32
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    33
import org.junit.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    34
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    35
import java.util.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    36
import java.lang.reflect.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    37
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    38
import java.lang.invoke.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    39
import static java.lang.invoke.MethodHandles.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    40
import static java.lang.invoke.MethodType.*;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    41
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    42
public class ThrowExceptionsTest {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    43
    private static final Class CLASS = ThrowExceptionsTest.class;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    44
    private static final Lookup LOOKUP = lookup();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    45
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    46
    public static void main(String argv[]) throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    47
        new ThrowExceptionsTest().testAll((argv.length == 0 ? null : Arrays.asList(argv).toString()));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    48
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    49
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    50
    @Test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    51
    public void testWMT() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    52
        // mostly call testWMTCallee, but sometimes call its void-returning variant
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    53
        MethodHandle mh = testWMTCallee();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    54
        MethodHandle mh1 = mh.asType(mh.type().changeReturnType(void.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    55
        assert(mh1 != mh);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    56
        testWMT(mh, mh1, 1000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    57
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    58
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    59
    @Test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    60
    public void testBoundWMT() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    61
        // mostly call exactInvoker.bindTo(testWMTCallee), but sometimes call its void-returning variant
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    62
        MethodHandle callee = testWMTCallee();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    63
        MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    64
        MethodHandle invoker = exactInvoker(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    65
        MethodHandle mh  = invoker.bindTo(callee);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    66
        MethodHandle mh1 = invoker.bindTo(callee1);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    67
        testWMT(mh, mh1, 1000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    68
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    69
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    70
    @Test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    71
    public void testFoldWMT() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    72
        // mostly call exactInvoker.fold(constant(testWMTCallee)), but sometimes call its void-returning variant
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    73
        MethodHandle callee = testWMTCallee();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    74
        MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    75
        MethodHandle invoker = exactInvoker(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    76
        MethodHandle mh  = foldArguments(invoker, constant(MethodHandle.class, callee));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    77
        MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    78
        testWMT(mh, mh1, 1000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    79
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    80
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    81
    @Test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    82
    public void testFoldCCE() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    83
        MethodHandle callee = testWMTCallee();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    84
        MethodHandle callee1 = callee.asType(callee.type().changeParameterType(1, Number.class)).asType(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    85
        MethodHandle invoker = exactInvoker(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    86
        MethodHandle mh  = foldArguments(invoker, constant(MethodHandle.class, callee));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    87
        MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    88
        testWMT(mh, mh1, 1000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    89
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    90
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    91
    @Test
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    92
    public void testStackOverflow() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    93
        MethodHandle callee = testWMTCallee();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    94
        MethodHandle callee1 = makeStackOverflow().asType(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    95
        MethodHandle invoker = exactInvoker(callee.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    96
        MethodHandle mh  = foldArguments(invoker, constant(MethodHandle.class, callee));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    97
        MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    98
        for (int i = 0; i < REPEAT; i++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
    99
            try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   100
                testWMT(mh, mh1, 1000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   101
            } catch (StackOverflowError ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   102
                // OK, try again
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   103
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   104
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   105
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   106
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   107
    private static MethodHandle makeStackOverflow() {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   108
        MethodType cellType = methodType(void.class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   109
        MethodHandle[] cell = { null };  // recursion point
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   110
        MethodHandle getCell = insertArguments(arrayElementGetter(cell.getClass()), 0, cell, 0);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   111
        MethodHandle invokeCell = foldArguments(exactInvoker(cellType), getCell);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   112
        assert(invokeCell.type() == cellType);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   113
        cell[0] = invokeCell;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   114
        // make it conformable to any type:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   115
        invokeCell = dropArguments(invokeCell, 0, Object[].class).asVarargsCollector(Object[].class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   116
        return invokeCell;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   117
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   118
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   119
    static int testCases;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   120
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   121
    private void testAll(String match) throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   122
        testCases = 0;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   123
        Lookup lookup = lookup();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   124
        for (Method m : CLASS.getDeclaredMethods()) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   125
            String name = m.getName();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   126
            if (name.startsWith("test") &&
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   127
                (match == null || match.contains(name.substring("test".length()))) &&
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   128
                m.getParameterTypes().length == 0 &&
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   129
                Modifier.isPublic(m.getModifiers()) &&
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   130
                !Modifier.isStatic(m.getModifiers())) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   131
                System.out.println("["+name+"]");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   132
                int tc = testCases;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   133
                try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   134
                    m.invoke(this);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   135
                } catch (Throwable ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   136
                    System.out.println("*** "+ex);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   137
                    ex.printStackTrace();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   138
                }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   139
                if (testCases == tc)  testCases++;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   140
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   141
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   142
        if (testCases == 0)  throw new RuntimeException("no test cases found");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   143
        System.out.println("ran a total of "+testCases+" test cases");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   144
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   145
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   146
    private static MethodHandle findStatic(String name) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   147
        return findMethod(name, true);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   148
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   149
    private static MethodHandle findVirtual(String name) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   150
        return findMethod(name, false);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   151
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   152
    private static MethodHandle findMethod(String name, boolean isStatic) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   153
        MethodHandle mh = null;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   154
        for (Method m : CLASS.getDeclaredMethods()) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   155
            if (m.getName().equals(name) &&
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   156
                Modifier.isStatic(m.getModifiers()) == isStatic) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   157
                if (mh != null)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   158
                    throw new RuntimeException("duplicate methods: "+name);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   159
                try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   160
                    mh = LOOKUP.unreflect(m);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   161
                } catch (ReflectiveOperationException ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   162
                    throw new RuntimeException(ex);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   163
                }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   164
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   165
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   166
        if (mh == null)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   167
            throw new RuntimeException("no method: "+name);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   168
        return mh;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   169
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   170
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   171
    int testWMTCallee;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   172
    private int testWMTCallee(String x) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   173
        return testWMTCallee++;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   174
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   175
    private static MethodHandle testWMTCallee() {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   176
        MethodHandle callee = findVirtual("testWMTCallee");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   177
        // FIXME: should not have to retype callee
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   178
        callee = callee.asType(callee.type().changeParameterType(0, Object.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   179
        return callee;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   180
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   181
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   182
    private Exception testWMT(MethodHandle[] mhs, int reps) throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   183
        testCases += 1;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   184
        testWMTCallee = 0;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   185
        int catches = 0;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   186
        Exception savedEx = null;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   187
        for (int i = 0; i < reps; i++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   188
            MethodHandle mh = mhs[i % mhs.length];
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   189
            int n;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   190
            try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   191
                // FIXME: should not have to retype this
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   192
                n = (int) mh.invokeExact((Object)this, "x");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   193
                assertEquals(n, i - catches);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   194
                // Using the exact type for this causes endless deopt due to
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   195
                // 'non_cached_result' in SystemDictionary::find_method_handle_invoke.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   196
                // The problem is that the compiler thread needs to access a cached
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   197
                // invoke method, but invoke methods are not cached if one of the
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   198
                // component types is not on the BCP.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   199
            } catch (Exception ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   200
                savedEx = ex;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   201
                catches++;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   202
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   203
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   204
        //VERBOSE: System.out.println("reps="+reps+" catches="+catches);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   205
        return savedEx;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   206
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   207
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   208
    private static final int REPEAT = Integer.getInteger(CLASS.getSimpleName()+".REPEAT", 10);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   209
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   210
    private Exception testWMT(MethodHandle mh, MethodHandle mh1, int reps) throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   211
        //VERBOSE: System.out.println("mh="+mh+" mh1="+mh1);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   212
        MethodHandle[] mhs = new MethodHandle[100];
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   213
        Arrays.fill(mhs, mh);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   214
        int patch = mhs.length-1;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   215
        Exception savedEx = null;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   216
        for (int i = 0; i < REPEAT; i++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   217
            mhs[patch] = mh;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   218
            testWMT(mhs, 10000);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   219
            mhs[patch] = mh1;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   220
            savedEx = testWMT(mhs, reps);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   221
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   222
        return savedEx;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   223
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   224
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   225
   private static void assertEquals(Object x, Object y) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   226
        if (x == y || x != null && x.equals(y))  return;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   227
        throw new RuntimeException(x+" != "+y);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   228
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents:
diff changeset
   229
}