jdk/test/java/lang/invoke/InvokeGenericTest.java
author jmelvin
Mon, 16 Apr 2012 18:09:53 -0400
changeset 12538 211d6e82fe51
parent 11534 9949ffb8eb3a
child 13423 17843fff200d
permissions -rw-r--r--
7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6 Summary: On Mac OS X, align system property "os.arch" with Apple legacy JDKs. Also, improve os.name string matching by using .contains() method instead of .startsWith(). This fix spans multiple repositories. Reviewed-by: dcubed, phh, ohair, katleman
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
/*
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 11534
diff changeset
     2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
7554
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
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
    27
 * @summary unit tests for java.lang.invoke.MethodHandle.invoke
10440
b90cf2f98868 7089131: test/java/lang/invoke/InvokeGenericTest.java does not compile
alanb
parents: 9752
diff changeset
    28
 * @compile InvokeGenericTest.java
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    29
 * @run junit/othervm test.java.lang.invoke.InvokeGenericTest
7554
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
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    32
package test.java.lang.invoke;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    33
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    34
import java.lang.invoke.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    35
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    36
import static java.lang.invoke.MethodType.*;
7554
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
 */
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
    48
@SuppressWarnings("cast")  // various casts help emphasize arguments to invokeExact
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    49
public class InvokeGenericTest {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    50
    // How much output?
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    51
    static int verbosity = 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    52
    static {
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    53
        String vstr = System.getProperty("test.java.lang.invoke.InvokeGenericTest.verbosity");
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    54
        if (vstr != null)  verbosity = Integer.parseInt(vstr);
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
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
    57
//    public static void main(String... av) throws Throwable {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
    58
//        new InvokeGenericTest().testFirst();
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
    59
//    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
    60
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    61
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    62
    public void testFirst() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    63
        verbosity += 9; try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    64
            // left blank for debugging
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    65
        } finally { printCounts(); verbosity -= 9; }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    66
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    67
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    68
    public InvokeGenericTest() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    69
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    70
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    71
    @Before
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    72
    public void checkImplementedPlatform() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    73
        boolean platformOK = false;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    74
        Properties properties = System.getProperties();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    75
        String vers = properties.getProperty("java.vm.version");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    76
        String name = properties.getProperty("java.vm.name");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    77
        String arch = properties.getProperty("os.arch");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    78
        if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") ||
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 11534
diff changeset
    79
             arch.equals("x86_64") || arch.equals("sparc") || arch.equals("sparcv9")) &&
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    80
            (name.contains("Client") || name.contains("Server"))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    81
            ) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    82
            platformOK = true;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    83
        } else {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    84
            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
    85
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    86
        assumeTrue(platformOK);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    87
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    88
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    89
    String testName;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    90
    static int allPosTests, allNegTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    91
    int posTests, negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    92
    @After
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    93
    public void printCounts() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    94
        if (verbosity >= 2 && (posTests | negTests) != 0) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    95
            System.out.println();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    96
            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
    97
            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
    98
            allPosTests += posTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
    99
            allNegTests += negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   100
            posTests = negTests = 0;
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
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   103
    void countTest(boolean positive) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   104
        if (positive) ++posTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   105
        else          ++negTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   106
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   107
    void countTest() { countTest(true); }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   108
    void startTest(String name) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   109
        if (testName != null)  printCounts();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   110
        if (verbosity >= 1)
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   111
            System.out.println("["+name+"]");
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   112
        posTests = negTests = 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   113
        testName = name;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   114
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   115
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   116
    @BeforeClass
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   117
    public static void setUpClass() throws Exception {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   118
        calledLog.clear();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   119
        calledLog.add(null);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   120
        nextArgVal = INITIAL_ARG_VAL;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   121
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   122
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   123
    @AfterClass
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   124
    public static void tearDownClass() throws Exception {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   125
        int posTests = allPosTests, negTests = allNegTests;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   126
        if (verbosity >= 2 && (posTests | negTests) != 0) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   127
            System.out.println();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   128
            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
   129
            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
   130
        }
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
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   133
    static List<Object> calledLog = new ArrayList<>();
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   134
    static Object logEntry(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   135
        return Arrays.asList(name, Arrays.asList(args));
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 Object called(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   138
        Object entry = logEntry(name, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   139
        calledLog.add(entry);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   140
        return entry;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   141
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   142
    static void assertCalled(String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   143
        Object expected = logEntry(name, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   144
        Object actual   = calledLog.get(calledLog.size() - 1);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   145
        if (expected.equals(actual) && verbosity < 9)  return;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   146
        System.out.println("assertCalled "+name+":");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   147
        System.out.println("expected:   "+expected);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   148
        System.out.println("actual:     "+actual);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   149
        System.out.println("ex. types:  "+getClasses(expected));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   150
        System.out.println("act. types: "+getClasses(actual));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   151
        assertEquals("previous method call", expected, actual);
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 void printCalled(MethodHandle target, String name, Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   154
        if (verbosity >= 3)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   155
            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
   156
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   157
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   158
    static Object castToWrapper(Object value, Class<?> dst) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   159
        Object wrap = null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   160
        if (value instanceof Number)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   161
            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   162
        if (value instanceof Character)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   163
            wrap = castToWrapperOrNull((char)(Character)value, dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   164
        if (wrap != null)  return wrap;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   165
        return dst.cast(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   166
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   167
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   168
    static Object castToWrapperOrNull(long value, Class<?> dst) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   169
        if (dst == int.class || dst == Integer.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   170
            return (int)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   171
        if (dst == long.class || dst == Long.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   172
            return (long)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   173
        if (dst == char.class || dst == Character.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   174
            return (char)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   175
        if (dst == short.class || dst == Short.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   176
            return (short)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   177
        if (dst == float.class || dst == Float.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   178
            return (float)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   179
        if (dst == double.class || dst == Double.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   180
            return (double)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   181
        if (dst == byte.class || dst == Byte.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   182
            return (byte)(value);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   183
        if (dst == boolean.class || dst == boolean.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   184
            return ((value % 29) & 1) == 0;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   185
        return null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   186
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   187
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   188
    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
   189
                     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
   190
                     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
   191
    static long nextArgVal;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   192
    static long nextArg(boolean moreBits) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   193
        long val = nextArgVal++;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   194
        long sign = -(val & 1); // alternate signs
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   195
        val >>= 1;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   196
        if (moreBits)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   197
            // Guarantee some bits in the high word.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   198
            // 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
   199
            // 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
   200
            // strings unnecessarily noisy.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   201
            val += (val % ONE_MILLION) * TEN_BILLION;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   202
        return val ^ sign;
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 int nextArg() {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   205
        // 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
   206
        // Example: 1_000_042.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   207
        return (int) nextArg(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   208
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   209
    static long nextArg(Class<?> kind) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   210
        if (kind == long.class   || kind == Long.class ||
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   211
            kind == double.class || kind == Double.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   212
            // produce a 64-bit result something like
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   213
            // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   214
            // Example: 10_000_420_001_000_042.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   215
            return nextArg(true);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   216
        return (long) nextArg();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   217
    }
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
    static Object randomArg(Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   220
        Object wrap = castToWrapperOrNull(nextArg(param), param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   221
        if (wrap != null) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   222
            return wrap;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   223
        }
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   224
//        import sun.invoke.util.Wrapper;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   225
//        Wrapper wrap = Wrapper.forBasicType(dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   226
//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   227
//            wrap = Wrapper.forWrapperType(dst);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   228
//        if (wrap != Wrapper.OBJECT)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   229
//            return wrap.wrap(nextArg++);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   230
        if (param.isInterface()) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   231
            for (Class<?> c : param.getClasses()) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   232
                if (param.isAssignableFrom(c) && !c.isInterface())
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   233
                    { param = c; break; }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   234
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   235
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   236
        if (param.isInterface() || param.isAssignableFrom(String.class))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   237
            return "#"+nextArg();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   238
        else
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   239
            try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   240
                return param.newInstance();
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   241
            } catch (InstantiationException | IllegalAccessException ex) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   242
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   243
        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
   244
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   245
    static Object[] randomArgs(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   246
        Object[] args = new Object[params.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   247
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   248
            args[i] = randomArg(params[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   249
        return args;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   250
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   251
    static Object[] randomArgs(int nargs, Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   252
        Object[] args = new Object[nargs];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   253
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   254
            args[i] = randomArg(param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   255
        return args;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   256
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   257
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   258
    static final Object ANON_OBJ = new Object();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   259
    static Object zeroArg(Class<?> param) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   260
        Object x = castToWrapperOrNull(0L, param);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   261
        if (x != null)  return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   262
        if (param.isInterface() || param.isAssignableFrom(String.class))  return "\"\"";
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   263
        if (param == Object.class)  return ANON_OBJ;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   264
        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
   265
        return null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   266
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   267
    static Object[] zeroArgs(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   268
        Object[] args = new Object[params.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   269
        for (int i = 0; i < args.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   270
            args[i] = zeroArg(params[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   271
        return args;
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 Object[] zeroArgs(List<Class<?>> params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   274
        return zeroArgs(params.toArray(new Class<?>[0]));
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
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   277
    @SafeVarargs @SuppressWarnings("varargs")
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   278
    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
   279
        return Arrays.copyOf(a, a.length, atype);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   280
    }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   281
    @SafeVarargs @SuppressWarnings("varargs")
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   282
    static <T> T[] cat(T[] a, T... b) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   283
        int alen = a.length, blen = b.length;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   284
        if (blen == 0)  return a;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   285
        T[] c = Arrays.copyOf(a, alen + blen);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   286
        System.arraycopy(b, 0, c, alen, blen);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   287
        return c;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   288
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   289
    static Integer[] boxAll(int... vx) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   290
        Integer[] res = new Integer[vx.length];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   291
        for (int i = 0; i < res.length; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   292
            res[i] = vx[i];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   293
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   294
        return res;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   295
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   296
    static Object getClasses(Object x) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   297
        if (x == null)  return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   298
        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
   299
        if (x instanceof List) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   300
            // recursively report classes of the list elements
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   301
            Object[] xa = ((List)x).toArray();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   302
            for (int i = 0; i < xa.length; i++)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   303
                xa[i] = getClasses(xa[i]);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   304
            return Arrays.asList(xa);
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
        return x.getClass().getSimpleName();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   307
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   308
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   309
    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   310
        return changeArgTypes(target, 0, 999, argType);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   311
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   312
    static MethodHandle changeArgTypes(MethodHandle target,
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   313
            int beg, int end, Class<?> argType) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   314
        MethodType targetType = target.type();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   315
        end = Math.min(end, targetType.parameterCount());
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   316
        ArrayList<Class<?>> argTypes = new ArrayList<>(targetType.parameterList());
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   317
        Collections.fill(argTypes.subList(beg, end), argType);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   318
        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9730
diff changeset
   319
        return target.asType(ttype2);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   320
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   321
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   322
    // 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
   323
    static final Lookup LOOKUP = MethodHandles.lookup();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   324
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   325
    Map<List<Class<?>>, MethodHandle> CALLABLES = new HashMap<>();
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   326
    MethodHandle callable(List<Class<?>> params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   327
        MethodHandle mh = CALLABLES.get(params);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   328
        if (mh == null) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7556
diff changeset
   329
            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
   330
            CALLABLES.put(params, mh);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   331
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   332
        return mh;
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
    MethodHandle callable(Class<?>... params) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   335
        return callable(Arrays.asList(params));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   336
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   337
    private static Object collector(Object... args) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   338
        return Arrays.asList(args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   339
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   340
    private static final MethodHandle collector_MH;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   341
    static {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   342
        try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   343
            collector_MH
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   344
                = LOOKUP.findStatic(LOOKUP.lookupClass(),
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   345
                                    "collector",
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   346
                                    methodType(Object.class, Object[].class));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   347
        } catch (ReflectiveOperationException ex) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   348
            throw new RuntimeException(ex);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   349
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   350
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   351
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   352
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   353
    public void testSimple() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   354
        startTest("testSimple");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   355
        countTest();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   356
        String[] args = { "one", "two" };
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   357
        MethodHandle mh = callable(Object.class, String.class);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   358
        Object res; List<?> resl;
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   359
        res = resl = (List<?>) mh.invoke((String)args[0], (Object)args[1]);
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   360
        //System.out.println(res);
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   361
        assertEquals(Arrays.asList(args), res);
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   362
    }
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   363
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   364
    @Test
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   365
    public void testSimplePrims() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   366
        startTest("testSimplePrims");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   367
        countTest();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   368
        int[] args = { 1, 2 };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   369
        MethodHandle mh = callable(Object.class, Object.class);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   370
        Object res; List<?> resl;
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   371
        res = resl = (List<?>) mh.invoke(args[0], args[1]);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   372
        //System.out.println(res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   373
        assertEquals(Arrays.toString(args), res.toString());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   374
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   375
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   376
    @Test
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   377
    public void testAlternateName() throws Throwable {
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   378
        startTest("testAlternateName");
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   379
        countTest();
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   380
        String[] args = { "one", "two" };
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   381
        MethodHandle mh = callable(Object.class, String.class);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   382
        Object res; List<?> resl;
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   383
        res = resl = (List<?>) mh.invoke((String)args[0], (Object)args[1]);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   384
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   385
        assertEquals(Arrays.asList(args), res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   386
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   387
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   388
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   389
    public void testWrongArgumentCount() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   390
        startTest("testWrongArgumentCount");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   391
        for (int i = 0; i <= 10; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   392
            testWrongArgumentCount(Collections.<Class<?>>nCopies(i, Integer.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   393
            if (i <= 4) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   394
                testWrongArgumentCount(Collections.<Class<?>>nCopies(i, int.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   395
                testWrongArgumentCount(Collections.<Class<?>>nCopies(i, long.class));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   396
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   397
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   398
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   399
    public void testWrongArgumentCount(List<Class<?>> params) throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   400
        int max = params.size();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   401
        for (int i = 0; i < max; i++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   402
            List<Class<?>> params2 = params.subList(0, i);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   403
            for (int k = 0; k <= 2; k++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   404
                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
   405
                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
   406
                testWrongArgumentCount(params, params2);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   407
                testWrongArgumentCount(params2, params);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   408
            }
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
    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
   412
        countTest(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   413
        if (expect.equals(observe))
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   414
            assert(false);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   415
        MethodHandle target = callable(expect);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   416
        Object[] args = zeroArgs(observe);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   417
        Object junk;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   418
        try {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   419
            switch (args.length) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   420
            case 0:
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   421
                junk = target.invoke(); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   422
            case 1:
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   423
                junk = target.invoke(args[0]); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   424
            case 2:
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   425
                junk = target.invoke(args[0], args[1]); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   426
            case 3:
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   427
                junk = target.invoke(args[0], args[1], args[2]); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   428
            case 4:
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   429
                junk = target.invoke(args[0], args[1], args[2], args[3]); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   430
            default:
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   431
                junk = target.invokeWithArguments(args); break;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   432
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   433
        } catch (WrongMethodTypeException ex) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   434
            return;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   435
        } catch (Exception ex) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   436
            throw new RuntimeException("wrong exception calling "+target+" on "+Arrays.asList(args), ex);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   437
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   438
        throw new RuntimeException("bad success calling "+target+" on "+Arrays.asList(args));
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   439
    }
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
    /** 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
   442
     *  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
   443
     */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   444
    static List<MethodType> allMethodTypes(int minargc, int maxargc, Class<?>... types) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   445
        ArrayList<MethodType> result = new ArrayList<>();
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   446
        if (types.length > 0) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   447
            ArrayList<MethodType> argcTypes = new ArrayList<>();
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   448
            // build arity-zero types first
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   449
            for (Class<?> rtype : types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   450
                argcTypes.add(MethodType.methodType(rtype));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   451
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   452
            if (types[0] == void.class)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   453
                // void is not an argument type
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   454
                types = Arrays.copyOfRange(types, 1, types.length);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   455
            for (int argc = 0; argc <= maxargc; argc++) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   456
                if (argc >= minargc)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   457
                    result.addAll(argcTypes);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   458
                if (argc >= maxargc)
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   459
                    break;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   460
                ArrayList<MethodType> prevTypes = argcTypes;
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   461
                argcTypes = new ArrayList<>();
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   462
                for (MethodType prevType : prevTypes) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   463
                    for (Class<?> ptype : types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   464
                        argcTypes.add(prevType.insertParameterTypes(argc, ptype));
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   465
                    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   466
                }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   467
            }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   468
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   469
        return Collections.unmodifiableList(result);
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
    static List<MethodType> allMethodTypes(int argc, Class<?>... types) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   472
        return allMethodTypes(argc, argc, types);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   473
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   474
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   475
    MethodHandle toString_MH;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   476
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   477
    @Test
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   478
    public void testReferenceConversions() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   479
        startTest("testReferenceConversions");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   480
        toString_MH = LOOKUP.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   481
            findVirtual(Object.class, "toString", MethodType.methodType(String.class));
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   482
        Object[] args = { "one", "two" };
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   483
        for (MethodType type : allMethodTypes(2, Object.class, String.class, CharSequence.class)) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   484
            testReferenceConversions(type, args);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   485
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   486
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   487
    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
   488
        countTest();
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   489
        int nargs = args.length;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   490
        List<Object> argList = Arrays.asList(args);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   491
        String expectString = argList.toString();
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   492
        if (verbosity > 3)  System.out.println("target type: "+type+expectString);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   493
        MethodHandle mh = callable(type.parameterList());
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   494
        mh = MethodHandles.filterReturnValue(mh, toString_MH);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   495
        mh = mh.asType(type);
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   496
        Object res = null;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   497
        if (nargs == 2) {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   498
            res = mh.invoke((Object)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   499
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   500
            res = mh.invoke((String)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   501
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   502
            res = mh.invoke((Object)args[0], (String)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   503
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   504
            res = mh.invoke((String)args[0], (String)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   505
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   506
            res = mh.invoke((String)args[0], (CharSequence)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   507
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   508
            res = mh.invoke((CharSequence)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   509
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   510
            res = (String) mh.invoke((Object)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   511
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   512
            res = (String) mh.invoke((String)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   513
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   514
            res = (CharSequence) mh.invoke((String)args[0], (Object)args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   515
            assertEquals(expectString, res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   516
        } else {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   517
            assert(false);  // write this code
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   518
        }
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   519
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   520
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   521
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   522
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   523
    @Test
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   524
    public void testBoxConversions() throws Throwable {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   525
        startTest("testBoxConversions");
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   526
        countTest();
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   527
        Object[] args = { 1, 2 };
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   528
        MethodHandle mh = callable(Object.class, int.class);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   529
        Object res; List<?> resl; int resi;
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10440
diff changeset
   530
        res = resl = (List<?>) mh.invoke((int)args[0], (Object)args[1]);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   531
        //System.out.println(res);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   532
        assertEquals(Arrays.asList(args), res);
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   533
        mh = MethodHandles.identity(int.class);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   534
        mh = MethodHandles.dropArguments(mh, 1, int.class);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   535
        res = resi = (int) mh.invoke((Object) args[0], (Object) args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   536
        assertEquals(args[0], res);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   537
        res = resi = (int) mh.invoke((int) args[0], (Object) args[1]);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   538
        assertEquals(args[0], res);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   539
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   540
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff changeset
   541
}