jdk/test/java/dyn/MethodHandlesTest.java
author jrose
Fri, 30 Apr 2010 23:48:23 -0700
changeset 5722 4ada807383c8
parent 4537 7c3c7f8d5195
child 5723 a58a0eed34b0
permissions -rw-r--r--
6939134: JSR 292 adjustments to method handle invocation Summary: split MethodHandle.invoke into invokeExact and invokeGeneric; also clean up JVM-to-Java interfaces Reviewed-by: twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     1
/*
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
     2
 * Copyright 2009-2010 Sun Microsystems, Inc.  All Rights Reserved.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     4
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    10
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    15
 * accompanied this code).
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    16
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    20
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    23
 * have any questions.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    24
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    25
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    26
/* @test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    27
 * @summary unit tests for java.dyn.MethodHandles
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    28
 * @compile -XDinvokedynamic MethodHandlesTest.java
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    29
 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic test.java.dyn.MethodHandlesTest
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    30
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    31
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    32
package test.java.dyn;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    33
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    34
import java.dyn.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    35
import java.dyn.MethodHandles.Lookup;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    36
import java.lang.reflect.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    37
import java.util.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    38
import org.junit.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    39
import static org.junit.Assert.*;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    40
import static org.junit.Assume.*;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    41
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    42
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    43
/**
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    44
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    45
 * @author jrose
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    46
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    47
public class MethodHandlesTest {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    48
    // How much output?
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    49
    static int verbosity = 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    50
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    51
    // Set this true during development if you want to fast-forward to
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    52
    // a particular new, non-working test.  Tests which are known to
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    53
    // work (or have recently worked) test this flag and return on true.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    54
    static boolean CAN_SKIP_WORKING = false;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    55
    //static { CAN_SKIP_WORKING = true; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    56
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    57
    // Set true to test more calls.  If false, some tests are just
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    58
    // lookups, without exercising the actual method handle.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    59
    static boolean DO_MORE_CALLS = true;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    60
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    61
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    62
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    63
    public void testFirst() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    64
        verbosity += 9; try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    65
            // left blank for debugging
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    66
        } finally { printCounts(); verbosity -= 9; }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    67
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    68
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    69
    // current failures
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    70
    @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    71
    public void testFail_1() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    72
        // AMH.<init>: IllegalArgumentException: bad adapter (conversion=0xfffab300): adapter pushes too many parameters
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    73
        testSpreadArguments(int.class, 0, 6);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    74
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    75
    @Test @Ignore("failure in JVM when expanding the stack using asm stub for _adapter_spread_args")
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    76
    public void testFail_2() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    77
        // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    78
        testSpreadArguments(Object.class, 0, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    79
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    80
    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    81
    public void testFail_3() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    82
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    83
        testSpreadArguments(int.class, 1, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    84
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    85
    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    86
    public void testFail_4() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    87
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    88
        testCollectArguments(int.class, 1, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    89
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    90
    @Test @Ignore("cannot collect leading primitive types")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    91
    public void testFail_5() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    92
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    93
        testInvokers(MethodType.genericMethodType(2).changeParameterType(0, int.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    94
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    95
    @Test @Ignore("should not insert arguments beyond MethodHandlePushLimit")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    96
    public void testFail_6() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    97
        // ValueConversions.varargsArray: UnsupportedOperationException: NYI: cannot form a varargs array of length 13
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    98
        testInsertArguments(0, 0, MAX_ARG_INCREASE+10);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    99
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   100
    static final int MAX_ARG_INCREASE = 3;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   101
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   102
    public MethodHandlesTest() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   103
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   104
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   105
    @Before
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   106
    public void checkImplementedPlatform() {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   107
        boolean platformOK = false;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   108
        Properties properties = System.getProperties();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   109
        String vers = properties.getProperty("java.vm.version");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   110
        String name = properties.getProperty("java.vm.name");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   111
        String arch = properties.getProperty("os.arch");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   112
        if ((arch.equals("i386")  || arch.equals("amd64") ||
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   113
             arch.equals("sparc") || arch.equals("sparcv9")) &&
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   114
            (name.contains("Client") || name.contains("Server"))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   115
            ) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   116
            platformOK = true;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   117
        } else {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   118
            System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   119
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   120
        assumeTrue(platformOK);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   121
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   122
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   123
    String testName;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   124
    int posTests, negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   125
    @After
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   126
    public void printCounts() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   127
        if (verbosity >= 2 && (posTests | negTests) != 0) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   128
            System.out.println();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   129
            if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   130
            if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   131
            posTests = negTests = 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   132
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   133
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   134
    void countTest(boolean positive) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   135
        if (positive) ++posTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   136
        else          ++negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   137
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   138
    void countTest() { countTest(true); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   139
    void startTest(String name) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   140
        if (testName != null)  printCounts();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   141
        if (verbosity >= 1)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   142
            System.out.println(name);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   143
        posTests = negTests = 0;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   144
        testName = name;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   145
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   146
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   147
    @BeforeClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   148
    public static void setUpClass() throws Exception {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   149
        calledLog.clear();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   150
        calledLog.add(null);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   151
        nextArgVal = INITIAL_ARG_VAL;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   152
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   153
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   154
    @AfterClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   155
    public static void tearDownClass() throws Exception {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   156
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   157
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   158
    static List<Object> calledLog = new ArrayList<Object>();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   159
    static Object logEntry(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   160
        return Arrays.asList(name, Arrays.asList(args));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   161
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   162
    static Object called(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   163
        Object entry = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   164
        calledLog.add(entry);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   165
        return entry;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   166
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   167
    static void assertCalled(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   168
        Object expected = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   169
        Object actual   = calledLog.get(calledLog.size() - 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   170
        if (expected.equals(actual) && verbosity < 9)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   171
        System.out.println("assertCalled "+name+":");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   172
        System.out.println("expected:   "+expected);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   173
        System.out.println("actual:     "+actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   174
        System.out.println("ex. types:  "+getClasses(expected));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   175
        System.out.println("act. types: "+getClasses(actual));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   176
        assertEquals("previous method call", expected, actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   177
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   178
    static void printCalled(MethodHandle target, String name, Object... args) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   179
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   180
            System.out.println("calling MH="+target+" to "+name+Arrays.toString(args));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   181
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   182
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   183
    static Object castToWrapper(Object value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   184
        Object wrap = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   185
        if (value instanceof Number)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   186
            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   187
        if (value instanceof Character)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   188
            wrap = castToWrapperOrNull((char)(Character)value, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   189
        if (wrap != null)  return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   190
        return dst.cast(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   191
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   192
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   193
    static Object castToWrapperOrNull(long value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   194
        if (dst == int.class || dst == Integer.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   195
            return (int)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   196
        if (dst == long.class || dst == Long.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   197
            return (long)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   198
        if (dst == char.class || dst == Character.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   199
            return (char)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   200
        if (dst == short.class || dst == Short.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   201
            return (short)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   202
        if (dst == float.class || dst == Float.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   203
            return (float)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   204
        if (dst == double.class || dst == Double.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   205
            return (double)(value);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   206
        if (dst == byte.class || dst == Byte.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   207
            return (byte)(value);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   208
        if (dst == boolean.class || dst == boolean.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   209
            return ((value % 29) & 1) == 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   210
        return null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   211
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   212
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   213
    static final int ONE_MILLION = (1000*1000),  // first int value
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   214
                     TEN_BILLION = (10*1000*1000*1000),  // scale factor to reach upper 32 bits
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   215
                     INITIAL_ARG_VAL = ONE_MILLION << 1;  // <<1 makes space for sign bit;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   216
    static long nextArgVal;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   217
    static long nextArg(boolean moreBits) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   218
        long val = nextArgVal++;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   219
        long sign = -(val & 1); // alternate signs
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   220
        val >>= 1;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   221
        if (moreBits)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   222
            // Guarantee some bits in the high word.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   223
            // In any case keep the decimal representation simple-looking,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   224
            // with lots of zeroes, so as not to make the printed decimal
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   225
            // strings unnecessarily noisy.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   226
            val += (val % ONE_MILLION) * TEN_BILLION;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   227
        return val ^ sign;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   228
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   229
    static int nextArg() {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   230
        // Produce a 32-bit result something like ONE_MILLION+(smallint).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   231
        // Example: 1_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   232
        return (int) nextArg(false);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   233
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   234
    static long nextArg(Class<?> kind) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   235
        if (kind == long.class   || kind == Long.class ||
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   236
            kind == double.class || kind == Double.class)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   237
            // produce a 64-bit result something like
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   238
            // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   239
            // Example: 10_000_420_001_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   240
            return nextArg(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   241
        return (long) nextArg();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   242
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   243
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   244
    static Object randomArg(Class<?> param) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   245
        Object wrap = castToWrapperOrNull(nextArg(param), param);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   246
        if (wrap != null) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   247
            return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   248
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   249
//        import sun.dyn.util.Wrapper;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   250
//        Wrapper wrap = Wrapper.forBasicType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   251
//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   252
//            wrap = Wrapper.forWrapperType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   253
//        if (wrap != Wrapper.OBJECT)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   254
//            return wrap.wrap(nextArg++);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   255
        if (param.isInterface() || param.isAssignableFrom(String.class))
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   256
            return "#"+nextArg();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   257
        else
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   258
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   259
                return param.newInstance();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   260
            } catch (InstantiationException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   261
            } catch (IllegalAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   262
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   263
        return null;  // random class not Object, String, Integer, etc.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   264
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   265
    static Object[] randomArgs(Class<?>... params) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   266
        Object[] args = new Object[params.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   267
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   268
            args[i] = randomArg(params[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   269
        return args;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   270
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   271
    static Object[] randomArgs(int nargs, Class<?> param) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   272
        Object[] args = new Object[nargs];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   273
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   274
            args[i] = randomArg(param);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   275
        return args;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   276
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   277
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   278
    static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   279
        return Arrays.copyOf(a, a.length, atype);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   280
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   281
    static <T> T[] cat(T[] a, T... b) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   282
        int alen = a.length, blen = b.length;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   283
        if (blen == 0)  return a;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   284
        T[] c = Arrays.copyOf(a, alen + blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   285
        System.arraycopy(b, 0, c, alen, blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   286
        return c;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   287
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   288
    static Integer[] boxAll(int... vx) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   289
        Integer[] res = new Integer[vx.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   290
        for (int i = 0; i < res.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   291
            res[i] = vx[i];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   292
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   293
        return res;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   294
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   295
    static Object getClasses(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   296
        if (x == null)  return x;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   297
        if (x instanceof String)  return x;  // keep the name
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   298
        if (x instanceof List) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   299
            // recursively report classes of the list elements
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   300
            Object[] xa = ((List)x).toArray();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   301
            for (int i = 0; i < xa.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   302
                xa[i] = getClasses(xa[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   303
            return Arrays.asList(xa);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   304
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   305
        return x.getClass().getSimpleName();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   306
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   307
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   308
    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   309
        return changeArgTypes(target, 0, 999, argType);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   310
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   311
    static MethodHandle changeArgTypes(MethodHandle target,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   312
            int beg, int end, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   313
        MethodType targetType = target.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   314
        end = Math.min(end, targetType.parameterCount());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   315
        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   316
        Collections.fill(argTypes.subList(beg, end), argType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   317
        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   318
        return MethodHandles.convertArguments(target, ttype2);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   319
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   320
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   321
    // This lookup is good for all members in and under MethodHandlesTest.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   322
    static final Lookup PRIVATE = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   323
    // This lookup is good for package-private members but not private ones.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   324
    static final Lookup PACKAGE = PackageSibling.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   325
    // This lookup is good only for public members.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   326
    static final Lookup PUBLIC  = MethodHandles.publicLookup();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   327
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   328
    // Subject methods...
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   329
    static class Example implements IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   330
        final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   331
        public Example() { name = "Example#"+nextArg(); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   332
        protected Example(String name) { this.name = name; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   333
        protected Example(int x) { this(); called("protected <init>", this, x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   334
        @Override public String toString() { return name; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   335
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   336
        public void            v0()     { called("v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   337
        void                   pkg_v0() { called("pkg_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   338
        private void           pri_v0() { called("pri_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   339
        public static void     s0()     { called("s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   340
        static void            pkg_s0() { called("pkg_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   341
        private static void    pri_s0() { called("pri_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   342
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   343
        public Object          v1(Object x) { return called("v1", this, x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   344
        public Object          v2(Object x, Object y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   345
        public Object          v2(Object x, int    y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   346
        public Object          v2(int    x, Object y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   347
        public Object          v2(int    x, int    y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   348
        public static Object   s1(Object x) { return called("s1", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   349
        public static Object   s2(int x)    { return called("s2", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   350
        public static Object   s3(long x)   { return called("s3", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   351
        public static Object   s4(int x, int y) { return called("s4", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   352
        public static Object   s5(long x, int y) { return called("s5", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   353
        public static Object   s6(int x, long y) { return called("s6", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   354
        public static Object   s7(float x, double y) { return called("s7", x, y); }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   355
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   356
        static final Lookup EXAMPLE = MethodHandles.lookup();  // for testing findSpecial
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   357
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   358
    static final Lookup EXAMPLE = Example.EXAMPLE;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   359
    public static class PubExample extends Example {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   360
        public PubExample() { super("PubExample#"+nextArg()); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   361
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   362
    static class SubExample extends Example {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   363
        @Override public void  v0()     { called("Sub/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   364
        @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   365
        private      SubExample(int x)  { called("<init>", this, x); }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   366
        public SubExample() { super("SubExample#"+nextArg()); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   367
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   368
    public static interface IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   369
        public void            v0();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   370
        static class Impl implements IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   371
            public void        v0()     { called("Int/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   372
            final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   373
            public Impl() { name = "Impl#"+nextArg(); }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   374
            @Override public String toString() { return name; }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   375
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   376
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   377
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   378
    static final Object[][][] ACCESS_CASES = {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   379
        { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   380
        { { false, PUBLIC }, { false, PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[1]: only PRIVATE
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   381
        { { false, PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[2]: PUBLIC false
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   382
        { { true,  PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[3]: all true
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   383
    };
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   384
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   385
    static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   386
        Object[][] cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   387
        if (name.contains("pri_") || isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   388
            cases = ACCESS_CASES[1]; // PRIVATE only
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   389
        } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   390
            cases = ACCESS_CASES[2]; // not PUBLIC
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   391
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   392
            assertTrue(name.indexOf('_') < 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   393
            boolean pubc = Modifier.isPublic(defc.getModifiers());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   394
            if (pubc)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   395
                cases = ACCESS_CASES[3]; // all access levels
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   396
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   397
                cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   398
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   399
        if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   400
            cases = Arrays.copyOfRange(cases, 0, cases.length-1);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   401
        return cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   402
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   403
    static Object[][] accessCases(Class<?> defc, String name) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   404
        return accessCases(defc, name, false);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   405
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   406
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   407
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   408
    public void testFindStatic() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   409
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   410
        startTest("findStatic");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   411
        testFindStatic(PubExample.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   412
        testFindStatic(Example.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   413
        testFindStatic(Example.class, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   414
        testFindStatic(Example.class, void.class, "pri_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   415
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   416
        testFindStatic(Example.class, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   417
        testFindStatic(Example.class, Object.class, "s2", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   418
        testFindStatic(Example.class, Object.class, "s3", long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   419
        testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   420
        testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   421
        testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   422
        testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   423
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   424
        testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   425
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   426
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   427
    void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   428
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   429
            testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   430
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   431
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   432
    void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   433
        testFindStatic(true, lookup, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   434
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   435
    void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   436
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   437
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   438
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   439
        RuntimeException noAccess = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   440
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   441
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   442
            target = lookup.findStatic(defc, name, type);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   443
        } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   444
            noAccess = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   445
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   446
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   447
            System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   448
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   449
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   450
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   451
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   452
        assertEquals(type, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   453
        assertTrue(target.toString().contains(name));  // rough check
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   454
        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   455
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   456
        printCalled(target, name, args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   457
        target.invokeVarargs(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   458
        assertCalled(name, args);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   459
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   460
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   461
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   462
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   463
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   464
    public void testFindVirtual() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   465
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   466
        startTest("findVirtual");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   467
        testFindVirtual(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   468
        testFindVirtual(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   469
        testFindVirtual(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   470
        testFindVirtual(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   471
        testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   472
        testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   473
        testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   474
        testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   475
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   476
        // test dispatch
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   477
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   478
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   479
        testFindVirtual(SubExample.class,      IntExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   480
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   481
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   482
        testFindVirtual(Example.class,         IntExample.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   483
        testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   484
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   485
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   486
    void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   487
        Class<?> rcvc = defc;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   488
        testFindVirtual(rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   489
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   490
    void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   491
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   492
            testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   493
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   494
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   495
    void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   496
        testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   497
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   498
    void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   499
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   500
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   501
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   502
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   503
        RuntimeException noAccess = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   504
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   505
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   506
            target = lookup.findVirtual(defc, methodName, type);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   507
        } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   508
            noAccess = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   509
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   510
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   511
            System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   512
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   513
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   514
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   515
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   516
        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   517
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   518
        if (defc.getClassLoader() != null)  // detune due to 6939196
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   519
            assertEquals(typeWithSelf.dropParameterTypes(0,1),
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   520
                         target.type().dropParameterTypes(0,1));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   521
        else // FIXME: use only this test when 6939196 is fixed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   522
        assertEquals(typeWithSelf, target.type());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   523
        assertTrue(target.toString().contains(methodName));  // rough check
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   524
        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   525
        Object[] argsWithSelf = randomArgs(paramsWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   526
        if (rcvc != defc)  argsWithSelf[0] = randomArg(rcvc);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   527
        printCalled(target, name, argsWithSelf);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   528
        target.invokeVarargs(argsWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   529
        assertCalled(name, argsWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   530
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   531
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   532
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   533
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   534
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   535
    public void testFindSpecial() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   536
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   537
        startTest("findSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   538
        testFindSpecial(SubExample.class, Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   539
        testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   540
        // Do some negative testing:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   541
        for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   542
            testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   543
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   544
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   545
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "bogus");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   546
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   547
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   548
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   549
    void testFindSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   550
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   551
        testFindSpecial(true,  EXAMPLE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   552
        testFindSpecial(true,  PRIVATE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   553
        testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   554
        testFindSpecial(false, PUBLIC,  specialCaller, defc, ret, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   555
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   556
    void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   557
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   558
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   559
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   560
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   561
        RuntimeException noAccess = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   562
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   563
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   564
            target = lookup.findSpecial(defc, name, type, specialCaller);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   565
        } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   566
            noAccess = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   567
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   568
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   569
            System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   570
                               +(target == null ? "" : target.type())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   571
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   572
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   573
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   574
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   575
        assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   576
        assertEquals(type,          target.type().dropParameterTypes(0,1));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   577
        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   578
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   579
        assertTrue(target.toString().contains(name));  // rough check
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   580
        if (!DO_MORE_CALLS && lookup != PRIVATE && lookup != EXAMPLE)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   581
        Object[] args = randomArgs(paramsWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   582
        printCalled(target, name, args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   583
        target.invokeVarargs(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   584
        assertCalled(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   585
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   586
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   587
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   588
    public void testBind() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   589
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   590
        startTest("bind");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   591
        testBind(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   592
        testBind(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   593
        testBind(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   594
        testBind(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   595
        testBind(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   596
        testBind(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   597
        testBind(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   598
        testBind(Example.class, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   599
        testBind(false, PRIVATE, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   600
        testBind(SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   601
        testBind(SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   602
        testBind(IntExample.Impl.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   603
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   604
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   605
    void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   606
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   607
            testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   608
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   609
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   610
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   611
    void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   612
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   613
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   614
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   615
        Object receiver = randomArg(defc);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   616
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   617
        RuntimeException noAccess = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   618
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   619
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   620
            target = lookup.bind(receiver, methodName, type);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   621
        } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   622
            noAccess = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   623
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   624
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   625
            System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   626
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   627
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   628
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   629
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   630
        assertEquals(type, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   631
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   632
        printCalled(target, name, args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   633
        target.invokeVarargs(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   634
        Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   635
        assertCalled(name, argsWithReceiver);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   636
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   637
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   638
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   639
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   640
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   641
    public void testUnreflect() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   642
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   643
        startTest("unreflect");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   644
        testUnreflect(Example.class, true, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   645
        testUnreflect(Example.class, true, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   646
        testUnreflect(Example.class, true, void.class, "pri_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   647
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   648
        testUnreflect(Example.class, true, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   649
        testUnreflect(Example.class, true, Object.class, "s2", int.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   650
        testUnreflect(Example.class, true, Object.class, "s3", long.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   651
        testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   652
        testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   653
        testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   654
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   655
        testUnreflect(Example.class, false, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   656
        testUnreflect(Example.class, false, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   657
        testUnreflect(Example.class, false, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   658
        testUnreflect(Example.class, false, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   659
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   660
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   661
        testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   662
        testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   663
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   664
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   665
    void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   666
        for (Object[] ac : accessCases(defc, name)) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   667
            testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   668
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   669
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   670
    void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   671
        for (Object[] ac : accessCases(defc, name)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   672
            testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   673
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   674
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   675
    void testUnreflectMaybeSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   676
                                   boolean positive, Lookup lookup,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   677
                                   Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   678
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   679
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   680
        Method rmethod = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   681
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   682
        RuntimeException noAccess = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   683
        try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   684
            rmethod = defc.getDeclaredMethod(name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   685
        } catch (NoSuchMethodException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   686
            throw new NoAccessException(ex);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   687
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   688
        boolean isStatic = (rcvc == null);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   689
        boolean isSpecial = (specialCaller != null);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   690
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   691
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   692
            if (isSpecial)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   693
                target = lookup.unreflectSpecial(rmethod, specialCaller);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   694
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   695
                target = lookup.unreflect(rmethod);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   696
        } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   697
            noAccess = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   698
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   699
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   700
            System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   701
                               +(!isSpecial ? "" : " specialCaller="+specialCaller)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   702
                               +( isStatic  ? "" : " receiver="+rcvc)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   703
                               +" => "+target
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   704
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   705
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   706
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   707
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   708
        assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   709
        Class<?>[] paramsMaybeWithSelf = params;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   710
        if (!isStatic) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   711
            paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   712
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   713
        MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   714
        if (isStatic) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   715
            assertEquals(typeMaybeWithSelf, target.type());
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   716
        } else {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   717
            if (defc.getClassLoader() != null)  // detune due to 6939196
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   718
                assertEquals(typeMaybeWithSelf.dropParameterTypes(0,1),
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   719
                             target.type().dropParameterTypes(0,1));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   720
            else // FIXME: use only this test when 6939196 is fixed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   721
            if (isSpecial)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   722
                assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   723
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   724
                assertEquals(defc, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   725
            assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   726
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   727
        Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   728
        printCalled(target, name, argsMaybeWithSelf);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   729
        target.invokeVarargs(argsMaybeWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   730
        assertCalled(name, argsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   731
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   732
            System.out.print(':');
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   733
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   734
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   735
    void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   736
        for (Object[] ac : accessCases(defc, name, true)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   737
            Class<?> specialCaller = rcvc;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   738
            testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   739
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   740
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   741
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   742
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   743
    public void testUnreflectSpecial() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   744
        if (CAN_SKIP_WORKING)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   745
        startTest("unreflectSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   746
        testUnreflectSpecial(Example.class,    Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   747
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   748
        testUnreflectSpecial(Example.class,    Example.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   749
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   750
        testUnreflectSpecial(Example.class,    Example.class, Object.class, "v2", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   751
        testUnreflectSpecial(Example.class, SubExample.class, Object.class, "v2", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   752
        testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   753
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   754
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   755
    public static class HasFields {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   756
        boolean fZ = false;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   757
        byte fB = (byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   758
        short fS = (short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   759
        char fC = 'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   760
        int fI = 'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   761
        long fJ = 'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   762
        float fF = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   763
        double fD = 'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   764
        static boolean sZ = true;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   765
        static byte sB = 1+(byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   766
        static short sS = 1+(short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   767
        static char sC = 1+'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   768
        static int sI = 1+'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   769
        static long sJ = 1+'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   770
        static float sF = 1+'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   771
        static double sD = 1+'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   772
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   773
        Object fL = 'L';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   774
        String fR = "R";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   775
        static Object sL = 'M';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   776
        static String sR = "S";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   777
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   778
        static final Object[][] CASES;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   779
        static {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   780
            ArrayList<Object[]> cases = new ArrayList<Object[]>();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   781
            Object types[][] = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   782
                {'L',Object.class}, {'R',String.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   783
                {'I',int.class}, {'J',long.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   784
                {'F',float.class}, {'D',double.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   785
                {'Z',boolean.class}, {'B',byte.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   786
                {'S',short.class}, {'C',char.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   787
            };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   788
            HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   789
            for (Object[] t : types) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   790
                for (int kind = 0; kind <= 1; kind++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   791
                    boolean isStatic = (kind != 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   792
                    char btc = (Character)t[0];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   793
                    String name = (isStatic ? "s" : "f") + btc;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   794
                    Class<?> type = (Class<?>) t[1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   795
                    Object value;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   796
                    Field field;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   797
                    try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   798
                        field = HasFields.class.getDeclaredField(name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   799
                    } catch (Exception ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   800
                        throw new InternalError("no field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   801
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   802
                    try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   803
                        value = field.get(fields);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   804
                    } catch (Exception ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   805
                        throw new InternalError("cannot fetch field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   806
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   807
                    if (type == float.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   808
                        float v = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   809
                        if (isStatic)  v++;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   810
                        assert(value.equals(v));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   811
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   812
                    assert(name.equals(field.getName()));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   813
                    assert(type.equals(field.getType()));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   814
                    assert(isStatic == (Modifier.isStatic(field.getModifiers())));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   815
                    cases.add(new Object[]{ field, value });
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   816
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   817
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   818
            CASES = cases.toArray(new Object[0][]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   819
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   820
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   821
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   822
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   823
    public void testUnreflectGetter() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   824
        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   825
        startTest("unreflectGetter");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   826
        for (Object[] c : HasFields.CASES) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   827
            Field f = (Field)c[0];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   828
            Object value = c[1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   829
            Class<?> type = f.getType();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   830
            if (type.isPrimitive() && type != int.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   831
                continue;  //FIXME
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   832
            testUnreflectGetter(lookup, f, type, value);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   833
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   834
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   835
    public void testUnreflectGetter(MethodHandles.Lookup lookup,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   836
            Field f, Class<?> type, Object value) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   837
        countTest(true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   838
        boolean isStatic = Modifier.isStatic(f.getModifiers());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   839
        MethodType expType = MethodType.methodType(type, HasFields.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   840
        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   841
        MethodHandle mh = lookup.unreflectGetter(f);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   842
        assertSame(mh.type(), expType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   843
        assertEquals(mh.toString(), f.getName());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   844
        HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   845
        Object sawValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   846
        Class<?> rtype = type;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   847
        if (type != int.class)  rtype = Object.class;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   848
        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(rtype));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   849
        Object expValue = value;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   850
        for (int i = 0; i <= 1; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   851
            if (isStatic) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   852
                if (type == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   853
                    sawValue = mh.<int>invokeExact();  // do these exactly
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   854
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   855
                    sawValue = mh.invokeExact();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   856
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   857
                if (type == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   858
                    sawValue = mh.<int>invokeExact((Object) fields);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   859
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   860
                    sawValue = mh.invokeExact((Object) fields);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   861
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   862
            assertEquals(sawValue, expValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   863
            Object random = randomArg(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   864
            f.set(fields, random);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   865
            expValue = random;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   866
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   867
        f.set(fields, value);  // put it back
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   868
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   869
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   870
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   871
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   872
    public void testUnreflectSetter() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   873
        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   874
        startTest("unreflectSetter");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   875
        for (Object[] c : HasFields.CASES) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   876
            Field f = (Field)c[0];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   877
            Object value = c[1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   878
            Class<?> type = f.getType();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   879
            if (type.isPrimitive() && type != int.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   880
                continue;  //FIXME
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   881
            testUnreflectSetter(lookup, f, type, value);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   882
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   883
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   884
    public void testUnreflectSetter(MethodHandles.Lookup lookup,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   885
            Field f, Class<?> type, Object value) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   886
        countTest(true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   887
        boolean isStatic = Modifier.isStatic(f.getModifiers());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   888
        MethodType expType = MethodType.methodType(void.class, HasFields.class, type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   889
        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   890
        MethodHandle mh = lookup.unreflectSetter(f);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   891
        assertSame(mh.type(), expType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   892
        assertEquals(mh.toString(), f.getName());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   893
        HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   894
        Object sawValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   895
        Class<?> vtype = type;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   896
        if (type != int.class)  vtype = Object.class;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   897
        int last = mh.type().parameterCount() - 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   898
        mh = MethodHandles.convertArguments(mh, mh.type().generic().changeReturnType(void.class).changeParameterType(last, vtype));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   899
        assertEquals(f.get(fields), value);  // clean to start with
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   900
        for (int i = 0; i <= 1; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   901
            Object putValue = randomArg(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   902
            if (isStatic) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   903
                if (type == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   904
                    mh.<void>invokeExact((int)(Integer)putValue);  // do these exactly
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   905
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   906
                    mh.<void>invokeExact(putValue);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   907
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   908
                if (type == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   909
                    mh.<void>invokeExact((Object) fields, (int)(Integer)putValue);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   910
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   911
                    mh.<void>invokeExact((Object) fields, putValue);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   912
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   913
            assertEquals(f.get(fields), putValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   914
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   915
        f.set(fields, value);  // put it back
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   916
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   917
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   918
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   919
    public void testArrayElementGetter() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   920
        startTest("arrayElementGetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   921
        testArrayElementGetterSetter(false);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   922
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   923
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   924
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   925
    public void testArrayElementSetter() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   926
        startTest("arrayElementSetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   927
        testArrayElementGetterSetter(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   928
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   929
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   930
    public void testArrayElementGetterSetter(boolean testSetter) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   931
        testArrayElementGetterSetter(new Object[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   932
        testArrayElementGetterSetter(new String[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   933
        testArrayElementGetterSetter(new boolean[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   934
        testArrayElementGetterSetter(new byte[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   935
        testArrayElementGetterSetter(new char[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   936
        testArrayElementGetterSetter(new short[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   937
        testArrayElementGetterSetter(new int[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   938
        testArrayElementGetterSetter(new float[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   939
        testArrayElementGetterSetter(new long[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   940
        testArrayElementGetterSetter(new double[10], testSetter);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   941
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   942
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   943
    public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   944
        countTest(true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   945
        if (verbosity >= 2)  System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   946
        Class<?> arrayType = array.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   947
        Class<?> elemType = arrayType.getComponentType();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   948
        MethodType expType = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   949
                ? MethodType.methodType(elemType,   arrayType, int.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   950
                : MethodType.methodType(void.class, arrayType, int.class, elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   951
        MethodHandle mh = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   952
                ? MethodHandles.arrayElementGetter(arrayType)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   953
                : MethodHandles.arrayElementSetter(arrayType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   954
        assertSame(mh.type(), expType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   955
        if (elemType != int.class && elemType != boolean.class) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   956
            MethodType gtype;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   957
            if (true) { // FIXME: remove this path (and remove <void> below in the mh.invokes)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   958
                gtype = mh.type().changeParameterType(0, Object.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   959
                if (testSetter)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   960
                    gtype = gtype.changeParameterType(2, Object.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   961
                else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   962
                    gtype = gtype.changeReturnType(Object.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   963
            } else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   964
                // FIXME: This simpler path hits a bug in convertArguments => ToGeneric
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   965
                gtype = mh.type().generic().changeParameterType(1, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   966
            mh = MethodHandles.convertArguments(mh, gtype);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   967
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   968
        Object sawValue, expValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   969
        List<Object> model = array2list(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   970
        int length = Array.getLength(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   971
        for (int i = 0; i < length; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   972
            // update array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   973
            Object random = randomArg(elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   974
            model.set(i, random);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   975
            if (testSetter) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   976
                if (elemType == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   977
                    mh.<void>invokeExact((int[]) array, i, (int)(Integer)random);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   978
                else if (elemType == boolean.class)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   979
                    mh.<void>invokeExact((boolean[]) array, i, (boolean)(Boolean)random);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   980
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   981
                    mh.<void>invokeExact(array, i, random);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   982
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   983
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   984
                Array.set(array, i, random);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   985
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   986
            if (verbosity >= 5) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   987
                List<Object> array2list = array2list(array);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   988
                System.out.println("a["+i+"]="+random+" => "+array2list);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   989
                if (!array2list.equals(model))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   990
                    System.out.println("***   != "+model);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   991
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   992
            // observe array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   993
            sawValue = Array.get(array, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   994
            if (!testSetter) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   995
                expValue = sawValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   996
                if (elemType == int.class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   997
                    sawValue = mh.<int>invokeExact((int[]) array, i);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   998
                else if (elemType == boolean.class)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   999
                    sawValue = mh.<boolean>invokeExact((boolean[]) array, i);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1000
                else
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1001
                    sawValue = mh.invokeExact(array, i);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1002
                assertEquals(sawValue, expValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1003
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1004
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1005
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1006
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1007
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1008
    List<Object> array2list(Object array) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1009
        int length = Array.getLength(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1010
        ArrayList<Object> model = new ArrayList<Object>(length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1011
        for (int i = 0; i < length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1012
            model.add(Array.get(array, i));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1013
        return model;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1014
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1015
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1016
    static class Callee {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1017
        static Object id() { return called("id"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1018
        static Object id(Object x) { return called("id", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1019
        static Object id(Object x, Object y) { return called("id", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1020
        static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1021
        static Object id(Object... vx) { return called("id", vx); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1022
        static MethodHandle ofType(int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1023
            return ofType(Object.class, n);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1024
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1025
        static MethodHandle ofType(Class<?> rtype, int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1026
            if (n == -1)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1027
                return ofType(MethodType.methodType(rtype, Object[].class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1028
            return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1029
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1030
        static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1031
            return ofType(MethodType.methodType(rtype, ptypes));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1032
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1033
        static MethodHandle ofType(MethodType type) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1034
            Class<?> rtype = type.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1035
            String pfx = "";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1036
            if (rtype != Object.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1037
                pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1038
            String name = pfx+"id";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1039
            return PRIVATE.findStatic(Callee.class, name, type);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1040
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1041
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1042
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1043
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1044
    public void testConvertArguments() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1045
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1046
        startTest("convertArguments");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1047
        testConvert(Callee.ofType(1), null, "id", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1048
        testConvert(Callee.ofType(1), null, "id", String.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1049
        testConvert(Callee.ofType(1), null, "id", Integer.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1050
        testConvert(Callee.ofType(1), null, "id", short.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1051
        testConvert(Callee.ofType(1), null, "id", char.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1052
        testConvert(Callee.ofType(1), null, "id", byte.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1053
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1054
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1055
    void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1056
        testConvert(true, id, rtype, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1057
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1058
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1059
    void testConvert(boolean positive, MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1060
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1061
        MethodType idType = id.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1062
        if (rtype == null)  rtype = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1063
        for (int i = 0; i < params.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1064
            if (params[i] == null)  params[i] = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1065
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1066
        // simulate the pairwise conversion
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1067
        MethodType newType = MethodType.methodType(rtype, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1068
        Object[] args = randomArgs(newType.parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1069
        Object[] convArgs = args.clone();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1070
        for (int i = 0; i < args.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1071
            Class<?> src = newType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1072
            Class<?> dst = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1073
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1074
                convArgs[i] = castToWrapper(convArgs[i], dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1075
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1076
        Object convResult = id.invokeVarargs(convArgs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1077
        {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1078
            Class<?> dst = newType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1079
            Class<?> src = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1080
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1081
                convResult = castToWrapper(convResult, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1082
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1083
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1084
        RuntimeException error = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1085
        try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1086
            target = MethodHandles.convertArguments(id, newType);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1087
        } catch (RuntimeException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1088
            error = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1089
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1090
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1091
            System.out.println("convert "+id+ " to "+newType+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1092
                    +(error == null ? "" : " !! "+error));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1093
        if (positive && error != null)  throw error;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1094
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1095
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1096
        assertEquals(newType, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1097
        printCalled(target, id.toString(), args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1098
        Object result = target.invokeVarargs(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1099
        assertCalled(name, convArgs);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1100
        assertEquals(convResult, result);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1101
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1102
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1103
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1104
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1105
    @Test
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1106
    public void testPermuteArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1107
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1108
        startTest("permuteArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1109
        testPermuteArguments(4, Integer.class,  2, String.class,  0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1110
        //testPermuteArguments(6, Integer.class,  0, null,         30);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1111
        //testPermuteArguments(4, Integer.class,  1, int.class,     6);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1112
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1113
    public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1114
        if (verbosity >= 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1115
            System.out.println("permuteArguments "+max+"*"+type1.getName()
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1116
                    +(t2c==0?"":"/"+t2c+"*"+type2.getName())
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1117
                    +(dilution > 0 ? " with dilution "+dilution : ""));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1118
        int t2pos = t2c == 0 ? 0 : 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1119
        for (int inargs = t2pos+1; inargs <= max; inargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1120
            Class<?>[] types = new Class<?>[inargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1121
            Arrays.fill(types, type1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1122
            if (t2c != 0) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1123
                // Fill in a middle range with type2:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1124
                Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1125
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1126
            Object[] args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1127
            int numcases = 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1128
            for (int outargs = 0; outargs <= max; outargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1129
                if (outargs - inargs >= MAX_ARG_INCREASE)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1130
                int[] reorder = new int[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1131
                int casStep = dilution + 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1132
                // Avoid some common factors:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1133
                while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1134
                       (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1135
                    casStep++;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1136
                for (int cas = 0; cas < numcases; cas += casStep) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1137
                    for (int i = 0, c = cas; i < outargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1138
                        reorder[i] = c % inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1139
                        c /= inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1140
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1141
                    testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1142
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1143
                numcases *= inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1144
                if (dilution > 10 && outargs >= 4) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1145
                    // Do some special patterns, which we probably missed.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1146
                    // Replication of a single argument or argument pair.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1147
                    for (int i = 0; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1148
                        Arrays.fill(reorder, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1149
                        testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1150
                        for (int d = 1; d <= 2; d++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1151
                            if (i + d >= inargs)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1152
                            for (int j = 1; j < outargs; j += 2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1153
                                reorder[j] += 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1154
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1155
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1156
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1157
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1158
                    // Repetition of a sequence of 3 or more arguments.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1159
                    for (int i = 1; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1160
                        for (int len = 3; len <= inargs; len++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1161
                            for (int j = 0; j < outargs; j++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1162
                                reorder[j] = (i + (j % len)) % inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1163
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1164
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1165
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1166
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1167
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1168
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1169
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1170
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1171
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1172
    static int[] reverse(int[] reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1173
        reorder = reorder.clone();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1174
        for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1175
            int j = reorder.length - 1 - i;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1176
            int tem = reorder[i];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1177
            reorder[i] = reorder[j];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1178
            reorder[j] = tem;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1179
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1180
        return reorder;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1181
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1182
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1183
    void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1184
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1185
        if (args == null && types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1186
            int max = 0;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1187
            for (int j : reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1188
                if (max < j)  max = j;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1189
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1190
            args = randomArgs(max+1, Integer.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1191
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1192
        if (args == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1193
            args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1194
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1195
        if (types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1196
            types = new Class<?>[args.length];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1197
            for (int i = 0; i < args.length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1198
                types[i] = args[i].getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1199
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1200
        int inargs = args.length, outargs = reorder.length;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1201
        assert(inargs == types.length);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1202
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1203
            System.out.println("permuteArguments "+Arrays.toString(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1204
        Object[] permArgs = new Object[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1205
        Class<?>[] permTypes = new Class<?>[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1206
        for (int i = 0; i < outargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1207
            permArgs[i] = args[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1208
            permTypes[i] = types[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1209
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1210
        if (verbosity >= 4) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1211
            System.out.println("in args:   "+Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1212
            System.out.println("out args:  "+Arrays.asList(permArgs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1213
            System.out.println("in types:  "+Arrays.asList(types));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1214
            System.out.println("out types: "+Arrays.asList(permTypes));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1215
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1216
        MethodType inType  = MethodType.methodType(Object.class, types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1217
        MethodType outType = MethodType.methodType(Object.class, permTypes);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1218
        MethodHandle target = MethodHandles.convertArguments(ValueConversions.varargsList(outargs), outType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1219
        MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1220
        Object result = newTarget.invokeVarargs(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1221
        Object expected = Arrays.asList(permArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1222
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1223
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1224
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1225
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1226
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1227
    public void testSpreadArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1228
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1229
        startTest("spreadArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1230
        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1231
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1232
                System.out.println("spreadArguments "+argType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1233
            // FIXME: enable _adapter_spread_args and fix Fail_2
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1234
            for (int nargs = 0; nargs < 10; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1235
                if (argType == int.class && nargs >= 6)  continue; // FIXME Fail_1
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1236
                for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1237
                    if (argType == int.class && pos > 0)  continue; // FIXME Fail_3
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1238
                     testSpreadArguments(argType, pos, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1239
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1240
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1241
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1242
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1243
    public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1244
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1245
        MethodHandle target = ValueConversions.varargsArray(nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1246
        MethodHandle target2 = changeArgTypes(target, argType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1247
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1248
            System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1249
        Object[] args = randomArgs(target2.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1250
        // make sure the target does what we think it does:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1251
        if (pos == 0 && nargs < 5) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1252
            Object[] check = (Object[]) target.invokeVarargs(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1253
            assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1254
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1255
                case 0:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1256
                    check = target.<Object[]>invokeExact();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1257
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1258
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1259
                case 1:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1260
                    check = target.<Object[]>invokeExact(args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1261
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1262
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1263
                case 2:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1264
                    check = target.<Object[]>invokeExact(args[0], args[1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1265
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1266
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1267
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1268
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1269
        List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1270
        {   // modify newParams in place
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1271
            List<Class<?>> spreadParams = newParams.subList(pos, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1272
            spreadParams.clear(); spreadParams.add(Object[].class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1273
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1274
        MethodType newType = MethodType.methodType(Object.class, newParams);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1275
        MethodHandle result = MethodHandles.spreadArguments(target2, newType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1276
        Object[] returnValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1277
        if (pos == 0) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1278
            returnValue = (Object[]) result.invokeExact(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1279
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1280
            Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1281
            args1[pos] = Arrays.copyOfRange(args, pos, args.length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1282
            returnValue = (Object[]) result.invokeVarargs(args1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1283
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1284
        assertArrayEquals(args, returnValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1285
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1286
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1287
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1288
    public void testCollectArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1289
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1290
        startTest("collectArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1291
        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1292
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1293
                System.out.println("collectArguments "+argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1294
            for (int nargs = 0; nargs < 10; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1295
                for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1296
                    if (argType == int.class)  continue; // FIXME Fail_4
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1297
                    testCollectArguments(argType, pos, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1298
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1299
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1300
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1301
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1302
    public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1303
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1304
        // fake up a MH with the same type as the desired adapter:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1305
        MethodHandle fake = ValueConversions.varargsArray(nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1306
        fake = changeArgTypes(fake, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1307
        MethodType newType = fake.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1308
        Object[] args = randomArgs(newType.parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1309
        // here is what should happen:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1310
        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1311
        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1312
        // here is the MH which will witness the collected argument tail:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1313
        MethodHandle target = ValueConversions.varargsArray(pos+1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1314
        target = changeArgTypes(target, 0, pos, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1315
        target = changeArgTypes(target, pos, pos+1, Object[].class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1316
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1317
            System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1318
        MethodHandle result = MethodHandles.collectArguments(target, newType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1319
        Object[] returnValue = (Object[]) result.invokeVarargs(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1320
//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1321
//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1322
//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1323
        assertArrayEquals(collectedArgs, returnValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1324
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1325
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1326
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1327
    public void testInsertArguments() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1328
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1329
        startTest("insertArguments");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1330
        for (int nargs = 0; nargs <= 4; nargs++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1331
            for (int ins = 0; ins <= 4; ins++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1332
                if (ins > MAX_ARG_INCREASE)  continue;  // FIXME Fail_6
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1333
                for (int pos = 0; pos <= nargs; pos++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1334
                    testInsertArguments(nargs, pos, ins);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1335
                }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1336
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1337
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1338
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1339
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1340
    void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1341
        countTest();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1342
        MethodHandle target = ValueConversions.varargsArray(nargs + ins);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1343
        Object[] args = randomArgs(target.type().parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1344
        List<Object> resList = Arrays.asList(args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1345
        List<Object> argsToPass = new ArrayList<Object>(resList);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1346
        List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1347
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1348
            System.out.println("insert: "+argsToInsert+" into "+target);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1349
        MethodHandle target2 = MethodHandles.insertArguments(target, pos,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1350
                (Object[]) argsToInsert.toArray());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1351
        argsToInsert.clear();  // remove from argsToInsert
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1352
        Object res2 = target2.invokeVarargs(argsToPass);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1353
        Object res2List = Arrays.asList((Object[])res2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1354
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1355
            System.out.println("result: "+res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1356
        //if (!resList.equals(res2List))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1357
        //    System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1358
        assertEquals(resList, res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1359
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1360
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1361
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1362
    public void testFilterArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1363
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1364
        startTest("filterArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1365
        for (int nargs = 1; nargs <= 6; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1366
            for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1367
                testFilterArguments(nargs, pos);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1368
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1369
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1370
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1371
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1372
    void testFilterArguments(int nargs, int pos) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1373
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1374
        MethodHandle target = ValueConversions.varargsList(nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1375
        MethodHandle filter = ValueConversions.varargsList(1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1376
        filter = MethodHandles.convertArguments(filter, filter.type().generic());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1377
        Object[] argsToPass = randomArgs(nargs, Object.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1378
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1379
            System.out.println("filter "+target+" at "+pos+" with "+filter);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1380
        MethodHandle[] filters = new MethodHandle[pos*2+1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1381
        filters[pos] = filter;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1382
        MethodHandle target2 = MethodHandles.filterArguments(target, filters);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1383
        // Simulate expected effect of filter on arglist:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1384
        Object[] filteredArgs = argsToPass.clone();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1385
        filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1386
        List<Object> expected = Arrays.asList(filteredArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1387
        Object result = target2.invokeVarargs(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1388
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1389
            System.out.println("result: "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1390
        if (!expected.equals(result))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1391
            System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+argsToPass+" => "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1392
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1393
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1394
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1395
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1396
    public void testFoldArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1397
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1398
        startTest("foldArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1399
        for (int nargs = 0; nargs <= 4; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1400
            for (int fold = 0; fold <= nargs; fold++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1401
                for (int pos = 0; pos <= nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1402
                    testFoldArguments(nargs, pos, fold);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1403
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1404
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1405
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1406
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1407
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1408
    void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1409
        if (pos != 0)  return;  // can fold only at pos=0 for now
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1410
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1411
        MethodHandle target = ValueConversions.varargsList(1 + nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1412
        MethodHandle combine = ValueConversions.varargsList(fold);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1413
        List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1414
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1415
            System.out.println("fold "+target+" with "+combine);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1416
        MethodHandle target2 = MethodHandles.foldArguments(target, combine);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1417
        // Simulate expected effect of combiner on arglist:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1418
        List<Object> expected = new ArrayList<Object>(argsToPass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1419
        List<Object> argsToFold = expected.subList(pos, pos + fold);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1420
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1421
            System.out.println("fold: "+argsToFold+" into "+target2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1422
        Object foldedArgs = combine.invokeVarargs(argsToFold);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1423
        argsToFold.add(0, foldedArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1424
        Object result = target2.invokeVarargs(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1425
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1426
            System.out.println("result: "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1427
        if (!expected.equals(result))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1428
            System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1429
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1430
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1431
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1432
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1433
    public void testDropArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1434
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1435
        startTest("dropArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1436
        for (int nargs = 0; nargs <= 4; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1437
            for (int drop = 1; drop <= 4; drop++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1438
                for (int pos = 0; pos <= nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1439
                    testDropArguments(nargs, pos, drop);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1440
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1441
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1442
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1443
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1444
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1445
    void testDropArguments(int nargs, int pos, int drop) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1446
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1447
        MethodHandle target = ValueConversions.varargsArray(nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1448
        Object[] args = randomArgs(target.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1449
        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1450
                Collections.nCopies(drop, Object.class).toArray(new Class[0]));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1451
        List<Object> resList = Arrays.asList(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1452
        List<Object> argsToDrop = new ArrayList<Object>(resList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1453
        for (int i = drop; i > 0; i--) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1454
            argsToDrop.add(pos, "blort#"+i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1455
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1456
        Object res2 = target2.invokeVarargs(argsToDrop);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1457
        Object res2List = Arrays.asList((Object[])res2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1458
        //if (!resList.equals(res2List))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1459
        //    System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1460
        assertEquals(resList, res2List);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1461
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1462
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1463
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1464
    public void testInvokers() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1465
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1466
        startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1467
        // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1468
        Set<MethodType> done = new HashSet<MethodType>();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1469
        for (int i = 0; i <= 6; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1470
            MethodType gtype = MethodType.genericMethodType(i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1471
            for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1472
                for (int j = -1; j < i; j++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1473
                    MethodType type = gtype;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1474
                    if (j < 0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1475
                        type = type.changeReturnType(argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1476
                    else if (argType == void.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1477
                        continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1478
                    else
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1479
                        type = type.changeParameterType(j, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1480
                    if (argType.isPrimitive() && j != i-1)  continue; // FIXME Fail_5
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1481
                    if (done.add(type))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1482
                        testInvokers(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1483
                    MethodType vtype = type.changeReturnType(void.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1484
                    if (done.add(vtype))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1485
                        testInvokers(vtype);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1486
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1487
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1488
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1489
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1490
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1491
    public void testInvokers(MethodType type) throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1492
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1493
            System.out.println("test invokers for "+type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1494
        int nargs = type.parameterCount();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1495
        boolean testRetCode = type.returnType() != void.class;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1496
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1497
                                MethodType.genericMethodType(0, true));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1498
        target = MethodHandles.collectArguments(target, type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1499
        Object[] args = randomArgs(type.parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1500
        List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1501
        targetPlusArgs.add(0, target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1502
        int code = (Integer) invokee(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1503
        Object log = logEntry("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1504
        assertEquals(log.hashCode(), code);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1505
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1506
        MethodHandle inv;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1507
        Object result;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1508
        // exact invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1509
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1510
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1511
        inv = MethodHandles.exactInvoker(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1512
        result = inv.invokeVarargs(targetPlusArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1513
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1514
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1515
        // generic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1516
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1517
        inv = MethodHandles.genericInvoker(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1518
        if (nargs <= 3) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1519
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1520
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1521
            case 0:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1522
                result = inv.invokeExact(target);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1523
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1524
            case 1:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1525
                result = inv.invokeExact(target, args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1526
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1527
            case 2:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1528
                result = inv.invokeExact(target, args[0], args[1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1529
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1530
            case 3:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1531
                result = inv.invokeExact(target, args[0], args[1], args[2]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1532
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1533
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1534
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1535
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1536
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1537
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1538
        result = inv.invokeVarargs(targetPlusArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1539
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1540
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1541
        // varargs invoker #0
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1542
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1543
        inv = MethodHandles.varargsInvoker(type, 0);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1544
        result = inv.invokeExact(target, args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1545
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1546
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1547
        if (nargs >= 1) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1548
            // varargs invoker #1
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1549
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1550
            inv = MethodHandles.varargsInvoker(type, 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1551
            result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1552
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1553
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1554
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1555
        if (nargs >= 2) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1556
            // varargs invoker #2
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1557
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1558
            inv = MethodHandles.varargsInvoker(type, 2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1559
            result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1560
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1561
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1562
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1563
        if (nargs >= 3) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1564
            // varargs invoker #3
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1565
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1566
            inv = MethodHandles.varargsInvoker(type, 3);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1567
            result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1568
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1569
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1570
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1571
        for (int k = 0; k <= nargs; k++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1572
            // varargs invoker #0..N
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1573
            countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1574
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1575
            inv = MethodHandles.varargsInvoker(type, k);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1576
            List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1577
            List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1578
            Object[] tail = tailList.toArray();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1579
            tailList.clear(); tailList.add(tail);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1580
            result = inv.invokeVarargs(targetPlusVarArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1581
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1582
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1583
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1584
        // dynamic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1585
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1586
        CallSite site = new CallSite(MethodHandlesTest.class, "foo", type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1587
        inv = MethodHandles.dynamicInvoker(site);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1588
        site.setTarget(target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1589
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1590
        result = inv.invokeVarargs(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1591
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1592
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1593
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1594
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1595
    static Object invokee(Object... args) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1596
        return called("invokee", args).hashCode();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1597
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1598
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1599
    private static final String MISSING_ARG = "missingArg";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1600
    static Object targetIfEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1601
        return called("targetIfEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1602
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1603
    static Object fallbackIfNotEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1604
        return called("fallbackIfNotEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1605
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1606
    static Object targetIfEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1607
        assertEquals(x, MISSING_ARG);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1608
        return called("targetIfEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1609
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1610
    static Object fallbackIfNotEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1611
        assertFalse(x.toString(), x.equals(MISSING_ARG));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1612
        return called("fallbackIfNotEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1613
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1614
    static Object targetIfEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1615
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1616
        return called("targetIfEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1617
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1618
    static Object fallbackIfNotEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1619
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1620
        return called("fallbackIfNotEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1621
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1622
    static Object targetIfEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1623
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1624
        return called("targetIfEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1625
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1626
    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1627
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1628
        return called("fallbackIfNotEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1629
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1630
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1631
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1632
    public void testGuardWithTest() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1633
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1634
        startTest("guardWithTest");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1635
        for (int nargs = 0; nargs <= 3; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1636
            if (nargs != 2)  continue;  // FIXME: test more later
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1637
            testGuardWithTest(nargs, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1638
            testGuardWithTest(nargs, String.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1639
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1640
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1641
    void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1642
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1643
        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1644
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1645
        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1646
        while (test.type().parameterCount() < nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1647
            test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1648
        while (test.type().parameterCount() > nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1649
            test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1650
        if (argClass != Object.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1651
            test = changeArgTypes(test, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1652
            target = changeArgTypes(target, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1653
            fallback = changeArgTypes(fallback, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1654
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1655
        MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1656
        assertEquals(target.type(), mh.type());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1657
        Object[][] argLists = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1658
            { },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1659
            { "foo" }, { MISSING_ARG },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1660
            { "foo", "foo" }, { "foo", "bar" },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1661
            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1662
        };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1663
        for (Object[] argList : argLists) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1664
            if (argList.length != nargs)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1665
            boolean equals;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1666
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1667
            case 0:   equals = true; break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1668
            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1669
            default:  equals = argList[0].equals(argList[1]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1670
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1671
            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1672
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1673
                System.out.println(logEntry(willCall, argList));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1674
            Object result = mh.invokeVarargs(argList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1675
            assertCalled(willCall, argList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1676
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1677
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1678
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1679
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1680
    public void testCatchException() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1681
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1682
        startTest("catchException");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1683
        for (int nargs = 2; nargs <= 6; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1684
            for (int ti = 0; ti <= 1; ti++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1685
                boolean throwIt = (ti != 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1686
                testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1687
                testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1688
                testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1689
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1690
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1691
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1692
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1693
    private static <T extends Throwable>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1694
    Object throwOrReturn(Object normal, T exception) throws T {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1695
        if (exception != null)  throw exception;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1696
        return normal;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1697
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1698
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1699
    void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1700
        countTest();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1701
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1702
            System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1703
        Class<? extends Throwable> exType = thrown.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1704
        MethodHandle throwOrReturn
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1705
                = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1706
                    MethodType.methodType(Object.class, Object.class, Throwable.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1707
        MethodHandle thrower = throwOrReturn;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1708
        while (thrower.type().parameterCount() < nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1709
            thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1710
        MethodHandle target = MethodHandles.catchException(thrower,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1711
                thrown.getClass(), ValueConversions.varargsList(1+nargs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1712
        assertEquals(thrower.type(), target.type());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1713
        //System.out.println("catching with "+target+" : "+throwOrReturn);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1714
        Object[] args = randomArgs(nargs, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1715
        args[1] = (throwIt ? thrown : null);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1716
        Object returned = target.invokeVarargs(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1717
        //System.out.println("return from "+target+" : "+returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1718
        if (!throwIt) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1719
            assertSame(args[0], returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1720
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1721
            List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1722
            catchArgs.add(0, thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1723
            assertEquals(catchArgs, returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1724
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1725
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1726
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1727
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1728
    public void testThrowException() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1729
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1730
        startTest("throwException");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1731
        testThrowException(int.class, new ClassCastException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1732
        testThrowException(void.class, new java.io.IOException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1733
        testThrowException(String.class, new LinkageError("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1734
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1735
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1736
    void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1737
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1738
        Class<? extends Throwable> exType = thrown.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1739
        MethodHandle target = MethodHandles.throwException(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1740
        //System.out.println("throwing with "+target+" : "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1741
        MethodType expectedType = MethodType.methodType(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1742
        assertEquals(expectedType, target.type());
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1743
        target = MethodHandles.convertArguments(target, target.type().generic());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1744
        Throwable caught = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1745
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1746
            Object res = target.invokeExact((Object) thrown);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1747
            fail("got "+res+" instead of throwing "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1748
        } catch (Throwable ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1749
            if (ex != thrown) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1750
                if (ex instanceof Error)  throw (Error)ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1751
                if (ex instanceof RuntimeException)  throw (RuntimeException)ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1752
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1753
            caught = ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1754
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1755
        assertSame(thrown, caught);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1756
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1757
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1758
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1759
    public void testCastFailure() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1760
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1761
        startTest("testCastFailure");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1762
        testCastFailure("cast/argument", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1763
        testCastFailure("unbox/argument", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1764
        testCastFailure("cast/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1765
        testCastFailure("unbox/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1766
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1767
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1768
    static class Surprise extends JavaMethodHandle {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1769
        Surprise() { super("value"); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1770
        Object value(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1771
            trace("value", x);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1772
            if (boo != null)  return boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1773
            return x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1774
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1775
        Object boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1776
        void boo(Object x) { boo = x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1777
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1778
        static void trace(String x, Object y) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1779
            if (verbosity > 8) System.out.println(x+"="+y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1780
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1781
        static Object  refIdentity(Object x)  { trace("ref.x", x); return x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1782
        static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1783
        static int     intIdentity(int x)     { trace("int.x", x); return x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1784
        static MethodHandle REF_IDENTITY = PRIVATE.findStatic(
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1785
                Surprise.class, "refIdentity",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1786
                    MethodType.methodType(Object.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1787
        static MethodHandle BOX_IDENTITY = PRIVATE.findStatic(
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1788
                Surprise.class, "boxIdentity",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1789
                    MethodType.methodType(Integer.class, Integer.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1790
        static MethodHandle INT_IDENTITY = PRIVATE.findStatic(
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1791
                Surprise.class, "intIdentity",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1792
                    MethodType.methodType(int.class, int.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1793
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1794
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1795
    void testCastFailure(String mode, int okCount) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1796
        countTest(false);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1797
        if (verbosity > 2)  System.out.println("mode="+mode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1798
        Surprise boo = new Surprise();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1799
        MethodHandle identity = Surprise.REF_IDENTITY, surprise = boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1800
        if (mode.endsWith("/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1801
            if (mode.equals("unbox/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1802
                // fail on return to ((Integer)surprise).intValue
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1803
                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(int.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1804
                identity = MethodHandles.convertArguments(identity, MethodType.methodType(int.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1805
            } else if (mode.equals("cast/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1806
                // fail on return to (Integer)surprise
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1807
                surprise = MethodHandles.convertArguments(surprise, MethodType.methodType(Integer.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1808
                identity = MethodHandles.convertArguments(identity, MethodType.methodType(Integer.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1809
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1810
        } else if (mode.endsWith("/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1811
            MethodHandle callee = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1812
            if (mode.equals("unbox/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1813
                // fail on handing surprise to int argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1814
                callee = Surprise.INT_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1815
            } else if (mode.equals("cast/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1816
                // fail on handing surprise to Integer argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1817
                callee = Surprise.BOX_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1818
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1819
            if (callee != null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1820
                callee = MethodHandles.convertArguments(callee, MethodType.genericMethodType(1));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1821
                surprise = MethodHandles.filterArguments(callee, surprise);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1822
                identity = MethodHandles.filterArguments(callee, identity);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1823
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1824
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1825
        assertNotSame(mode, surprise, boo);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1826
        identity = MethodHandles.convertArguments(identity, MethodType.genericMethodType(1));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1827
        surprise = MethodHandles.convertArguments(surprise, MethodType.genericMethodType(1));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1828
        Object x = 42;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1829
        for (int i = 0; i < okCount; i++) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1830
            Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1831
            assertEquals(x, y);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1832
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1833
            assertEquals(x, z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1834
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1835
        boo.boo("Boo!");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1836
        Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1837
        assertEquals(x, y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1838
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1839
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1840
            System.out.println("Failed to throw; got z="+z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1841
            assertTrue(false);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1842
        } catch (Exception ex) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1843
            if (verbosity > 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1844
                System.out.println("caught "+ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1845
            if (verbosity > 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1846
                ex.printStackTrace();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1847
            assertTrue(ex instanceof ClassCastException
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1848
                    // FIXME: accept only one of the two for any given unit test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1849
                    || ex instanceof WrongMethodTypeException
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1850
                    );
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1851
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1852
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1853
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1854
}
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1855
// Local abbreviated copy of sun.dyn.util.ValueConversions
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1856
class ValueConversions {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1857
    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1858
    private static final Object[] NO_ARGS_ARRAY = {};
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1859
    private static Object[] makeArray(Object... args) { return args; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1860
    private static Object[] array() { return NO_ARGS_ARRAY; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1861
    private static Object[] array(Object a0)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1862
                { return makeArray(a0); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1863
    private static Object[] array(Object a0, Object a1)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1864
                { return makeArray(a0, a1); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1865
    private static Object[] array(Object a0, Object a1, Object a2)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1866
                { return makeArray(a0, a1, a2); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1867
    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1868
                { return makeArray(a0, a1, a2, a3); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1869
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1870
                                  Object a4)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1871
                { return makeArray(a0, a1, a2, a3, a4); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1872
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1873
                                  Object a4, Object a5)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1874
                { return makeArray(a0, a1, a2, a3, a4, a5); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1875
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1876
                                  Object a4, Object a5, Object a6)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1877
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1878
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1879
                                  Object a4, Object a5, Object a6, Object a7)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1880
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1881
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1882
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1883
                                  Object a8)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1884
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1885
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1886
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1887
                                  Object a8, Object a9)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1888
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1889
    static MethodHandle[] makeArrays() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1890
        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1891
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1892
        for (;;) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1893
            int nargs = arrays.size();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1894
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1895
            String name = "array";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1896
            MethodHandle array = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1897
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1898
                array = lookup.findStatic(ValueConversions.class, name, type);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1899
            } catch (NoAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1900
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1901
            if (array == null)  break;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1902
            arrays.add(array);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1903
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1904
        assert(arrays.size() == 11);  // current number of methods
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1905
        return arrays.toArray(new MethodHandle[0]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1906
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1907
    static final MethodHandle[] ARRAYS = makeArrays();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1908
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1909
    /** Return a method handle that takes the indicated number of Object
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1910
     *  arguments and returns an Object array of them, as if for varargs.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1911
     */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1912
    public static MethodHandle varargsArray(int nargs) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1913
        if (nargs < ARRAYS.length)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1914
            return ARRAYS[nargs];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1915
        // else need to spin bytecode or do something else fancy
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1916
        throw new UnsupportedOperationException("NYI: cannot form a varargs array of length "+nargs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1917
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1918
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1919
    private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1920
    private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1921
    private static List<Object> list() { return NO_ARGS_LIST; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1922
    private static List<Object> list(Object a0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1923
                { return makeList(a0); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1924
    private static List<Object> list(Object a0, Object a1)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1925
                { return makeList(a0, a1); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1926
    private static List<Object> list(Object a0, Object a1, Object a2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1927
                { return makeList(a0, a1, a2); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1928
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1929
                { return makeList(a0, a1, a2, a3); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1930
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1931
                                     Object a4)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1932
                { return makeList(a0, a1, a2, a3, a4); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1933
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1934
                                     Object a4, Object a5)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1935
                { return makeList(a0, a1, a2, a3, a4, a5); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1936
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1937
                                     Object a4, Object a5, Object a6)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1938
                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1939
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1940
                                     Object a4, Object a5, Object a6, Object a7)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1941
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1942
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1943
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1944
                                     Object a8)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1945
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1946
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1947
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1948
                                     Object a8, Object a9)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1949
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1950
    static MethodHandle[] makeLists() {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1951
        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1952
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1953
        for (;;) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1954
            int nargs = arrays.size();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1955
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1956
            String name = "list";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1957
            MethodHandle array = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1958
            try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1959
                array = lookup.findStatic(ValueConversions.class, name, type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1960
            } catch (NoAccessException ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1961
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1962
            if (array == null)  break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1963
            arrays.add(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1964
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1965
        assert(arrays.size() == 11);  // current number of methods
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1966
        return arrays.toArray(new MethodHandle[0]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1967
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1968
    static final MethodHandle[] LISTS = makeLists();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1969
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1970
    /** Return a method handle that takes the indicated number of Object
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1971
     *  arguments and returns List.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1972
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1973
    public static MethodHandle varargsList(int nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1974
        if (nargs < LISTS.length)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1975
            return LISTS[nargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1976
        // else need to spin bytecode or do something else fancy
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1977
        throw new UnsupportedOperationException("NYI");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1978
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1979
}
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1980
// This guy tests access from outside the same package member, but inside
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1981
// the package itself.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1982
class PackageSibling {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1983
    static Lookup lookup() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1984
        return MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1985
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1986
}