jdk/test/java/dyn/InvokeGenericTest.java
author duke
Wed, 05 Jul 2017 17:38:31 +0200
changeset 8717 f75a1efb1412
parent 8347 e5daa5772ffd
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     1
/*
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     4
 *
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    10
 *
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    15
 * accompanied this code).
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    16
 *
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    20
 *
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    23
 * questions.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    24
 */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    25
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    26
/* @test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    27
 * @summary unit tests for java.dyn.MethodHandle.invokeGeneric
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
    28
 * @compile -XDallowTransitionalJSR292=no -target 7 InvokeGenericTest.java
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    29
 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.dyn.InvokeGenericTest
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    30
 */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    31
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    32
package test.java.dyn;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    33
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    34
import java.dyn.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    35
import static java.dyn.MethodHandles.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    36
import static java.dyn.MethodType.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    37
import java.lang.reflect.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    38
import java.util.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    39
import org.junit.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    40
import static org.junit.Assert.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    41
import static org.junit.Assume.*;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    42
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    43
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    44
/**
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    45
 *
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    46
 * @author jrose
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    47
 */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    48
public class InvokeGenericTest {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    49
    // How much output?
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    50
    static int verbosity = 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    51
    static {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    52
        String vstr = System.getProperty("test.java.dyn.InvokeGenericTest.verbosity");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    53
        if (vstr != null)  verbosity = Integer.parseInt(vstr);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    54
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    55
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    56
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    57
    public void testFirst() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    58
        verbosity += 9; try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    59
            // left blank for debugging
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    60
        } finally { printCounts(); verbosity -= 9; }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    61
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    62
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    63
    public InvokeGenericTest() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    64
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    65
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    66
    @Before
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    67
    public void checkImplementedPlatform() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    68
        boolean platformOK = false;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    69
        Properties properties = System.getProperties();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    70
        String vers = properties.getProperty("java.vm.version");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    71
        String name = properties.getProperty("java.vm.name");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    72
        String arch = properties.getProperty("os.arch");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    73
        if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") ||
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    74
             arch.equals("sparc") || arch.equals("sparcv9")) &&
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    75
            (name.contains("Client") || name.contains("Server"))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    76
            ) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    77
            platformOK = true;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    78
        } else {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    79
            System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    80
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    81
        assumeTrue(platformOK);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    82
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    83
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    84
    String testName;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    85
    static int allPosTests, allNegTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    86
    int posTests, negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    87
    @After
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    88
    public void printCounts() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    89
        if (verbosity >= 2 && (posTests | negTests) != 0) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    90
            System.out.println();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    91
            if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    92
            if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    93
            allPosTests += posTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    94
            allNegTests += negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    95
            posTests = negTests = 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    96
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    97
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    98
    void countTest(boolean positive) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    99
        if (positive) ++posTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   100
        else          ++negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   101
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   102
    void countTest() { countTest(true); }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   103
    void startTest(String name) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   104
        if (testName != null)  printCounts();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   105
        if (verbosity >= 1)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   106
            System.out.println(name);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   107
        posTests = negTests = 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   108
        testName = name;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   109
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   110
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   111
    @BeforeClass
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   112
    public static void setUpClass() throws Exception {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   113
        calledLog.clear();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   114
        calledLog.add(null);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   115
        nextArgVal = INITIAL_ARG_VAL;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   116
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   117
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   118
    @AfterClass
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   119
    public static void tearDownClass() throws Exception {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   120
        int posTests = allPosTests, negTests = allNegTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   121
        if (verbosity >= 2 && (posTests | negTests) != 0) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   122
            System.out.println();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   123
            if (posTests != 0)  System.out.println("=== "+posTests+" total positive test cases");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   124
            if (negTests != 0)  System.out.println("=== "+negTests+" total negative test cases");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   125
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   126
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   127
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   128
    static List<Object> calledLog = new ArrayList<Object>();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   129
    static Object logEntry(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   130
        return Arrays.asList(name, Arrays.asList(args));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   131
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   132
    static Object called(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   133
        Object entry = logEntry(name, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   134
        calledLog.add(entry);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   135
        return entry;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   136
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   137
    static void assertCalled(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   138
        Object expected = logEntry(name, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   139
        Object actual   = calledLog.get(calledLog.size() - 1);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   140
        if (expected.equals(actual) && verbosity < 9)  return;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   141
        System.out.println("assertCalled "+name+":");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   142
        System.out.println("expected:   "+expected);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   143
        System.out.println("actual:     "+actual);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   144
        System.out.println("ex. types:  "+getClasses(expected));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   145
        System.out.println("act. types: "+getClasses(actual));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   146
        assertEquals("previous method call", expected, actual);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   147
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   148
    static void printCalled(MethodHandle target, String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   149
        if (verbosity >= 3)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   150
            System.out.println("calling MH="+target+" to "+name+Arrays.toString(args));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   151
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   152
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   153
    static Object castToWrapper(Object value, Class<?> dst) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   154
        Object wrap = null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   155
        if (value instanceof Number)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   156
            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   157
        if (value instanceof Character)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   158
            wrap = castToWrapperOrNull((char)(Character)value, dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   159
        if (wrap != null)  return wrap;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   160
        return dst.cast(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   161
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   162
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   163
    static Object castToWrapperOrNull(long value, Class<?> dst) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   164
        if (dst == int.class || dst == Integer.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   165
            return (int)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   166
        if (dst == long.class || dst == Long.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   167
            return (long)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   168
        if (dst == char.class || dst == Character.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   169
            return (char)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   170
        if (dst == short.class || dst == Short.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   171
            return (short)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   172
        if (dst == float.class || dst == Float.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   173
            return (float)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   174
        if (dst == double.class || dst == Double.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   175
            return (double)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   176
        if (dst == byte.class || dst == Byte.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   177
            return (byte)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   178
        if (dst == boolean.class || dst == boolean.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   179
            return ((value % 29) & 1) == 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   180
        return null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   181
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   182
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   183
    static final int ONE_MILLION = (1000*1000),  // first int value
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   184
                     TEN_BILLION = (10*1000*1000*1000),  // scale factor to reach upper 32 bits
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   185
                     INITIAL_ARG_VAL = ONE_MILLION << 1;  // <<1 makes space for sign bit;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   186
    static long nextArgVal;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   187
    static long nextArg(boolean moreBits) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   188
        long val = nextArgVal++;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   189
        long sign = -(val & 1); // alternate signs
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   190
        val >>= 1;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   191
        if (moreBits)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   192
            // Guarantee some bits in the high word.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   193
            // In any case keep the decimal representation simple-looking,
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   194
            // with lots of zeroes, so as not to make the printed decimal
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   195
            // strings unnecessarily noisy.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   196
            val += (val % ONE_MILLION) * TEN_BILLION;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   197
        return val ^ sign;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   198
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   199
    static int nextArg() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   200
        // Produce a 32-bit result something like ONE_MILLION+(smallint).
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   201
        // Example: 1_000_042.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   202
        return (int) nextArg(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   203
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   204
    static long nextArg(Class<?> kind) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   205
        if (kind == long.class   || kind == Long.class ||
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   206
            kind == double.class || kind == Double.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   207
            // produce a 64-bit result something like
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   208
            // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   209
            // Example: 10_000_420_001_000_042.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   210
            return nextArg(true);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   211
        return (long) nextArg();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   212
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   213
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   214
    static Object randomArg(Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   215
        Object wrap = castToWrapperOrNull(nextArg(param), param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   216
        if (wrap != null) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   217
            return wrap;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   218
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   219
//        import sun.dyn.util.Wrapper;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   220
//        Wrapper wrap = Wrapper.forBasicType(dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   221
//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   222
//            wrap = Wrapper.forWrapperType(dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   223
//        if (wrap != Wrapper.OBJECT)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   224
//            return wrap.wrap(nextArg++);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   225
        if (param.isInterface()) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   226
            for (Class<?> c : param.getClasses()) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   227
                if (param.isAssignableFrom(c) && !c.isInterface())
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   228
                    { param = c; break; }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   229
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   230
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   231
        if (param.isInterface() || param.isAssignableFrom(String.class))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   232
            return "#"+nextArg();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   233
        else
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   234
            try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   235
                return param.newInstance();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   236
            } catch (InstantiationException ex) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   237
            } catch (IllegalAccessException ex) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   238
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   239
        return null;  // random class not Object, String, Integer, etc.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   240
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   241
    static Object[] randomArgs(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   242
        Object[] args = new Object[params.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   243
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   244
            args[i] = randomArg(params[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   245
        return args;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   246
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   247
    static Object[] randomArgs(int nargs, Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   248
        Object[] args = new Object[nargs];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   249
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   250
            args[i] = randomArg(param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   251
        return args;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   252
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   253
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   254
    static final Object ANON_OBJ = new Object();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   255
    static Object zeroArg(Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   256
        Object x = castToWrapperOrNull(0L, param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   257
        if (x != null)  return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   258
        if (param.isInterface() || param.isAssignableFrom(String.class))  return "\"\"";
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   259
        if (param == Object.class)  return ANON_OBJ;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   260
        if (param.getComponentType() != null)  return Array.newInstance(param.getComponentType(), 0);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   261
        return null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   262
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   263
    static Object[] zeroArgs(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   264
        Object[] args = new Object[params.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   265
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   266
            args[i] = zeroArg(params[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   267
        return args;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   268
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   269
    static Object[] zeroArgs(List<Class<?>> params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   270
        return zeroArgs(params.toArray(new Class<?>[0]));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   271
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   272
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   273
    static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   274
        return Arrays.copyOf(a, a.length, atype);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   275
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   276
    static <T> T[] cat(T[] a, T... b) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   277
        int alen = a.length, blen = b.length;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   278
        if (blen == 0)  return a;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   279
        T[] c = Arrays.copyOf(a, alen + blen);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   280
        System.arraycopy(b, 0, c, alen, blen);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   281
        return c;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   282
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   283
    static Integer[] boxAll(int... vx) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   284
        Integer[] res = new Integer[vx.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   285
        for (int i = 0; i < res.length; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   286
            res[i] = vx[i];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   287
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   288
        return res;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   289
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   290
    static Object getClasses(Object x) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   291
        if (x == null)  return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   292
        if (x instanceof String)  return x;  // keep the name
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   293
        if (x instanceof List) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   294
            // recursively report classes of the list elements
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   295
            Object[] xa = ((List)x).toArray();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   296
            for (int i = 0; i < xa.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   297
                xa[i] = getClasses(xa[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   298
            return Arrays.asList(xa);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   299
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   300
        return x.getClass().getSimpleName();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   301
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   302
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   303
    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   304
        return changeArgTypes(target, 0, 999, argType);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   305
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   306
    static MethodHandle changeArgTypes(MethodHandle target,
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   307
            int beg, int end, Class<?> argType) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   308
        MethodType targetType = target.type();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   309
        end = Math.min(end, targetType.parameterCount());
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   310
        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   311
        Collections.fill(argTypes.subList(beg, end), argType);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   312
        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   313
        return MethodHandles.convertArguments(target, ttype2);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   314
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   315
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   316
    // This lookup is good for all members in and under InvokeGenericTest.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   317
    static final Lookup LOOKUP = MethodHandles.lookup();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   318
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   319
    Map<List<Class<?>>, MethodHandle> CALLABLES = new HashMap<List<Class<?>>, MethodHandle>();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   320
    MethodHandle callable(List<Class<?>> params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   321
        MethodHandle mh = CALLABLES.get(params);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   322
        if (mh == null) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7556
diff changeset
   323
            mh = collector_MH.asType(methodType(Object.class, params));
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   324
            CALLABLES.put(params, mh);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   325
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   326
        return mh;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   327
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   328
    MethodHandle callable(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   329
        return callable(Arrays.asList(params));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   330
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   331
    private static Object collector(Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   332
        return Arrays.asList(args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   333
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   334
    private static final MethodHandle collector_MH;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   335
    static {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   336
        try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   337
            collector_MH
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   338
                = LOOKUP.findStatic(LOOKUP.lookupClass(),
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   339
                                    "collector",
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   340
                                    methodType(Object.class, Object[].class));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   341
        } catch (ReflectiveOperationException ex) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   342
            throw new RuntimeException(ex);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   343
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   344
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   345
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   346
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   347
    public void testSimple() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   348
        startTest("testSimple");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   349
        countTest();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   350
        String[] args = { "one", "two" };
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   351
        MethodHandle mh = callable(Object.class, String.class);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   352
        Object res; List resl;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   353
        res = resl = (List) mh.invokeGeneric((String)args[0], (Object)args[1]);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   354
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   355
        assertEquals(Arrays.asList(args), res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   356
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   357
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   358
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   359
    public void testWrongArgumentCount() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   360
        startTest("testWrongArgumentCount");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   361
        for (int i = 0; i <= 10; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   362
            testWrongArgumentCount(Collections.<Class<?>>nCopies(i, Integer.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   363
            if (i <= 4) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   364
                testWrongArgumentCount(Collections.<Class<?>>nCopies(i, int.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   365
                testWrongArgumentCount(Collections.<Class<?>>nCopies(i, long.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   366
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   367
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   368
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   369
    public void testWrongArgumentCount(List<Class<?>> params) throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   370
        int max = params.size();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   371
        for (int i = 0; i < max; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   372
            List<Class<?>> params2 = params.subList(0, i);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   373
            for (int k = 0; k <= 2; k++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   374
                if (k == 1)  params  = methodType(Object.class,  params).generic().parameterList();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   375
                if (k == 2)  params2 = methodType(Object.class, params2).generic().parameterList();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   376
                testWrongArgumentCount(params, params2);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   377
                testWrongArgumentCount(params2, params);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   378
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   379
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   380
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   381
    public void testWrongArgumentCount(List<Class<?>> expect, List<Class<?>> observe) throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   382
        countTest(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   383
        if (expect.equals(observe))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   384
            assert(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   385
        MethodHandle target = callable(expect);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   386
        Object[] args = zeroArgs(observe);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   387
        Object junk;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   388
        try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   389
            switch (args.length) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   390
            case 0:
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   391
                junk = target.invokeGeneric(); break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   392
            case 1:
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   393
                junk = target.invokeGeneric(args[0]); break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   394
            case 2:
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   395
                junk = target.invokeGeneric(args[0], args[1]); break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   396
            case 3:
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   397
                junk = target.invokeGeneric(args[0], args[1], args[2]); break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   398
            case 4:
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   399
                junk = target.invokeGeneric(args[0], args[1], args[2], args[3]); break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   400
            default:
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   401
                junk = target.invokeWithArguments(args); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   402
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   403
        } catch (WrongMethodTypeException ex) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   404
            return;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   405
        } catch (Exception ex) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   406
            throw new RuntimeException("wrong exception calling "+target+target.type()+" on "+Arrays.asList(args)+" : "+ex);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   407
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   408
        throw new RuntimeException("bad success calling "+target+target.type()+" on "+Arrays.asList(args));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   409
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   410
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   411
    /** Make a list of all combinations of the given types, with the given arities.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   412
     *  A void return type is possible iff the first type is void.class.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   413
     */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   414
    static List<MethodType> allMethodTypes(int minargc, int maxargc, Class<?>... types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   415
        ArrayList<MethodType> result = new ArrayList<MethodType>();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   416
        if (types.length > 0) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   417
            ArrayList<MethodType> argcTypes = new ArrayList<MethodType>();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   418
            // build arity-zero types first
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   419
            for (Class<?> rtype : types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   420
                argcTypes.add(MethodType.methodType(rtype));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   421
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   422
            if (types[0] == void.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   423
                // void is not an argument type
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   424
                types = Arrays.copyOfRange(types, 1, types.length);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   425
            for (int argc = 0; argc <= maxargc; argc++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   426
                if (argc >= minargc)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   427
                    result.addAll(argcTypes);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   428
                if (argc >= maxargc)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   429
                    break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   430
                ArrayList<MethodType> prevTypes = argcTypes;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   431
                argcTypes = new ArrayList<MethodType>();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   432
                for (MethodType prevType : prevTypes) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   433
                    for (Class<?> ptype : types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   434
                        argcTypes.add(prevType.insertParameterTypes(argc, ptype));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   435
                    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   436
                }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   437
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   438
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   439
        return Collections.unmodifiableList(result);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   440
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   441
    static List<MethodType> allMethodTypes(int argc, Class<?>... types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   442
        return allMethodTypes(argc, argc, types);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   443
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   444
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   445
    interface RandomInterface { }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   446
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   447
    MethodHandle toString_MH;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   448
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   449
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   450
    public void testReferenceConversions() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   451
        startTest("testReferenceConversions");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   452
        toString_MH = LOOKUP.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   453
            findVirtual(Object.class, "toString", MethodType.methodType(String.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   454
        String[] args = { "one", "two" };
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   455
        for (MethodType type : allMethodTypes(2, Object.class, String.class, RandomInterface.class)) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   456
            testReferenceConversions(type, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   457
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   458
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   459
    public void testReferenceConversions(MethodType type, Object... args) throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   460
        countTest();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   461
        if (verbosity > 3)  System.out.println("target type: "+type);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   462
        MethodHandle mh = callable(type.parameterList());
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   463
        MethodHandle tsdrop = MethodHandles.dropArguments(toString_MH, 1, type.parameterList());
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   464
        mh = MethodHandles.foldArguments(tsdrop, mh);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   465
        mh = mh.asType(type);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   466
        Object res = mh.invokeGeneric((String)args[0], (Object)args[1]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   467
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   468
        assertEquals(Arrays.asList(args).toString(), res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   469
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   470
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   471
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   472
    @Test @Ignore("known failure pending 6939861")
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   473
    public void testBoxConversions() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   474
        startTest("testBoxConversions");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   475
        countTest();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   476
        Integer[] args = { 1, 2 };
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   477
        MethodHandle mh = callable(Object.class, int.class);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   478
        Object res; List resl;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   479
        res = resl = (List) mh.invokeGeneric((int)args[0], (Object)args[1]);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   480
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   481
        assertEquals(Arrays.asList(args), res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   482
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   483
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   484
}