jdk/test/java/lang/invoke/MethodHandlesTest.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 31178 6ad68e35506d
child 33874 46651fd30c0b
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     1
/*
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
     2
 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     4
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 20535
diff changeset
     7
 * published by the Free Software Foundation.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     8
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    13
 * accompanied this code).
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    14
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * questions.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    22
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    23
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    24
/* @test
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    25
 * @summary unit tests for java.lang.invoke.MethodHandles
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
    26
 * @library /lib/testlibrary /lib/testlibrary/jsr292
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    27
 * @compile MethodHandlesTest.java remote/RemoteExample.java
14408
db08012d1d6f 7197210: java/lang/invoke/CallSiteTest.java failing on armsflt.
jiangli
parents: 14342
diff changeset
    28
 * @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -esa test.java.lang.invoke.MethodHandlesTest
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    29
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    30
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    31
package test.java.lang.invoke;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    32
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    33
import test.java.lang.invoke.remote.RemoteExample;
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 java.lang.invoke.MethodHandles.Lookup;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    36
import java.lang.reflect.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    37
import java.util.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    38
import org.junit.*;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    39
import static org.junit.Assert.*;
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
    40
import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    41
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    42
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    43
/**
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    44
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    45
 * @author jrose
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    46
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    47
public class MethodHandlesTest {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    48
    static final Class<?> THIS_CLASS = MethodHandlesTest.class;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    49
    // How much output?
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    50
    static int verbosity = 0;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    51
    static {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    52
        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    53
        if (vstr == null)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    54
            vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    55
        if (vstr != null)  verbosity = Integer.parseInt(vstr);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    56
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    57
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    58
    // Set this true during development if you want to fast-forward to
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    59
    // a particular new, non-working test.  Tests which are known to
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    60
    // work (or have recently worked) test this flag and return on true.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    61
    static final boolean CAN_SKIP_WORKING;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    62
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    63
        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".CAN_SKIP_WORKING");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    64
        if (vstr == null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    65
            vstr = System.getProperty(THIS_CLASS.getName()+".CAN_SKIP_WORKING");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    66
        CAN_SKIP_WORKING = Boolean.parseBoolean(vstr);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    67
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    68
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    69
    // Set 'true' to do about 15x fewer tests, especially those redundant with RicochetTest.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    70
    // This might be useful with -Xcomp stress tests that compile all method handles.
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
    71
    static boolean CAN_TEST_LIGHTLY = Boolean.getBoolean(THIS_CLASS.getName()+".CAN_TEST_LIGHTLY");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    72
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    73
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    74
    public void testFirst() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    75
        verbosity += 9; try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    76
            // left blank for debugging
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    77
        } finally { printCounts(); verbosity -= 9; }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    78
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    79
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    80
    static final int MAX_ARG_INCREASE = 3;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    81
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    82
    public MethodHandlesTest() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    83
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    84
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    85
    String testName;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    86
    static int allPosTests, allNegTests;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    87
    int posTests, negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    88
    @After
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    89
    public void printCounts() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    90
        if (verbosity >= 2 && (posTests | negTests) != 0) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    91
            System.out.println();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    92
            if (posTests != 0)  System.out.println("=== "+testName+": "+posTests+" positive test cases run");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    93
            if (negTests != 0)  System.out.println("=== "+testName+": "+negTests+" negative test cases run");
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    94
            allPosTests += posTests;
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    95
            allNegTests += negTests;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    96
            posTests = negTests = 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    97
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    98
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    99
    void countTest(boolean positive) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   100
        if (positive) ++posTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   101
        else          ++negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   102
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   103
    void countTest() { countTest(true); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   104
    void startTest(String name) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   105
        if (testName != null)  printCounts();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   106
        if (verbosity >= 1)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   107
            System.out.println(name);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   108
        posTests = negTests = 0;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   109
        testName = name;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   110
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   111
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   112
    @BeforeClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   113
    public static void setUpClass() throws Exception {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   114
        calledLog.clear();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   115
        calledLog.add(null);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   116
        nextArgVal = INITIAL_ARG_VAL;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   117
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   118
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   119
    @AfterClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   120
    public static void tearDownClass() throws Exception {
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   121
        int posTests = allPosTests, negTests = allNegTests;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   122
        if (verbosity >= 0 && (posTests | negTests) != 0) {
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   123
            System.out.println();
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   124
            if (posTests != 0)  System.out.println("=== "+posTests+" total positive test cases");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   125
            if (negTests != 0)  System.out.println("=== "+negTests+" total negative test cases");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   126
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   127
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   128
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   129
    static List<Object> calledLog = new ArrayList<>();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   130
    static Object logEntry(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   131
        return Arrays.asList(name, Arrays.asList(args));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   132
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   133
    public static Object called(String name, Object... args) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   134
        Object entry = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   135
        calledLog.add(entry);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   136
        return entry;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   137
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   138
    static void assertCalled(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   139
        Object expected = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   140
        Object actual   = calledLog.get(calledLog.size() - 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   141
        if (expected.equals(actual) && verbosity < 9)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   142
        System.out.println("assertCalled "+name+":");
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   143
        System.out.println("expected:   "+deepToString(expected));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   144
        System.out.println("actual:     "+actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   145
        System.out.println("ex. types:  "+getClasses(expected));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   146
        System.out.println("act. types: "+getClasses(actual));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   147
        assertEquals("previous method call", expected, actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   148
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   149
    static void printCalled(MethodHandle target, String name, Object... args) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   150
        if (verbosity >= 3)
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   151
            System.out.println("calling MH="+target+" to "+name+deepToString(args));
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   152
    }
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   153
    static String deepToString(Object x) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   154
        if (x == null)  return "null";
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   155
        if (x instanceof Collection)
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   156
            x = ((Collection)x).toArray();
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   157
        if (x instanceof Object[]) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   158
            Object[] ax = (Object[]) x;
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   159
            ax = Arrays.copyOf(ax, ax.length, Object[].class);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   160
            for (int i = 0; i < ax.length; i++)
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   161
                ax[i] = deepToString(ax[i]);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   162
            x = Arrays.deepToString(ax);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   163
        }
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   164
        if (x.getClass().isArray())
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   165
            try {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   166
                x = Arrays.class.getMethod("toString", x.getClass()).invoke(null, x);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   167
            } catch (ReflectiveOperationException ex) { throw new Error(ex); }
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   168
        assert(!(x instanceof Object[]));
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   169
        return x.toString();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   170
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   171
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   172
    static Object castToWrapper(Object value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   173
        Object wrap = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   174
        if (value instanceof Number)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   175
            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   176
        if (value instanceof Character)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   177
            wrap = castToWrapperOrNull((char)(Character)value, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   178
        if (wrap != null)  return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   179
        return dst.cast(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   180
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   181
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   182
    @SuppressWarnings("cast")  // primitive cast to (long) is part of the pattern
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   183
    static Object castToWrapperOrNull(long value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   184
        if (dst == int.class || dst == Integer.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   185
            return (int)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   186
        if (dst == long.class || dst == Long.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   187
            return (long)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   188
        if (dst == char.class || dst == Character.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   189
            return (char)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   190
        if (dst == short.class || dst == Short.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   191
            return (short)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   192
        if (dst == float.class || dst == Float.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   193
            return (float)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   194
        if (dst == double.class || dst == Double.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   195
            return (double)(value);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   196
        if (dst == byte.class || dst == Byte.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   197
            return (byte)(value);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   198
        if (dst == boolean.class || dst == boolean.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   199
            return ((value % 29) & 1) == 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   200
        return null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   201
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   202
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   203
    static final int ONE_MILLION = (1000*1000),  // first int value
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   204
                     TEN_BILLION = (10*1000*1000*1000),  // scale factor to reach upper 32 bits
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   205
                     INITIAL_ARG_VAL = ONE_MILLION << 1;  // <<1 makes space for sign bit;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   206
    static long nextArgVal;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   207
    static long nextArg(boolean moreBits) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   208
        long val = nextArgVal++;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   209
        long sign = -(val & 1); // alternate signs
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   210
        val >>= 1;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   211
        if (moreBits)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   212
            // Guarantee some bits in the high word.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   213
            // In any case keep the decimal representation simple-looking,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   214
            // with lots of zeroes, so as not to make the printed decimal
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   215
            // strings unnecessarily noisy.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   216
            val += (val % ONE_MILLION) * TEN_BILLION;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   217
        return val ^ sign;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   218
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   219
    static int nextArg() {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   220
        // Produce a 32-bit result something like ONE_MILLION+(smallint).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   221
        // Example: 1_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   222
        return (int) nextArg(false);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   223
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   224
    static long nextArg(Class<?> kind) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   225
        if (kind == long.class   || kind == Long.class ||
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   226
            kind == double.class || kind == Double.class)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   227
            // produce a 64-bit result something like
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   228
            // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   229
            // Example: 10_000_420_001_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   230
            return nextArg(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   231
        return (long) nextArg();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   232
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   233
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   234
    static Object randomArg(Class<?> param) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   235
        Object wrap = castToWrapperOrNull(nextArg(param), param);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   236
        if (wrap != null) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   237
            return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   238
        }
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   239
//        import sun.invoke.util.Wrapper;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   240
//        Wrapper wrap = Wrapper.forBasicType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   241
//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   242
//            wrap = Wrapper.forWrapperType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   243
//        if (wrap != Wrapper.OBJECT)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   244
//            return wrap.wrap(nextArg++);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   245
        if (param.isInterface()) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   246
            for (Class<?> c : param.getClasses()) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   247
                if (param.isAssignableFrom(c) && !c.isInterface())
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   248
                    { param = c; break; }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   249
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   250
        }
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   251
        if (param.isArray()) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   252
            Class<?> ctype = param.getComponentType();
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   253
            Object arg = Array.newInstance(ctype, 2);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   254
            Array.set(arg, 0, randomArg(ctype));
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   255
            return arg;
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   256
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   257
        if (param.isInterface() && param.isAssignableFrom(List.class))
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   258
            return Arrays.asList("#"+nextArg());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   259
        if (param.isInterface() || param.isAssignableFrom(String.class))
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   260
            return "#"+nextArg();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   261
        else
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   262
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   263
                return param.newInstance();
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   264
            } catch (InstantiationException | IllegalAccessException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   265
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   266
        return null;  // random class not Object, String, Integer, etc.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   267
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   268
    static Object[] randomArgs(Class<?>... params) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   269
        Object[] args = new Object[params.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   270
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   271
            args[i] = randomArg(params[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   272
        return args;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   273
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   274
    static Object[] randomArgs(int nargs, Class<?> param) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   275
        Object[] args = new Object[nargs];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   276
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   277
            args[i] = randomArg(param);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   278
        return args;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   279
    }
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   280
    static Object[] randomArgs(List<Class<?>> params) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   281
        return randomArgs(params.toArray(new Class<?>[params.size()]));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   282
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   283
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   284
    @SafeVarargs @SuppressWarnings("varargs")
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   285
    static <T, E extends T> T[] array(Class<T[]> atype, E... a) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   286
        return Arrays.copyOf(a, a.length, atype);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   287
    }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   288
    @SafeVarargs @SuppressWarnings("varargs")
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   289
    static <T> T[] cat(T[] a, T... b) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   290
        int alen = a.length, blen = b.length;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   291
        if (blen == 0)  return a;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   292
        T[] c = Arrays.copyOf(a, alen + blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   293
        System.arraycopy(b, 0, c, alen, blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   294
        return c;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   295
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   296
    static Integer[] boxAll(int... vx) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   297
        Integer[] res = new Integer[vx.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   298
        for (int i = 0; i < res.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   299
            res[i] = vx[i];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   300
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   301
        return res;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   302
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   303
    static Object getClasses(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   304
        if (x == null)  return x;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   305
        if (x instanceof String)  return x;  // keep the name
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   306
        if (x instanceof List) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   307
            // recursively report classes of the list elements
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   308
            Object[] xa = ((List)x).toArray();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   309
            for (int i = 0; i < xa.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   310
                xa[i] = getClasses(xa[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   311
            return Arrays.asList(xa);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   312
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   313
        return x.getClass().getSimpleName();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   314
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   315
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   316
    /** Return lambda(arg...[arity]) { new Object[]{ arg... } } */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   317
    static MethodHandle varargsList(int arity) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   318
        return ValueConversions.varargsList(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   319
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   320
    /** Return lambda(arg...[arity]) { Arrays.asList(arg...) } */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   321
    static MethodHandle varargsArray(int arity) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   322
        return ValueConversions.varargsArray(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   323
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   324
    static MethodHandle varargsArray(Class<?> arrayType, int arity) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   325
        return ValueConversions.varargsArray(arrayType, arity);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   326
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   327
    /** Variation of varargsList, but with the given rtype. */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   328
    static MethodHandle varargsList(int arity, Class<?> rtype) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   329
        MethodHandle list = varargsList(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   330
        MethodType listType = list.type().changeReturnType(rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   331
        if (List.class.isAssignableFrom(rtype) || rtype == void.class || rtype == Object.class) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   332
            // OK
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   333
        } else if (rtype.isAssignableFrom(String.class)) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   334
            if (LIST_TO_STRING == null)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   335
                try {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   336
                    LIST_TO_STRING = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToString",
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   337
                                                        MethodType.methodType(String.class, List.class));
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   338
                } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   339
            list = MethodHandles.filterReturnValue(list, LIST_TO_STRING);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   340
        } else if (rtype.isPrimitive()) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   341
            if (LIST_TO_INT == null)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   342
                try {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   343
                    LIST_TO_INT = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToInt",
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   344
                                                     MethodType.methodType(int.class, List.class));
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   345
                } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   346
            list = MethodHandles.filterReturnValue(list, LIST_TO_INT);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   347
            list = MethodHandles.explicitCastArguments(list, listType);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   348
        } else {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   349
            throw new RuntimeException("varargsList: "+rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   350
        }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   351
        return list.asType(listType);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   352
    }
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   353
    /** Variation of varargsList, but with the given ptypes and rtype. */
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   354
    static MethodHandle varargsList(List<Class<?>> ptypes, Class<?> rtype) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   355
        MethodHandle list = varargsList(ptypes.size(), rtype);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   356
        return list.asType(MethodType.methodType(rtype, ptypes));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   357
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   358
    private static MethodHandle LIST_TO_STRING, LIST_TO_INT;
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   359
    private static String listToString(List<?> x) { return x.toString(); }
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   360
    private static int listToInt(List<?> x) { return x.toString().hashCode(); }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   361
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   362
    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   363
        return changeArgTypes(target, 0, 999, argType);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   364
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   365
    static MethodHandle changeArgTypes(MethodHandle target,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   366
            int beg, int end, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   367
        MethodType targetType = target.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   368
        end = Math.min(end, targetType.parameterCount());
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   369
        ArrayList<Class<?>> argTypes = new ArrayList<>(targetType.parameterList());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   370
        Collections.fill(argTypes.subList(beg, end), argType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   371
        MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   372
        return target.asType(ttype2);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   373
    }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   374
    static MethodHandle addTrailingArgs(MethodHandle target, int nargs, Class<?> argClass) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   375
        int targetLen = target.type().parameterCount();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   376
        int extra = (nargs - targetLen);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   377
        if (extra <= 0)  return target;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   378
        List<Class<?>> fakeArgs = Collections.<Class<?>>nCopies(extra, argClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   379
        return MethodHandles.dropArguments(target, targetLen, fakeArgs);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   380
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   381
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   382
    // This lookup is good for all members in and under MethodHandlesTest.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   383
    static final Lookup PRIVATE = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   384
    // This lookup is good for package-private members but not private ones.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   385
    static final Lookup PACKAGE = PackageSibling.lookup();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   386
    // This lookup is good for public members and protected members of PubExample
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   387
    static final Lookup SUBCLASS = RemoteExample.lookup();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   388
    // This lookup is good only for public members.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   389
    static final Lookup PUBLIC  = MethodHandles.publicLookup();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   390
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   391
    // Subject methods...
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   392
    static class Example implements IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   393
        final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   394
        public Example() { name = "Example#"+nextArg(); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   395
        protected Example(String name) { this.name = name; }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   396
        @SuppressWarnings("LeakingThisInConstructor")
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   397
        protected Example(int x) { this(); called("protected <init>", this, x); }
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   398
        //Example(Void x) { does not exist; lookup elicts NoSuchMethodException }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   399
        @Override public String toString() { return name; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   400
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   401
        public void            v0()     { called("v0", this); }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   402
        protected void         pro_v0() { called("pro_v0", this); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   403
        void                   pkg_v0() { called("pkg_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   404
        private void           pri_v0() { called("pri_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   405
        public static void     s0()     { called("s0"); }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   406
        protected static void  pro_s0() { called("pro_s0"); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   407
        static void            pkg_s0() { called("pkg_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   408
        private static void    pri_s0() { called("pri_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   409
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   410
        public Object          v1(Object x) { return called("v1", this, x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   411
        public Object          v2(Object x, Object y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   412
        public Object          v2(Object x, int    y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   413
        public Object          v2(int    x, Object y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   414
        public Object          v2(int    x, int    y) { return called("v2", this, x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   415
        public static Object   s1(Object x) { return called("s1", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   416
        public static Object   s2(int x)    { return called("s2", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   417
        public static Object   s3(long x)   { return called("s3", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   418
        public static Object   s4(int x, int y) { return called("s4", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   419
        public static Object   s5(long x, int y) { return called("s5", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   420
        public static Object   s6(int x, long y) { return called("s6", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   421
        public static Object   s7(float x, double y) { return called("s7", x, y); }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   422
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   423
        // for testing findConstructor:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   424
        public Example(String x, int y) { this.name = x+y; called("Example.<init>", x, y); }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   425
        public Example(int x, String y) { this.name = x+y; called("Example.<init>", x, y); }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   426
        public Example(int x, int    y) { this.name = x+""+y; called("Example.<init>", x, y); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   427
        public Example(int x, long   y) { this.name = x+""+y; called("Example.<init>", x, y); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   428
        public Example(int x, float  y) { this.name = x+""+y; called("Example.<init>", x, y); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   429
        public Example(int x, double y) { this.name = x+""+y; called("Example.<init>", x, y); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   430
        public Example(int x, int    y, int z) { this.name = x+""+y+""+z; called("Example.<init>", x, y, z); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   431
        public Example(int x, int    y, int z, int a) { this.name = x+""+y+""+z+""+a; called("Example.<init>", x, y, z, a); }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   432
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   433
        static final Lookup EXAMPLE = MethodHandles.lookup();  // for testing findSpecial
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   434
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   435
    static final Lookup EXAMPLE = Example.EXAMPLE;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   436
    public static class PubExample extends Example {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   437
        public PubExample() { this("PubExample"); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   438
        protected PubExample(String prefix) { super(prefix+"#"+nextArg()); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   439
        protected void         pro_v0() { called("Pub/pro_v0", this); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   440
        protected static void  pro_s0() { called("Pub/pro_s0"); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   441
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   442
    static class SubExample extends Example {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   443
        @Override public void  v0()     { called("Sub/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   444
        @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   445
        @SuppressWarnings("LeakingThisInConstructor")
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   446
        private      SubExample(int x)  { called("<init>", this, x); }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   447
        public SubExample() { super("SubExample#"+nextArg()); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   448
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   449
    public static interface IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   450
        public void            v0();
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   451
        public static class Impl implements IntExample {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   452
            public void        v0()     { called("Int/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   453
            final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   454
            public Impl() { name = "Impl#"+nextArg(); }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   455
            @Override public String toString() { return name; }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   456
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   457
    }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   458
    static interface SubIntExample extends IntExample { }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   459
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   460
    static final Object[][][] ACCESS_CASES = {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   461
        { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   462
        { { false, PUBLIC }, { false, SUBCLASS }, { false, PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[1]: only PRIVATE
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   463
        { { false, PUBLIC }, { false, SUBCLASS }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[2]: PUBLIC false
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   464
        { { false, PUBLIC }, { true,  SUBCLASS }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[3]: subclass OK
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   465
        { { true,  PUBLIC }, { true,  SUBCLASS }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[4]: all true
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   466
    };
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   467
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   468
    static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   469
        Object[][] cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   470
        if (name.contains("pri_") || isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   471
            cases = ACCESS_CASES[1]; // PRIVATE only
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   472
        } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   473
            cases = ACCESS_CASES[2]; // not PUBLIC
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   474
        } else if (name.contains("pro_")) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   475
            cases = ACCESS_CASES[3]; // PUBLIC class, protected member
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   476
        } else {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   477
            assertTrue(name.indexOf('_') < 0 || name.contains("fin_"));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   478
            boolean pubc = Modifier.isPublic(defc.getModifiers());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   479
            if (pubc)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   480
                cases = ACCESS_CASES[4]; // all access levels
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   481
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   482
                cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   483
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   484
        if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   485
            cases = Arrays.copyOfRange(cases, 0, cases.length-1);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   486
        return cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   487
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   488
    static Object[][] accessCases(Class<?> defc, String name) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   489
        return accessCases(defc, name, false);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   490
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   491
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   492
    static Lookup maybeMoveIn(Lookup lookup, Class<?> defc) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   493
        if (lookup == PUBLIC || lookup == SUBCLASS || lookup == PACKAGE)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   494
            // external views stay external
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   495
            return lookup;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   496
        return lookup.in(defc);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   497
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   498
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20533
diff changeset
   499
    /** Is findVirtual (etc.) of "&lt;init&lt;" supposed to elicit a NoSuchMethodException? */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31178
diff changeset
   500
    static final boolean INIT_REF_CAUSES_NSME = true;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   501
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   502
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   503
    public void testFindStatic() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   504
        CodeCacheOverflowProcessor.runMHTest(this::testFindStatic0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   505
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   506
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   507
    public void testFindStatic0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   508
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   509
        startTest("findStatic");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   510
        testFindStatic(PubExample.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   511
        testFindStatic(Example.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   512
        testFindStatic(Example.class, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   513
        testFindStatic(Example.class, void.class, "pri_s0");
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   514
        testFindStatic(Example.class, void.class, "pro_s0");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   515
        testFindStatic(PubExample.class, void.class, "Pub/pro_s0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   516
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   517
        testFindStatic(Example.class, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   518
        testFindStatic(Example.class, Object.class, "s2", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   519
        testFindStatic(Example.class, Object.class, "s3", long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   520
        testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   521
        testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   522
        testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   523
        testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   524
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   525
        testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   526
        testFindStatic(false, PRIVATE, Example.class, void.class, "<init>", int.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   527
        testFindStatic(false, PRIVATE, Example.class, void.class, "<init>", Void.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   528
        testFindStatic(false, PRIVATE, Example.class, void.class, "v0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   529
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   530
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   531
    void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   532
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   533
            testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   534
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   535
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   536
    void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   537
        testFindStatic(true, lookup, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   538
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   539
    void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   540
        countTest(positive);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   542
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   543
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   544
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   545
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   546
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   547
            target = maybeMoveIn(lookup, defc).findStatic(defc, methodName, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   548
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   549
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   550
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   551
                (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   552
                ?   NoSuchMethodException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   553
                :   IllegalAccessException.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   554
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   555
            if (verbosity >= 5)  ex.printStackTrace(System.out);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   556
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   557
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   558
            System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   559
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   560
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   561
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   562
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   563
        assertEquals(type, target.type());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   564
        assertNameStringContains(target, methodName);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   565
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   566
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   567
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   568
        assertCalled(name, args);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   569
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   570
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   571
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   572
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   573
    static void assertExceptionClass(Class<? extends Throwable> expected,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   574
                                     Throwable actual) {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   575
        if (expected.isInstance(actual))  return;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   576
        actual.printStackTrace();
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   577
        assertEquals(expected, actual.getClass());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   578
    }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   579
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   580
    static final boolean DEBUG_METHOD_HANDLE_NAMES = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   581
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   582
    // rough check of name string
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   583
    static void assertNameStringContains(MethodHandle x, String s) {
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   584
        if (!DEBUG_METHOD_HANDLE_NAMES) {
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   585
            // ignore s
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   586
            assertEquals("MethodHandle"+x.type(), x.toString());
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   587
            return;
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   588
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   589
        if (x.toString().contains(s))  return;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   590
        assertEquals(s, x);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   591
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   592
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   593
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   594
    public void testFindVirtual() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   595
        CodeCacheOverflowProcessor.runMHTest(this::testFindVirtual0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   596
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   597
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   598
    public void testFindVirtual0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   599
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   600
        startTest("findVirtual");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   601
        testFindVirtual(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   602
        testFindVirtual(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   603
        testFindVirtual(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   604
        testFindVirtual(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   605
        testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   606
        testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   607
        testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   608
        testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   609
        testFindVirtual(Example.class, void.class, "pro_v0");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   610
        testFindVirtual(PubExample.class, void.class, "Pub/pro_v0");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   611
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   612
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   613
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "<init>", int.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   614
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "<init>", Void.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   615
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "s0");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   616
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   617
        // test dispatch
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   618
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   619
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   620
        testFindVirtual(SubExample.class,      IntExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   621
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   622
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   623
        testFindVirtual(Example.class,         IntExample.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   624
        testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   625
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   626
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   627
    @Test
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   628
    public void testFindVirtualClone() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   629
        CodeCacheOverflowProcessor.runMHTest(this::testFindVirtualClone0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   630
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   631
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   632
    public void testFindVirtualClone0() throws Throwable {
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   633
        // test some ad hoc system methods
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   634
        testFindVirtual(false, PUBLIC, Object.class, Object.class, "clone");
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   635
        testFindVirtual(true, PUBLIC, Object[].class, Object.class, "clone");
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   636
        testFindVirtual(true, PUBLIC, int[].class, Object.class, "clone");
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   637
        for (Class<?> cls : new Class<?>[]{ boolean[].class, long[].class, float[].class, char[].class })
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   638
            testFindVirtual(true, PUBLIC, cls, Object.class, "clone");
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   639
    }
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   640
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   641
    void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   642
        Class<?> rcvc = defc;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   643
        testFindVirtual(rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   644
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   645
    void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   646
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   647
            testFindVirtual((Boolean)ac[0], (Lookup)ac[1], rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   648
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   649
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   650
    void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   651
        testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   652
    }
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   653
    void testFindVirtual(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   654
        testFindVirtual(positive, lookup, defc, defc, ret, name, params);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   655
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   656
    void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   657
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   658
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   659
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   660
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   661
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   662
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   663
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   664
            target = maybeMoveIn(lookup, defc).findVirtual(defc, methodName, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   665
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   666
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   667
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   668
                (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   669
                ?   NoSuchMethodException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   670
                :   IllegalAccessException.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   671
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   672
            if (verbosity >= 5)  ex.printStackTrace(System.out);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   673
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   674
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   675
            System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   676
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   677
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   678
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   679
        if (!positive)  return; // negative test failed as expected
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   680
        Class<?> selfc = defc;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   681
        // predict receiver type narrowing:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   682
        if (lookup == SUBCLASS &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   683
                name.contains("pro_") &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   684
                selfc.isAssignableFrom(lookup.lookupClass())) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   685
            selfc = lookup.lookupClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   686
            if (name.startsWith("Pub/"))  name = "Rem/"+name.substring(4);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   687
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   688
        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)selfc), params);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   689
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   690
        assertEquals(typeWithSelf, target.type());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   691
        assertNameStringContains(target, methodName);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   692
        Object[] argsWithSelf = randomArgs(paramsWithSelf);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   693
        if (selfc.isAssignableFrom(rcvc) && rcvc != selfc)  argsWithSelf[0] = randomArg(rcvc);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   694
        printCalled(target, name, argsWithSelf);
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   695
        Object res = target.invokeWithArguments(argsWithSelf);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   696
        if (Example.class.isAssignableFrom(defc) || IntExample.class.isAssignableFrom(defc)) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   697
            assertCalled(name, argsWithSelf);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   698
        } else if (name.equals("clone")) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   699
            // Ad hoc method call outside Example.  For Object[].clone.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   700
            printCalled(target, name, argsWithSelf);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   701
            assertEquals(MethodType.methodType(Object.class, rcvc), target.type());
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   702
            Object orig = argsWithSelf[0];
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   703
            assertEquals(orig.getClass(), res.getClass());
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   704
            if (res instanceof Object[])
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   705
                assertArrayEquals((Object[])res, (Object[])argsWithSelf[0]);
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   706
            assert(Arrays.deepEquals(new Object[]{res}, new Object[]{argsWithSelf[0]}));
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   707
        } else {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   708
            assert(false) : Arrays.asList(positive, lookup, rcvc, defc, ret, name, deepToString(params));
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 14408
diff changeset
   709
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   710
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   711
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   712
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   713
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   714
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   715
    public void testFindSpecial() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   716
        CodeCacheOverflowProcessor.runMHTest(this::testFindSpecial0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   717
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   718
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   719
    public void testFindSpecial0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   720
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   721
        startTest("findSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   722
        testFindSpecial(SubExample.class, Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   723
        testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   724
        testFindSpecial(RemoteExample.class, PubExample.class, void.class, "Pub/pro_v0");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   725
        // Do some negative testing:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   726
        for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   727
            testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   728
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "bogus");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   729
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   730
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", Void.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   731
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   732
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   733
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   734
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   735
    void testFindSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   736
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   737
        if (specialCaller == RemoteExample.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   738
            testFindSpecial(false, EXAMPLE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   739
            testFindSpecial(false, PRIVATE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   740
            testFindSpecial(false, PACKAGE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   741
            testFindSpecial(true,  SUBCLASS, specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   742
            testFindSpecial(false, PUBLIC,   specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   743
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   744
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   745
        testFindSpecial(true,  EXAMPLE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   746
        testFindSpecial(true,  PRIVATE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   747
        testFindSpecial(false, PACKAGE,  specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   748
        testFindSpecial(false, SUBCLASS, specialCaller, defc, ret, name, params);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   749
        testFindSpecial(false, PUBLIC,   specialCaller, defc, ret, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   750
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   751
    void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   752
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   753
        countTest(positive);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   754
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   755
        MethodType type = MethodType.methodType(ret, params);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   756
        Lookup specialLookup = maybeMoveIn(lookup, specialCaller);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   757
        boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   758
                                   (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   759
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   760
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   761
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   762
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   763
            if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   764
            target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   765
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   766
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   767
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   768
                (!specialAccessOK)  // this check should happen first
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   769
                ?   IllegalAccessException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   770
                : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   771
                ?   NoSuchMethodException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   772
                : IllegalAccessException.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   773
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   774
            if (verbosity >= 5)  ex.printStackTrace(System.out);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   775
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   776
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   777
            System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   778
                               +(target == null ? "" : target.type())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   779
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   780
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   781
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   782
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   783
        assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   784
        assertEquals(type,          target.type().dropParameterTypes(0,1));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   785
        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   786
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   787
        assertNameStringContains(target, methodName);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   788
        Object[] args = randomArgs(paramsWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   789
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   790
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   791
        assertCalled(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   792
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   793
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   794
    @Test
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   795
    public void testFindConstructor() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   796
        CodeCacheOverflowProcessor.runMHTest(this::testFindConstructor0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   797
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   798
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   799
    public void testFindConstructor0() throws Throwable {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   800
        if (CAN_SKIP_WORKING)  return;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   801
        startTest("findConstructor");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   802
        testFindConstructor(true, EXAMPLE, Example.class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   803
        testFindConstructor(true, EXAMPLE, Example.class, int.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   804
        testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   805
        testFindConstructor(true, EXAMPLE, Example.class, int.class, long.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   806
        testFindConstructor(true, EXAMPLE, Example.class, int.class, float.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   807
        testFindConstructor(true, EXAMPLE, Example.class, int.class, double.class);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   808
        testFindConstructor(true, EXAMPLE, Example.class, String.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   809
        testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class, int.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   810
        testFindConstructor(true, EXAMPLE, Example.class, int.class, int.class, int.class, int.class);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   811
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   812
    void testFindConstructor(boolean positive, Lookup lookup,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   813
                             Class<?> defc, Class<?>... params) throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   814
        countTest(positive);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   815
        MethodType type = MethodType.methodType(void.class, params);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   816
        MethodHandle target = null;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   817
        Exception noAccess = null;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   818
        try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   819
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   820
            target = lookup.findConstructor(defc, type);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   821
        } catch (ReflectiveOperationException ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   822
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   823
            assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   824
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   825
        if (verbosity >= 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   826
            System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   827
                               +(target == null ? "" : target.type())
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   828
                               +(noAccess == null ? "" : " !! "+noAccess));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   829
        if (positive && noAccess != null)  throw noAccess;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   830
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   831
        if (!positive)  return; // negative test failed as expected
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   832
        assertEquals(type.changeReturnType(defc), target.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   833
        Object[] args = randomArgs(params);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   834
        printCalled(target, defc.getSimpleName(), args);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   835
        Object obj = target.invokeWithArguments(args);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   836
        if (!(defc == Example.class && params.length < 2))
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   837
            assertCalled(defc.getSimpleName()+".<init>", args);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   838
        assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   839
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   840
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
   841
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   842
    public void testBind() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   843
        CodeCacheOverflowProcessor.runMHTest(this::testBind0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   844
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   845
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   846
    public void testBind0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   847
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   848
        startTest("bind");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   849
        testBind(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   850
        testBind(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   851
        testBind(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   852
        testBind(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   853
        testBind(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   854
        testBind(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   855
        testBind(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   856
        testBind(Example.class, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   857
        testBind(false, PRIVATE, Example.class, void.class, "bogus");
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   858
        testBind(false, PRIVATE, Example.class, void.class, "<init>", int.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   859
        testBind(false, PRIVATE, Example.class, void.class, "<init>", Void.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   860
        testBind(SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   861
        testBind(SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   862
        testBind(IntExample.Impl.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   863
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   864
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   865
    void testBind(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   866
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   867
            testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   868
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   869
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   870
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   871
    void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   872
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   873
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   874
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   875
        Object receiver = randomArg(defc);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   876
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   877
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   878
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   879
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   880
            target = maybeMoveIn(lookup, defc).bind(receiver, methodName, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   881
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   882
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   883
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   884
                (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   885
                ?   NoSuchMethodException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   886
                :   IllegalAccessException.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   887
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   888
            if (verbosity >= 5)  ex.printStackTrace(System.out);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   889
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   890
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   891
            System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   892
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   893
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   894
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   895
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   896
        assertEquals(type, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   897
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   898
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   899
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   900
        Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   901
        assertCalled(name, argsWithReceiver);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   902
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   903
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   904
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   905
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   906
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   907
    public void testUnreflect() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   908
        CodeCacheOverflowProcessor.runMHTest(this::testUnreflect0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   909
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   910
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
   911
    public void testUnreflect0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   912
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   913
        startTest("unreflect");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   914
        testUnreflect(Example.class, true, void.class, "s0");
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   915
        testUnreflect(Example.class, true, void.class, "pro_s0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   916
        testUnreflect(Example.class, true, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   917
        testUnreflect(Example.class, true, void.class, "pri_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   918
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   919
        testUnreflect(Example.class, true, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   920
        testUnreflect(Example.class, true, Object.class, "s2", int.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   921
        testUnreflect(Example.class, true, Object.class, "s3", long.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   922
        testUnreflect(Example.class, true, Object.class, "s4", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   923
        testUnreflect(Example.class, true, Object.class, "s5", long.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   924
        testUnreflect(Example.class, true, Object.class, "s6", int.class, long.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   925
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   926
        testUnreflect(Example.class, false, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   927
        testUnreflect(Example.class, false, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   928
        testUnreflect(Example.class, false, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   929
        testUnreflect(Example.class, false, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   930
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   931
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   932
        testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   933
        testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   934
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   935
        // Test a public final member in another package:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   936
        testUnreflect(RemoteExample.class, false, void.class, "Rem/fin_v0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   937
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   938
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   939
    void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   940
        for (Object[] ac : accessCases(defc, name)) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   941
            testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   942
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   943
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   944
    void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   945
        for (Object[] ac : accessCases(defc, name)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   946
            testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   947
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   948
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   949
    void testUnreflectMaybeSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   950
                                   boolean positive, Lookup lookup,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   951
                                   Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   952
        countTest(positive);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   953
        String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   954
        MethodType type = MethodType.methodType(ret, params);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   955
        Lookup specialLookup = (specialCaller != null ? maybeMoveIn(lookup, specialCaller) : null);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   956
        boolean specialAccessOK = (specialCaller != null &&
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   957
                                   specialLookup.lookupClass() == specialCaller &&
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   958
                                   (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   959
        Method rmethod = defc.getDeclaredMethod(methodName, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   960
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   961
        Exception noAccess = null;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   962
        boolean isStatic = (rcvc == null);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   963
        boolean isSpecial = (specialCaller != null);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   964
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   965
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   966
            if (isSpecial)
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   967
                target = specialLookup.unreflectSpecial(rmethod, specialCaller);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   968
            else
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   969
                target = maybeMoveIn(lookup, defc).unreflect(rmethod);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   970
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   971
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   972
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   973
                IllegalAccessException.class,  // NSME is impossible, since it was already reflected
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
   974
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   975
            if (verbosity >= 5)  ex.printStackTrace(System.out);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   976
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   977
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   978
            System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   979
                               +(!isSpecial ? "" : " specialCaller="+specialCaller)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   980
                               +( isStatic  ? "" : " receiver="+rcvc)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   981
                               +" => "+target
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   982
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   983
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   984
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   985
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   986
        assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   987
        Class<?>[] paramsMaybeWithSelf = params;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   988
        if (!isStatic) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   989
            paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   990
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   991
        MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   992
        if (isStatic) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   993
            assertEquals(typeMaybeWithSelf, target.type());
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   994
        } else {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   995
            if (isSpecial)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   996
                assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   997
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   998
                assertEquals(defc, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   999
            assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1000
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1001
        Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1002
        printCalled(target, name, argsMaybeWithSelf);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1003
        target.invokeWithArguments(argsMaybeWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1004
        assertCalled(name, argsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1005
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1006
            System.out.print(':');
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1007
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1008
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1009
    void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1010
        for (Object[] ac : accessCases(defc, name, true)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1011
            Class<?> specialCaller = rcvc;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1012
            testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1013
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1014
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1015
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1016
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1017
    public void testUnreflectSpecial() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1018
        CodeCacheOverflowProcessor.runMHTest(this::testUnreflectSpecial0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1019
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1020
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1021
    public void testUnreflectSpecial0() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1022
        if (CAN_SKIP_WORKING)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1023
        startTest("unreflectSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1024
        testUnreflectSpecial(Example.class,    Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1025
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1026
        testUnreflectSpecial(Example.class,    Example.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1027
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1028
        testUnreflectSpecial(Example.class,    Example.class, Object.class, "v2", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1029
        testUnreflectSpecial(Example.class, SubExample.class, Object.class, "v2", int.class, int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1030
        testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0");
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1031
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1032
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1033
    public static class HasFields {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1034
        boolean fZ = false;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1035
        byte fB = (byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1036
        short fS = (short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1037
        char fC = 'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1038
        int fI = 'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1039
        long fJ = 'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1040
        float fF = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1041
        double fD = 'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1042
        static boolean sZ = true;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1043
        static byte sB = 1+(byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1044
        static short sS = 1+(short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1045
        static char sC = 1+'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1046
        static int sI = 1+'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1047
        static long sJ = 1+'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1048
        static float sF = 1+'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1049
        static double sD = 1+'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1050
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1051
        Object fL = 'L';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1052
        String fR = "R";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1053
        static Object sL = 'M';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1054
        static String sR = "S";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1055
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1056
        static final Object[][] CASES;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1057
        static {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1058
            ArrayList<Object[]> cases = new ArrayList<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1059
            Object types[][] = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1060
                {'L',Object.class}, {'R',String.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1061
                {'I',int.class}, {'J',long.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1062
                {'F',float.class}, {'D',double.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1063
                {'Z',boolean.class}, {'B',byte.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1064
                {'S',short.class}, {'C',char.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1065
            };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1066
            HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1067
            for (Object[] t : types) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1068
                for (int kind = 0; kind <= 1; kind++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1069
                    boolean isStatic = (kind != 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1070
                    char btc = (Character)t[0];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1071
                    String name = (isStatic ? "s" : "f") + btc;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1072
                    Class<?> type = (Class<?>) t[1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1073
                    Object value;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1074
                    Field field;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1075
                        try {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1076
                        field = HasFields.class.getDeclaredField(name);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1077
                    } catch (NoSuchFieldException | SecurityException ex) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1078
                        throw new InternalError("no field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1079
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1080
                    try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1081
                        value = field.get(fields);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1082
                    } catch (IllegalArgumentException | IllegalAccessException ex) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1083
                        throw new InternalError("cannot fetch field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1084
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1085
                    if (type == float.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1086
                        float v = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1087
                        if (isStatic)  v++;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1088
                        assertTrue(value.equals(v));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1089
                    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1090
                    assertTrue(name.equals(field.getName()));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1091
                    assertTrue(type.equals(field.getType()));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1092
                    assertTrue(isStatic == (Modifier.isStatic(field.getModifiers())));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1093
                    cases.add(new Object[]{ field, value });
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1094
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1095
            }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1096
            cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1097
            cases.add(new Object[]{ new Object[]{ true,  HasFields.class, "bogus_sL", Object.class }, Error.class });
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1098
            CASES = cases.toArray(new Object[0][]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1099
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1100
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1101
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1102
    static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10, TEST_BOUND = 0x20, TEST_NPE = 0x40;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1103
    static boolean testModeMatches(int testMode, boolean isStatic) {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1104
        switch (testMode) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1105
        case TEST_FIND_STATIC:          return isStatic;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1106
        case TEST_FIND_FIELD:           return !isStatic;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1107
        case TEST_UNREFLECT:            return true;  // unreflect matches both
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1108
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1109
        throw new InternalError("testMode="+testMode);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1110
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1111
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1112
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1113
    public void testUnreflectGetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1114
        CodeCacheOverflowProcessor.runMHTest(this::testUnreflectGetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1115
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1116
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1117
    public void testUnreflectGetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1118
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1119
        startTest("unreflectGetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1120
        testGetter(TEST_UNREFLECT);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1121
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1122
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1123
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1124
    public void testFindGetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1125
        CodeCacheOverflowProcessor.runMHTest(this::testFindGetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1126
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1127
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1128
    public void testFindGetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1129
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1130
        startTest("findGetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1131
        testGetter(TEST_FIND_FIELD);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1132
        testGetter(TEST_FIND_FIELD | TEST_BOUND);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1133
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1134
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1135
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1136
    public void testFindStaticGetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1137
        CodeCacheOverflowProcessor.runMHTest(this::testFindStaticGetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1138
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1139
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1140
    public void testFindStaticGetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1141
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1142
        startTest("findStaticGetter");
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1143
        testGetter(TEST_FIND_STATIC);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1144
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1145
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1146
    public void testGetter(int testMode) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1147
        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1148
        for (Object[] c : HasFields.CASES) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1149
            boolean positive = (c[1] != Error.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1150
            testGetter(positive, lookup, c[0], c[1], testMode);
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1151
            if (positive)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1152
                testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1153
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1154
        testGetter(true, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1155
                   new Object[]{ true,  System.class, "out", java.io.PrintStream.class },
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1156
                   System.out, testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1157
        for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1158
            testGetter(false, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1159
                       new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1160
                       null, testMode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1161
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1162
    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1163
    public void testGetter(boolean positive, MethodHandles.Lookup lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1164
                           Object fieldRef, Object value, int testMode) throws Throwable {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1165
        testAccessor(positive, lookup, fieldRef, value, testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1166
    }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1167
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1168
    public void testAccessor(boolean positive0, MethodHandles.Lookup lookup,
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1169
                             Object fieldRef, Object value, int testMode0) throws Throwable {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1170
        if (verbosity >= 4)
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1171
            System.out.println("testAccessor"+Arrays.deepToString(new Object[]{positive0, lookup, fieldRef, value, testMode0}));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1172
        boolean isGetter = ((testMode0 & TEST_SETTER) == 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1173
        boolean doBound  = ((testMode0 & TEST_BOUND) != 0);
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1174
        boolean testNPE  = ((testMode0 & TEST_NPE) != 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1175
        int testMode = testMode0 & ~(TEST_SETTER | TEST_BOUND | TEST_NPE);
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1176
        boolean positive = positive0 && !testNPE;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1177
        boolean isStatic;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1178
        Class<?> fclass;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1179
        String   fname;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1180
        Class<?> ftype;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1181
        Field f = (fieldRef instanceof Field ? (Field)fieldRef : null);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1182
        if (f != null) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1183
            isStatic = Modifier.isStatic(f.getModifiers());
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1184
            fclass   = f.getDeclaringClass();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1185
            fname    = f.getName();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1186
            ftype    = f.getType();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1187
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1188
            Object[] scnt = (Object[]) fieldRef;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1189
            isStatic = (Boolean)  scnt[0];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1190
            fclass   = (Class<?>) scnt[1];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1191
            fname    = (String)   scnt[2];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1192
            ftype    = (Class<?>) scnt[3];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1193
            try {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1194
                f = fclass.getDeclaredField(fname);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1195
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1196
                f = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1197
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1198
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1199
        if (!testModeMatches(testMode, isStatic))  return;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1200
        if (f == null && testMode == TEST_UNREFLECT)  return;
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1201
        if (testNPE && isStatic)  return;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1202
        countTest(positive);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1203
        MethodType expType;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1204
        if (isGetter)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1205
            expType = MethodType.methodType(ftype, HasFields.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1206
        else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1207
            expType = MethodType.methodType(void.class, HasFields.class, ftype);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1208
        if (isStatic)  expType = expType.dropParameterTypes(0, 1);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1209
        Exception noAccess = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1210
        MethodHandle mh;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1211
        try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1212
            switch (testMode0 & ~(TEST_BOUND | TEST_NPE)) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1213
            case TEST_UNREFLECT:   mh = lookup.unreflectGetter(f);                      break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1214
            case TEST_FIND_FIELD:  mh = lookup.findGetter(fclass, fname, ftype);        break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1215
            case TEST_FIND_STATIC: mh = lookup.findStaticGetter(fclass, fname, ftype);  break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1216
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1217
                 TEST_UNREFLECT:   mh = lookup.unreflectSetter(f);                      break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1218
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1219
                 TEST_FIND_FIELD:  mh = lookup.findSetter(fclass, fname, ftype);        break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1220
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1221
                 TEST_FIND_STATIC: mh = lookup.findStaticSetter(fclass, fname, ftype);  break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1222
            default:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1223
                throw new InternalError("testMode="+testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1224
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1225
        } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1226
            mh = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1227
            noAccess = ex;
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
  1228
            assertExceptionClass(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
  1229
                (fname.contains("bogus"))
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
  1230
                ?   NoSuchFieldException.class
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
  1231
                :   IllegalAccessException.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20528
diff changeset
  1232
                noAccess);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1233
            if (verbosity >= 5)  ex.printStackTrace(System.out);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1234
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1235
        if (verbosity >= 3)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1236
            System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1237
                               +" => "+mh
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1238
                               +(noAccess == null ? "" : " !! "+noAccess));
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1239
        if (positive && !testNPE && noAccess != null)  throw new RuntimeException(noAccess);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1240
        assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1241
        if (!positive && !testNPE)  return; // negative access test failed as expected
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1242
        assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount());
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1243
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1244
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1245
        assertSame(mh.type(), expType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1246
        //assertNameStringContains(mh, fname);  // This does not hold anymore with LFs
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1247
        HasFields fields = new HasFields();
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1248
        HasFields fieldsForMH = fields;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1249
        if (testNPE)  fieldsForMH = null;  // perturb MH argument to elicit expected error
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1250
        if (doBound)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1251
            mh = mh.bindTo(fieldsForMH);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1252
        Object sawValue;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1253
        Class<?> vtype = ftype;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1254
        if (ftype != int.class)  vtype = Object.class;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1255
        if (isGetter) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1256
            mh = mh.asType(mh.type().generic()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1257
                           .changeReturnType(vtype));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1258
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1259
            int last = mh.type().parameterCount() - 1;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1260
            mh = mh.asType(mh.type().generic()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1261
                           .changeReturnType(void.class)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1262
                           .changeParameterType(last, vtype));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1263
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1264
        if (f != null && f.getDeclaringClass() == HasFields.class) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1265
            assertEquals(f.get(fields), value);  // clean to start with
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1266
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1267
        Throwable caughtEx = null;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1268
        if (isGetter) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1269
            Object expValue = value;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1270
            for (int i = 0; i <= 1; i++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1271
                sawValue = null;  // make DA rules happy under try/catch
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1272
                try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1273
                    if (isStatic || doBound) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1274
                        if (ftype == int.class)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1275
                            sawValue = (int) mh.invokeExact();  // do these exactly
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1276
                        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1277
                            sawValue = mh.invokeExact();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1278
                    } else {
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1279
                        if (ftype == int.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1280
                            sawValue = (int) mh.invokeExact((Object) fieldsForMH);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1281
                        else
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1282
                            sawValue = mh.invokeExact((Object) fieldsForMH);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1283
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1284
                } catch (RuntimeException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1285
                    if (ex instanceof NullPointerException && testNPE) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1286
                        caughtEx = ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1287
                        break;
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1288
                    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1289
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1290
                assertEquals(sawValue, expValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1291
                if (f != null && f.getDeclaringClass() == HasFields.class
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1292
                    && !Modifier.isFinal(f.getModifiers())) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1293
                    Object random = randomArg(ftype);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1294
                    f.set(fields, random);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1295
                    expValue = random;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1296
                } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1297
                    break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1298
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1299
            }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1300
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1301
            for (int i = 0; i <= 1; i++) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1302
                Object putValue = randomArg(ftype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1303
                try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1304
                    if (isStatic || doBound) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1305
                        if (ftype == int.class)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1306
                            mh.invokeExact((int)putValue);  // do these exactly
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1307
                        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1308
                            mh.invokeExact(putValue);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1309
                    } else {
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1310
                        if (ftype == int.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1311
                            mh.invokeExact((Object) fieldsForMH, (int)putValue);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1312
                        else
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1313
                            mh.invokeExact((Object) fieldsForMH, putValue);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1314
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1315
                } catch (RuntimeException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1316
                    if (ex instanceof NullPointerException && testNPE) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1317
                        caughtEx = ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1318
                        break;
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1319
                    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1320
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1321
                if (f != null && f.getDeclaringClass() == HasFields.class) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1322
                    assertEquals(f.get(fields), putValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1323
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1324
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1325
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1326
        if (f != null && f.getDeclaringClass() == HasFields.class) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1327
            f.set(fields, value);  // put it back
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1328
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1329
        if (testNPE) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1330
            if (caughtEx == null || !(caughtEx instanceof NullPointerException))
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1331
                throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1332
            caughtEx = null;  // nullify expected exception
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1333
        }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1334
        if (caughtEx != null) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1335
            throw new RuntimeException("unexpected exception", caughtEx);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1336
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1337
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1338
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1339
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1340
    public void testUnreflectSetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1341
        CodeCacheOverflowProcessor.runMHTest(this::testUnreflectSetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1342
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1343
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1344
    public void testUnreflectSetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1345
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1346
        startTest("unreflectSetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1347
        testSetter(TEST_UNREFLECT);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1348
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1349
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1350
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1351
    public void testFindSetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1352
        CodeCacheOverflowProcessor.runMHTest(this::testFindSetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1353
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1354
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1355
    public void testFindSetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1356
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1357
        startTest("findSetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1358
        testSetter(TEST_FIND_FIELD);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1359
        testSetter(TEST_FIND_FIELD | TEST_BOUND);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1360
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1361
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1362
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1363
    public void testFindStaticSetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1364
        CodeCacheOverflowProcessor.runMHTest(this::testFindStaticSetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1365
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1366
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1367
    public void testFindStaticSetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1368
        if (CAN_SKIP_WORKING)  return;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1369
        startTest("findStaticSetter");
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1370
        testSetter(TEST_FIND_STATIC);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1371
    }
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1372
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1373
    public void testSetter(int testMode) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1374
        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1375
        startTest("unreflectSetter");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1376
        for (Object[] c : HasFields.CASES) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1377
            boolean positive = (c[1] != Error.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1378
            testSetter(positive, lookup, c[0], c[1], testMode);
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1379
            if (positive)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1380
                testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1381
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1382
        for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1383
            testSetter(false, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1384
                       new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1385
                       null, testMode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1386
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1387
    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1388
    public void testSetter(boolean positive, MethodHandles.Lookup lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1389
                           Object fieldRef, Object value, int testMode) throws Throwable {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1390
        testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1391
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1392
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1393
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1394
    public void testArrayElementGetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1395
        CodeCacheOverflowProcessor.runMHTest(this::testArrayElementGetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1396
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1397
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1398
    public void testArrayElementGetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1399
        if (CAN_SKIP_WORKING)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1400
        startTest("arrayElementGetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1401
        testArrayElementGetterSetter(false);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1402
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1403
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1404
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1405
    public void testArrayElementSetter() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1406
        CodeCacheOverflowProcessor.runMHTest(this::testArrayElementSetter0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1407
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1408
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1409
    public void testArrayElementSetter0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1410
        if (CAN_SKIP_WORKING)  return;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1411
        startTest("arrayElementSetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1412
        testArrayElementGetterSetter(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1413
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1414
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1415
    private static final int TEST_ARRAY_NONE = 0, TEST_ARRAY_NPE = 1, TEST_ARRAY_OOB = 2, TEST_ARRAY_ASE = 3;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1416
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1417
    public void testArrayElementGetterSetter(boolean testSetter) throws Throwable {
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1418
        testArrayElementGetterSetter(testSetter, TEST_ARRAY_NONE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1419
    }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1421
    @Test
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1422
    public void testArrayElementErrors() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1423
        CodeCacheOverflowProcessor.runMHTest(this::testArrayElementErrors0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1424
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1425
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1426
    public void testArrayElementErrors0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1427
        if (CAN_SKIP_WORKING)  return;
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1428
        startTest("arrayElementErrors");
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1429
        testArrayElementGetterSetter(false, TEST_ARRAY_NPE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1430
        testArrayElementGetterSetter(true, TEST_ARRAY_NPE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1431
        testArrayElementGetterSetter(false, TEST_ARRAY_OOB);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1432
        testArrayElementGetterSetter(true, TEST_ARRAY_OOB);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1433
        testArrayElementGetterSetter(new Object[10], true, TEST_ARRAY_ASE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1434
        testArrayElementGetterSetter(new Example[10], true, TEST_ARRAY_ASE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1435
        testArrayElementGetterSetter(new IntExample[10], true, TEST_ARRAY_ASE);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1436
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1437
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1438
    public void testArrayElementGetterSetter(boolean testSetter, int negTest) throws Throwable {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1439
        testArrayElementGetterSetter(new String[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1440
        testArrayElementGetterSetter(new Iterable<?>[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1441
        testArrayElementGetterSetter(new Example[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1442
        testArrayElementGetterSetter(new IntExample[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1443
        testArrayElementGetterSetter(new Object[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1444
        testArrayElementGetterSetter(new boolean[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1445
        testArrayElementGetterSetter(new byte[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1446
        testArrayElementGetterSetter(new char[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1447
        testArrayElementGetterSetter(new short[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1448
        testArrayElementGetterSetter(new int[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1449
        testArrayElementGetterSetter(new float[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1450
        testArrayElementGetterSetter(new long[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1451
        testArrayElementGetterSetter(new double[10], testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1452
    }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1453
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1454
    public void testArrayElementGetterSetter(Object array, boolean testSetter, int negTest) throws Throwable {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1455
        boolean positive = (negTest == TEST_ARRAY_NONE);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1456
        int length = java.lang.reflect.Array.getLength(array);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1457
        Class<?> arrayType = array.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1458
        Class<?> elemType = arrayType.getComponentType();
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1459
        Object arrayToMH = array;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1460
        // this stanza allows negative tests to make argument perturbations:
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1461
        switch (negTest) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1462
        case TEST_ARRAY_NPE:
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1463
            arrayToMH = null;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1464
            break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1465
        case TEST_ARRAY_OOB:
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1466
            assert(length > 0);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1467
            arrayToMH = java.lang.reflect.Array.newInstance(elemType, 0);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1468
            break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1469
        case TEST_ARRAY_ASE:
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1470
            assert(testSetter && !elemType.isPrimitive());
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1471
            if (elemType == Object.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1472
                arrayToMH = new StringBuffer[length];  // very random subclass of Object!
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1473
            else if (elemType == Example.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1474
                arrayToMH = new SubExample[length];
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1475
            else if (elemType == IntExample.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1476
                arrayToMH = new SubIntExample[length];
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1477
            else
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1478
                return;  // can't make an ArrayStoreException test
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1479
            assert(arrayType.isInstance(arrayToMH))
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1480
                : Arrays.asList(arrayType, arrayToMH.getClass(), testSetter, negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1481
            break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1482
        }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1483
        countTest(positive);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1484
        if (verbosity > 2)  System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+length+"]"+(positive ? "" : " negative test #"+negTest+" using "+Arrays.deepToString(new Object[]{arrayToMH})));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1485
        MethodType expType = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1486
                ? MethodType.methodType(elemType,   arrayType, int.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1487
                : MethodType.methodType(void.class, arrayType, int.class, elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1488
        MethodHandle mh = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1489
                ? MethodHandles.arrayElementGetter(arrayType)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1490
                : MethodHandles.arrayElementSetter(arrayType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1491
        assertSame(mh.type(), expType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1492
        if (elemType != int.class && elemType != boolean.class) {
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1493
            MethodType gtype = mh.type().generic().changeParameterType(1, int.class);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1494
            if (testSetter)  gtype = gtype.changeReturnType(void.class);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1495
            mh = mh.asType(gtype);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1496
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1497
        Object sawValue, expValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1498
        List<Object> model = array2list(array);
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1499
        Throwable caughtEx = null;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1500
        for (int i = 0; i < length; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1501
            // update array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1502
            Object random = randomArg(elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1503
            model.set(i, random);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1504
            if (testSetter) {
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1505
                try {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1506
                    if (elemType == int.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1507
                        mh.invokeExact((int[]) arrayToMH, i, (int)random);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1508
                    else if (elemType == boolean.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1509
                        mh.invokeExact((boolean[]) arrayToMH, i, (boolean)random);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1510
                    else
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1511
                        mh.invokeExact(arrayToMH, i, random);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1512
                } catch (RuntimeException ex) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1513
                    caughtEx = ex;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1514
                    break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1515
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1516
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1517
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1518
                Array.set(array, i, random);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1519
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1520
            if (verbosity >= 5) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1521
                List<Object> array2list = array2list(array);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1522
                System.out.println("a["+i+"]="+random+" => "+array2list);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1523
                if (!array2list.equals(model))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1524
                    System.out.println("***   != "+model);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1525
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1526
            // observe array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1527
            sawValue = Array.get(array, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1528
            if (!testSetter) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1529
                expValue = sawValue;
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1530
                try {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1531
                    if (elemType == int.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1532
                        sawValue = (int) mh.invokeExact((int[]) arrayToMH, i);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1533
                    else if (elemType == boolean.class)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1534
                        sawValue = (boolean) mh.invokeExact((boolean[]) arrayToMH, i);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1535
                    else
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1536
                        sawValue = mh.invokeExact(arrayToMH, i);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1537
                } catch (RuntimeException ex) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1538
                    caughtEx = ex;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1539
                    break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1540
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1541
                assertEquals(sawValue, expValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1542
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1543
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1544
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1545
        if (!positive) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1546
            if (caughtEx == null)
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1547
                throw new RuntimeException("failed to catch exception for negTest="+negTest);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1548
            // test the kind of exception
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1549
            Class<?> reqType = null;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1550
            switch (negTest) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1551
            case TEST_ARRAY_ASE:  reqType = ArrayStoreException.class; break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1552
            case TEST_ARRAY_OOB:  reqType = ArrayIndexOutOfBoundsException.class; break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1553
            case TEST_ARRAY_NPE:  reqType = NullPointerException.class; break;
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1554
            default:              assert(false);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1555
            }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1556
            if (reqType.isInstance(caughtEx)) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1557
                caughtEx = null;  // nullify expected exception
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1558
            }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1559
        }
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1560
        if (caughtEx != null) {
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1561
            throw new RuntimeException("unexpected exception", caughtEx);
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
  1562
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1563
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1564
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1565
    List<Object> array2list(Object array) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1566
        int length = Array.getLength(array);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1567
        ArrayList<Object> model = new ArrayList<>(length);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1568
        for (int i = 0; i < length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1569
            model.add(Array.get(array, i));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1570
        return model;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1571
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1572
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1573
    static class Callee {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1574
        static Object id() { return called("id"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1575
        static Object id(Object x) { return called("id", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1576
        static Object id(Object x, Object y) { return called("id", x, y); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1577
        static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1578
        static Object id(Object... vx) { return called("id", vx); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1579
        static MethodHandle ofType(int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1580
            return ofType(Object.class, n);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1581
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1582
        static MethodHandle ofType(Class<?> rtype, int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1583
            if (n == -1)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1584
                return ofType(MethodType.methodType(rtype, Object[].class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1585
            return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1586
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1587
        static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1588
            return ofType(MethodType.methodType(rtype, ptypes));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1589
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1590
        static MethodHandle ofType(MethodType type) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1591
            Class<?> rtype = type.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1592
            String pfx = "";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1593
            if (rtype != Object.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1594
                pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1595
            String name = pfx+"id";
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1596
            try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1597
                return PRIVATE.findStatic(Callee.class, name, type);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1598
            } catch (NoSuchMethodException | IllegalAccessException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1599
                throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1600
            }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1601
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1602
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1603
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1604
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1605
    public void testConvertArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1606
        CodeCacheOverflowProcessor.runMHTest(this::testConvertArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1607
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1608
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1609
    public void testConvertArguments0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1610
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1611
        startTest("convertArguments");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1612
        testConvert(Callee.ofType(1), null, "id", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1613
        testConvert(Callee.ofType(1), null, "id", String.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1614
        testConvert(Callee.ofType(1), null, "id", Integer.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1615
        testConvert(Callee.ofType(1), null, "id", short.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1616
        testConvert(Callee.ofType(1), null, "id", char.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1617
        testConvert(Callee.ofType(1), null, "id", byte.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1618
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1619
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1620
    void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1621
        testConvert(true, id, rtype, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1622
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1623
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1624
    void testConvert(boolean positive,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1625
                     MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1626
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1627
        MethodType idType = id.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1628
        if (rtype == null)  rtype = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1629
        for (int i = 0; i < params.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1630
            if (params[i] == null)  params[i] = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1631
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1632
        // simulate the pairwise conversion
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1633
        MethodType newType = MethodType.methodType(rtype, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1634
        Object[] args = randomArgs(newType.parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1635
        Object[] convArgs = args.clone();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1636
        for (int i = 0; i < args.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1637
            Class<?> src = newType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1638
            Class<?> dst = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1639
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1640
                convArgs[i] = castToWrapper(convArgs[i], dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1641
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1642
        Object convResult = id.invokeWithArguments(convArgs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1643
        {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1644
            Class<?> dst = newType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1645
            Class<?> src = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1646
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1647
                convResult = castToWrapper(convResult, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1648
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1649
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1650
        RuntimeException error = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1651
        try {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1652
            target = id.asType(newType);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  1653
        } catch (WrongMethodTypeException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1654
            error = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1655
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1656
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1657
            System.out.println("convert "+id+ " to "+newType+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1658
                    +(error == null ? "" : " !! "+error));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1659
        if (positive && error != null)  throw error;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1660
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1661
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1662
        assertEquals(newType, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1663
        printCalled(target, id.toString(), args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1664
        Object result = target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1665
        assertCalled(name, convArgs);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1666
        assertEquals(convResult, result);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1667
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1668
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1669
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1670
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1671
    @Test
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1672
    public void testVarargsCollector() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1673
        CodeCacheOverflowProcessor.runMHTest(this::testVarargsCollector0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1674
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1675
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1676
    public void testVarargsCollector0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1677
        if (CAN_SKIP_WORKING)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1678
        startTest("varargsCollector");
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1679
        MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called",
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1680
                               MethodType.methodType(Object.class, String.class, Object[].class));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1681
        vac0 = vac0.bindTo("vac");
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1682
        MethodHandle vac = vac0.asVarargsCollector(Object[].class);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1683
        testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1684
        testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1685
        for (Class<?> at : new Class<?>[] { Object.class, String.class, Integer.class }) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1686
            testConvert(true, vac.asType(MethodType.genericMethodType(1)), null, "vac", at);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1687
            testConvert(true, vac.asType(MethodType.genericMethodType(2)), null, "vac", at, at);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1688
        }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1689
    }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1690
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1691
    @Test // SLOW
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1692
    public void testPermuteArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1693
        CodeCacheOverflowProcessor.runMHTest(this::testPermuteArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1694
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1695
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1696
    public void testPermuteArguments0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1697
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1698
        startTest("permuteArguments");
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1699
        testPermuteArguments(4, Integer.class,  2, long.class,    6);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1700
        if (CAN_TEST_LIGHTLY)  return;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1701
        testPermuteArguments(4, Integer.class,  2, String.class,  0);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1702
        testPermuteArguments(6, Integer.class,  0, null,         30);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1703
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1704
    public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1705
        if (verbosity >= 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1706
            System.out.println("permuteArguments "+max+"*"+type1.getName()
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1707
                    +(t2c==0?"":"/"+t2c+"*"+type2.getName())
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1708
                    +(dilution > 0 ? " with dilution "+dilution : ""));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1709
        int t2pos = t2c == 0 ? 0 : 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1710
        for (int inargs = t2pos+1; inargs <= max; inargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1711
            Class<?>[] types = new Class<?>[inargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1712
            Arrays.fill(types, type1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1713
            if (t2c != 0) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1714
                // Fill in a middle range with type2:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1715
                Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1716
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1717
            Object[] args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1718
            int numcases = 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1719
            for (int outargs = 0; outargs <= max; outargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1720
                if (outargs - inargs >= MAX_ARG_INCREASE)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1721
                int casStep = dilution + 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1722
                // Avoid some common factors:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1723
                while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1724
                       (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1725
                    casStep++;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1726
                testPermuteArguments(args, types, outargs, numcases, casStep);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1727
                numcases *= inargs;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1728
                if (CAN_TEST_LIGHTLY && outargs < max-2)  continue;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1729
                if (dilution > 10 && outargs >= 4) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1730
                    if (CAN_TEST_LIGHTLY)  continue;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1731
                    int[] reorder = new int[outargs];
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1732
                    // Do some special patterns, which we probably missed.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1733
                    // Replication of a single argument or argument pair.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1734
                    for (int i = 0; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1735
                        Arrays.fill(reorder, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1736
                        testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1737
                        for (int d = 1; d <= 2; d++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1738
                            if (i + d >= inargs)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1739
                            for (int j = 1; j < outargs; j += 2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1740
                                reorder[j] += 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1741
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1742
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1743
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1744
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1745
                    // Repetition of a sequence of 3 or more arguments.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1746
                    for (int i = 1; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1747
                        for (int len = 3; len <= inargs; len++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1748
                            for (int j = 0; j < outargs; j++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1749
                                reorder[j] = (i + (j % len)) % inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1750
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1751
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1752
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1753
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1754
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1755
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1756
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1757
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1758
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1759
    public void testPermuteArguments(Object[] args, Class<?>[] types,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1760
                                     int outargs, int numcases, int casStep) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1761
        int inargs = args.length;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1762
        int[] reorder = new int[outargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1763
        for (int cas = 0; cas < numcases; cas += casStep) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1764
            for (int i = 0, c = cas; i < outargs; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1765
                reorder[i] = c % inargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1766
                c /= inargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1767
            }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1768
            if (CAN_TEST_LIGHTLY && outargs >= 3 && (reorder[0] == reorder[1] || reorder[1] == reorder[2]))  continue;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1769
            testPermuteArguments(args, types, reorder);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1770
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1771
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1772
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1773
    static int[] reverse(int[] reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1774
        reorder = reorder.clone();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1775
        for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1776
            int j = reorder.length - 1 - i;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1777
            int tem = reorder[i];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1778
            reorder[i] = reorder[j];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1779
            reorder[j] = tem;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1780
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1781
        return reorder;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1782
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1783
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1784
    void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1785
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1786
        if (args == null && types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1787
            int max = 0;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1788
            for (int j : reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1789
                if (max < j)  max = j;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1790
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1791
            args = randomArgs(max+1, Integer.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1792
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1793
        if (args == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1794
            args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1795
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1796
        if (types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1797
            types = new Class<?>[args.length];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1798
            for (int i = 0; i < args.length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1799
                types[i] = args[i].getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1800
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1801
        int inargs = args.length, outargs = reorder.length;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1802
        assertTrue(inargs == types.length);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1803
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1804
            System.out.println("permuteArguments "+Arrays.toString(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1805
        Object[] permArgs = new Object[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1806
        Class<?>[] permTypes = new Class<?>[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1807
        for (int i = 0; i < outargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1808
            permArgs[i] = args[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1809
            permTypes[i] = types[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1810
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1811
        if (verbosity >= 4) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1812
            System.out.println("in args:   "+Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1813
            System.out.println("out args:  "+Arrays.asList(permArgs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1814
            System.out.println("in types:  "+Arrays.asList(types));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1815
            System.out.println("out types: "+Arrays.asList(permTypes));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1816
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1817
        MethodType inType  = MethodType.methodType(Object.class, types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1818
        MethodType outType = MethodType.methodType(Object.class, permTypes);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1819
        MethodHandle target = varargsList(outargs).asType(outType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1820
        MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1821
        if (verbosity >= 5)  System.out.println("newTarget = "+newTarget);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1822
        Object result = newTarget.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1823
        Object expected = Arrays.asList(permArgs);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1824
        if (!expected.equals(result)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1825
            System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+" types="+Arrays.asList(types));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1826
            System.out.println("in args:   "+Arrays.asList(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1827
            System.out.println("out args:  "+expected);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1828
            System.out.println("bad args:  "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1829
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1830
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1831
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1832
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1833
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1834
    @Test // SLOW
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1835
    public void testSpreadArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1836
        CodeCacheOverflowProcessor.runMHTest(this::testSpreadArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1837
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1838
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1839
    public void testSpreadArguments0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1840
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1841
        startTest("spreadArguments");
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1842
        for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1843
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1844
                System.out.println("spreadArguments "+argType);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1845
            for (int nargs = 0; nargs < 50; nargs++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1846
                if (CAN_TEST_LIGHTLY && nargs > 11)  break;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1847
                for (int pos = 0; pos <= nargs; pos++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1848
                    if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2)  continue;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1849
                    if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1850
                        continue;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1851
                    testSpreadArguments(argType, pos, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1852
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1853
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1854
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1855
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1856
    public void testSpreadArguments(Class<?> argType, int pos, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1857
        countTest();
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1858
        Class<?> arrayType = java.lang.reflect.Array.newInstance(argType, 0).getClass();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1859
        MethodHandle target2 = varargsArray(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1860
        MethodHandle target = target2.asType(target2.type().generic());
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1861
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1862
            System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1863
        Object[] args = randomArgs(target2.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1864
        // make sure the target does what we think it does:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1865
        if (pos == 0 && nargs < 5 && !argType.isPrimitive()) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1866
            Object[] check = (Object[]) target.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1867
            assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1868
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1869
                case 0:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1870
                    check = (Object[]) (Object) target.invokeExact();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1871
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1872
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1873
                case 1:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1874
                    check = (Object[]) (Object) target.invokeExact(args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1875
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1876
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1877
                case 2:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1878
                    check = (Object[]) (Object) target.invokeExact(args[0], args[1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1879
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1880
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1881
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1882
        }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1883
        List<Class<?>> newParams = new ArrayList<>(target2.type().parameterList());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1884
        {   // modify newParams in place
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1885
            List<Class<?>> spreadParams = newParams.subList(pos, nargs);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1886
            spreadParams.clear(); spreadParams.add(arrayType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1887
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1888
        MethodType newType = MethodType.methodType(arrayType, newParams);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1889
        MethodHandle result = target2.asSpreader(arrayType, nargs-pos);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1890
        assert(result.type() == newType) : Arrays.asList(result, newType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1891
        result = result.asType(newType.generic());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1892
        Object returnValue;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1893
        if (pos == 0) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1894
            Object args2 = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1895
            returnValue = result.invokeExact(args2);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1896
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1897
            Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1898
            args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1899
            returnValue = result.invokeWithArguments(args1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1900
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1901
        String argstr = Arrays.toString(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1902
        if (!argType.isPrimitive()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1903
            Object[] rv = (Object[]) returnValue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1904
            String rvs = Arrays.toString(rv);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1905
            if (!Arrays.equals(args, rv)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1906
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1907
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1908
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1909
                assertArrayEquals(args, rv);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1910
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1911
        } else if (argType == int.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1912
            String rvs = Arrays.toString((int[]) returnValue);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1913
            if (!argstr.equals(rvs)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1914
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1915
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1916
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1917
                assertEquals(argstr, rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1918
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1919
        } else if (argType == long.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1920
            String rvs = Arrays.toString((long[]) returnValue);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1921
            if (!argstr.equals(rvs)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1922
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1923
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1924
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1925
                assertEquals(argstr, rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1926
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1927
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1928
            // cannot test...
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1929
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1930
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1931
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1932
    @Test // SLOW
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  1933
    public void testAsCollector() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1934
        CodeCacheOverflowProcessor.runMHTest(this::testAsCollector0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1935
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1936
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1937
    public void testAsCollector0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1938
        if (CAN_SKIP_WORKING)  return;
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  1939
        startTest("asCollector");
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  1940
        for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1941
            if (verbosity >= 3)
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  1942
                System.out.println("asCollector "+argType);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1943
            for (int nargs = 0; nargs < 50; nargs++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1944
                if (CAN_TEST_LIGHTLY && nargs > 11)  break;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1945
                for (int pos = 0; pos <= nargs; pos++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1946
                    if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2)  continue;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1947
                    if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1948
                        continue;
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  1949
                    testAsCollector(argType, pos, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1950
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1951
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1952
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1953
    }
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  1954
    public void testAsCollector(Class<?> argType, int pos, int nargs) throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1955
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1956
        // fake up a MH with the same type as the desired adapter:
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1957
        MethodHandle fake = varargsArray(nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1958
        fake = changeArgTypes(fake, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1959
        MethodType newType = fake.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1960
        Object[] args = randomArgs(newType.parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1961
        // here is what should happen:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1962
        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1963
        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1964
        // here is the MH which will witness the collected argument tail:
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1965
        MethodHandle target = varargsArray(pos+1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1966
        target = changeArgTypes(target, 0, pos, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1967
        target = changeArgTypes(target, pos, pos+1, Object[].class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1968
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1969
            System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1970
        MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1971
        Object[] returnValue = (Object[]) result.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1972
//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1973
//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1974
//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1975
        assertArrayEquals(collectedArgs, returnValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1976
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1977
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1978
    @Test // SLOW
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1979
    public void testInsertArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1980
        CodeCacheOverflowProcessor.runMHTest(this::testInsertArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1981
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1982
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  1983
    public void testInsertArguments0() throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1984
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1985
        startTest("insertArguments");
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1986
        for (int nargs = 0; nargs < 50; nargs++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1987
            if (CAN_TEST_LIGHTLY && nargs > 11)  break;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1988
            for (int ins = 0; ins <= nargs; ins++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1989
                if (nargs > 10 && ins > 4 && ins < nargs-4 && ins % 10 != 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1990
                    continue;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1991
                for (int pos = 0; pos <= nargs; pos++) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1992
                    if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1993
                        continue;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  1994
                    if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2)  continue;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1995
                    testInsertArguments(nargs, pos, ins);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1996
                }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1997
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1998
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1999
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2000
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2001
    void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2002
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2003
        MethodHandle target = varargsArray(nargs + ins);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2004
        Object[] args = randomArgs(target.type().parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2005
        List<Object> resList = Arrays.asList(args);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2006
        List<Object> argsToPass = new ArrayList<>(resList);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2007
        List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2008
        if (verbosity >= 3)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2009
            System.out.println("insert: "+argsToInsert+" @"+pos+" into "+target);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2010
        @SuppressWarnings("cast")  // cast to spread Object... is helpful
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2011
        MethodHandle target2 = MethodHandles.insertArguments(target, pos,
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2012
                (Object[]/*...*/) argsToInsert.toArray());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2013
        argsToInsert.clear();  // remove from argsToInsert
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2014
        Object res2 = target2.invokeWithArguments(argsToPass);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2015
        Object res2List = Arrays.asList((Object[])res2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2016
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2017
            System.out.println("result: "+res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2018
        //if (!resList.equals(res2List))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2019
        //    System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2020
        assertEquals(resList, res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2021
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2022
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2023
    @Test
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2024
    public void testFilterReturnValue() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2025
        CodeCacheOverflowProcessor.runMHTest(this::testFilterReturnValue0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2026
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2027
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2028
    public void testFilterReturnValue0() throws Throwable {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2029
        if (CAN_SKIP_WORKING)  return;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2030
        startTest("filterReturnValue");
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2031
        Class<?> classOfVCList = varargsList(1).invokeWithArguments(0).getClass();
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2032
        assertTrue(List.class.isAssignableFrom(classOfVCList));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2033
        for (int nargs = 0; nargs <= 3; nargs++) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2034
            for (Class<?> rtype : new Class<?>[] { Object.class,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2035
                                                List.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2036
                                                int.class,
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2037
                                                byte.class,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2038
                                                long.class,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2039
                                                CharSequence.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2040
                                                String.class }) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2041
                testFilterReturnValue(nargs, rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2042
            }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2043
        }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2044
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2045
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2046
    void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2047
        countTest();
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2048
        MethodHandle target = varargsList(nargs, rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2049
        MethodHandle filter;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2050
        if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class))
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2051
            filter = varargsList(1);  // add another layer of list-ness
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2052
        else
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2053
            filter = MethodHandles.identity(rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2054
        filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2055
        Object[] argsToPass = randomArgs(nargs, Object.class);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2056
        if (verbosity >= 3)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2057
            System.out.println("filter "+target+" to "+rtype.getSimpleName()+" with "+filter);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2058
        MethodHandle target2 = MethodHandles.filterReturnValue(target, filter);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2059
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2060
            System.out.println("filtered target: "+target2);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2061
        // Simulate expected effect of filter on return value:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2062
        Object unfiltered = target.invokeWithArguments(argsToPass);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2063
        Object expected = filter.invokeWithArguments(unfiltered);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2064
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2065
            System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2066
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2067
            System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2068
        Object result = target2.invokeWithArguments(argsToPass);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2069
        if (verbosity >= 3)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2070
            System.out.println("result: "+result+" : "+result.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2071
        if (!expected.equals(result))
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2072
            System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2073
        assertEquals(expected, result);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2074
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2075
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2076
    @Test
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2077
    public void testFilterArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2078
        CodeCacheOverflowProcessor.runMHTest(this::testFilterArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2079
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2080
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2081
    public void testFilterArguments0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2082
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2083
        startTest("filterArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2084
        for (int nargs = 1; nargs <= 6; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2085
            for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2086
                testFilterArguments(nargs, pos);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2087
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2088
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2089
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2090
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2091
    void testFilterArguments(int nargs, int pos) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2092
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2093
        MethodHandle target = varargsList(nargs);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2094
        MethodHandle filter = varargsList(1);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2095
        filter = filter.asType(filter.type().generic());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2096
        Object[] argsToPass = randomArgs(nargs, Object.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2097
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2098
            System.out.println("filter "+target+" at "+pos+" with "+filter);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2099
        MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2100
        // Simulate expected effect of filter on arglist:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2101
        Object[] filteredArgs = argsToPass.clone();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2102
        filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2103
        List<Object> expected = Arrays.asList(filteredArgs);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2104
        Object result = target2.invokeWithArguments(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2105
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2106
            System.out.println("result: "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2107
        if (!expected.equals(result))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2108
            System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2109
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2110
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2111
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2112
    @Test
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2113
    public void testCollectArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2114
        CodeCacheOverflowProcessor.runMHTest(this::testCollectArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2115
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2116
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2117
    public void testCollectArguments0() throws Throwable {
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2118
        if (CAN_SKIP_WORKING)  return;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2119
        startTest("collectArguments");
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2120
        testFoldOrCollectArguments(true);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2121
    }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2122
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2123
    @Test
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2124
    public void testFoldArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2125
        CodeCacheOverflowProcessor.runMHTest(this::testFoldArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2126
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2127
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2128
    public void testFoldArguments0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2129
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2130
        startTest("foldArguments");
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2131
        testFoldOrCollectArguments(false);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2132
    }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2133
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2134
    void testFoldOrCollectArguments(boolean isCollect) throws Throwable {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2135
        for (Class<?> lastType : new Class<?>[]{ Object.class, String.class, int.class }) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2136
            for (Class<?> collectType : new Class<?>[]{ Object.class, String.class, int.class, void.class }) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2137
                int maxArity = 10;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2138
                if (collectType != String.class)  maxArity = 5;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2139
                if (lastType != Object.class)  maxArity = 4;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2140
                for (int nargs = 0; nargs <= maxArity; nargs++) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2141
                    ArrayList<Class<?>> argTypes = new ArrayList<>(Collections.nCopies(nargs, Object.class));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2142
                    int maxMix = 20;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2143
                    if (collectType != Object.class)  maxMix = 0;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2144
                    Map<Object,Integer> argTypesSeen = new HashMap<>();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2145
                    for (int mix = 0; mix <= maxMix; mix++) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2146
                        if (!mixArgs(argTypes, mix, argTypesSeen))  continue;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2147
                        for (int collect = 0; collect <= nargs; collect++) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2148
                            for (int pos = 0; pos <= nargs - collect; pos++) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2149
                                testFoldOrCollectArguments(argTypes, pos, collect, collectType, lastType, isCollect);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2150
                            }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2151
                        }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2152
                    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2153
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2154
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2155
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2156
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2157
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2158
    boolean mixArgs(List<Class<?>> argTypes, int mix, Map<Object,Integer> argTypesSeen) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2159
        assert(mix >= 0);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2160
        if (mix == 0)  return true;  // no change
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2161
        if ((mix >>> argTypes.size()) != 0)  return false;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2162
        for (int i = 0; i < argTypes.size(); i++) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2163
            if (i >= 31)  break;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2164
            boolean bit = (mix & (1 << i)) != 0;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2165
            if (bit) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2166
                Class<?> type = argTypes.get(i);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2167
                if (type == Object.class)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2168
                    type = String.class;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2169
                else if (type == String.class)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2170
                    type = int.class;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2171
                else
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2172
                    type = Object.class;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2173
                argTypes.set(i, type);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2174
            }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2175
        }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2176
        Integer prev = argTypesSeen.put(new ArrayList<>(argTypes), mix);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2177
        if (prev != null) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2178
            if (verbosity >= 4)  System.out.println("mix "+prev+" repeated "+mix+": "+argTypes);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2179
            return false;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2180
        }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2181
        if (verbosity >= 3)  System.out.println("mix "+mix+" = "+argTypes);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2182
        return true;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2183
    }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2184
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2185
    void testFoldOrCollectArguments(List<Class<?>> argTypes,  // argument types minus the inserted combineType
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2186
                                    int pos, int fold, // position and length of the folded arguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2187
                                    Class<?> combineType, // type returned from the combiner
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2188
                                    Class<?> lastType,  // type returned from the target
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2189
                                    boolean isCollect) throws Throwable {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2190
        int nargs = argTypes.size();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2191
        if (pos != 0 && !isCollect)  return;  // can fold only at pos=0 for now
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2192
        countTest();
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2193
        List<Class<?>> combineArgTypes = argTypes.subList(pos, pos + fold);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2194
        List<Class<?>> targetArgTypes = new ArrayList<>(argTypes);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2195
        if (isCollect)  // does targret see arg[pos..pos+cc-1]?
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2196
            targetArgTypes.subList(pos, pos + fold).clear();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2197
        if (combineType != void.class)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2198
            targetArgTypes.add(pos, combineType);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2199
        MethodHandle target = varargsList(targetArgTypes, lastType);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2200
        MethodHandle combine = varargsList(combineArgTypes, combineType);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2201
        List<Object> argsToPass = Arrays.asList(randomArgs(argTypes));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2202
        if (verbosity >= 3)
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2203
            System.out.println((isCollect ? "collect" : "fold")+" "+target+" with "+combine);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2204
        MethodHandle target2;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2205
        if (isCollect)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2206
            target2 = MethodHandles.collectArguments(target, pos, combine);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2207
        else
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2208
            target2 = MethodHandles.foldArguments(target, combine);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2209
        // Simulate expected effect of combiner on arglist:
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2210
        List<Object> expectedList = new ArrayList<>(argsToPass);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2211
        List<Object> argsToFold = expectedList.subList(pos, pos + fold);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2212
        if (verbosity >= 3)
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2213
            System.out.println((isCollect ? "collect" : "fold")+": "+argsToFold+" into "+target2);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2214
        Object foldedArgs = combine.invokeWithArguments(argsToFold);
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2215
        if (isCollect)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2216
            argsToFold.clear();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2217
        if (combineType != void.class)
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2218
            argsToFold.add(0, foldedArgs);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2219
        Object result = target2.invokeWithArguments(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2220
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2221
            System.out.println("result: "+result);
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
  2222
        Object expected = target.invokeWithArguments(expectedList);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2223
        if (!expected.equals(result))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2224
            System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result+" != "+expected);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2225
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2226
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2227
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2228
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2229
    public void testDropArguments() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2230
        CodeCacheOverflowProcessor.runMHTest(this::testDropArguments0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2231
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2232
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2233
    public void testDropArguments0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2234
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2235
        startTest("dropArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2236
        for (int nargs = 0; nargs <= 4; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2237
            for (int drop = 1; drop <= 4; drop++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2238
                for (int pos = 0; pos <= nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2239
                    testDropArguments(nargs, pos, drop);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2240
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2241
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2242
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2243
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2244
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2245
    void testDropArguments(int nargs, int pos, int drop) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2246
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2247
        MethodHandle target = varargsArray(nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2248
        Object[] args = randomArgs(target.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2249
        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2250
                Collections.nCopies(drop, Object.class).toArray(new Class<?>[0]));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2251
        List<Object> resList = Arrays.asList(args);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2252
        List<Object> argsToDrop = new ArrayList<>(resList);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2253
        for (int i = drop; i > 0; i--) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2254
            argsToDrop.add(pos, "blort#"+i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2255
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2256
        Object res2 = target2.invokeWithArguments(argsToDrop);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2257
        Object res2List = Arrays.asList((Object[])res2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2258
        //if (!resList.equals(res2List))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2259
        //    System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2260
        assertEquals(resList, res2List);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2261
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2262
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2263
    @Test  // SLOW
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2264
    public void testInvokers() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2265
        CodeCacheOverflowProcessor.runMHTest(this::testInvokers0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2266
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2267
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2268
    public void testInvokers0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2269
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2270
        startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2271
        // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2272
        Set<MethodType> done = new HashSet<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2273
        for (int i = 0; i <= 6; i++) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2274
            if (CAN_TEST_LIGHTLY && i > 3)  break;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2275
            MethodType gtype = MethodType.genericMethodType(i);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2276
            for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2277
                for (int j = -1; j < i; j++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2278
                    MethodType type = gtype;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2279
                    if (j < 0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2280
                        type = type.changeReturnType(argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2281
                    else if (argType == void.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2282
                        continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2283
                    else
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2284
                        type = type.changeParameterType(j, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2285
                    if (done.add(type))
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2286
                        testInvokersWithCatch(type);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2287
                    MethodType vtype = type.changeReturnType(void.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2288
                    if (done.add(vtype))
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2289
                        testInvokersWithCatch(vtype);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2290
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2291
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2292
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2293
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2294
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2295
    public void testInvokersWithCatch(MethodType type) throws Throwable {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2296
        try {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2297
            testInvokers(type);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2298
        } catch (Throwable ex) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2299
            System.out.println("*** testInvokers on "+type+" => ");
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2300
            ex.printStackTrace(System.out);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2301
        }
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 23594
diff changeset
  2302
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2303
    public void testInvokers(MethodType type) throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2304
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2305
            System.out.println("test invokers for "+type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2306
        int nargs = type.parameterCount();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2307
        boolean testRetCode = type.returnType() != void.class;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2308
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2309
                                MethodType.genericMethodType(0, true));
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2310
        assertTrue(target.isVarargsCollector());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2311
        target = target.asType(type);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2312
        Object[] args = randomArgs(type.parameterArray());
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2313
        List<Object> targetPlusArgs = new ArrayList<>(Arrays.asList(args));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2314
        targetPlusArgs.add(0, target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2315
        int code = (Integer) invokee(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2316
        Object log = logEntry("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2317
        assertEquals(log.hashCode(), code);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2318
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2319
        MethodHandle inv;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2320
        Object result;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2321
        // exact invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2322
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2323
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2324
        inv = MethodHandles.exactInvoker(type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2325
        result = inv.invokeWithArguments(targetPlusArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2326
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2327
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2328
        // generic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2329
        countTest();
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
  2330
        inv = MethodHandles.invoker(type);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2331
        if (nargs <= 3 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2332
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2333
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2334
            case 0:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2335
                result = inv.invokeExact(target);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2336
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2337
            case 1:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2338
                result = inv.invokeExact(target, args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2339
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2340
            case 2:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2341
                result = inv.invokeExact(target, args[0], args[1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2342
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2343
            case 3:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2344
                result = inv.invokeExact(target, args[0], args[1], args[2]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2345
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2346
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2347
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2348
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2349
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2350
        calledLog.clear();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2351
        result = inv.invokeWithArguments(targetPlusArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2352
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2353
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2354
        // varargs invoker #0
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2355
        calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2356
        inv = MethodHandles.spreadInvoker(type, 0);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2357
        if (type.returnType() == Object.class) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2358
            result = inv.invokeExact(target, args);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2359
        } else if (type.returnType() == void.class) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2360
            result = null; inv.invokeExact(target, args);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2361
        } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2362
            result = inv.invokeWithArguments(target, (Object) args);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2363
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2364
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2365
        assertCalled("invokee", args);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2366
        if (nargs >= 1 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2367
            // varargs invoker #1
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2368
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2369
            inv = MethodHandles.spreadInvoker(type, 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2370
            result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2371
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2372
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2373
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2374
        if (nargs >= 2 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2375
            // varargs invoker #2
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2376
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2377
            inv = MethodHandles.spreadInvoker(type, 2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2378
            result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2379
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2380
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2381
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2382
        if (nargs >= 3 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2383
            // varargs invoker #3
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2384
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2385
            inv = MethodHandles.spreadInvoker(type, 3);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2386
            result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2387
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2388
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2389
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2390
        for (int k = 0; k <= nargs; k++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2391
            // varargs invoker #0..N
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2392
            if (CAN_TEST_LIGHTLY && (k > 1 || k < nargs - 1))  continue;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2393
            countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2394
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2395
            inv = MethodHandles.spreadInvoker(type, k);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2396
            MethodType expType = (type.dropParameterTypes(k, nargs)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2397
                                  .appendParameterTypes(Object[].class)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2398
                                  .insertParameterTypes(0, MethodHandle.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2399
            assertEquals(expType, inv.type());
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2400
            List<Object> targetPlusVarArgs = new ArrayList<>(targetPlusArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2401
            List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2402
            Object[] tail = tailList.toArray();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2403
            tailList.clear(); tailList.add(tail);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2404
            result = inv.invokeWithArguments(targetPlusVarArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2405
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2406
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2407
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2408
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2409
        // dynamic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2410
        countTest();
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  2411
        CallSite site = new MutableCallSite(type);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  2412
        inv = site.dynamicInvoker();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2413
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2414
        // see if we get the result of the original target:
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2415
        try {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2416
            result = inv.invokeWithArguments(args);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2417
            assertTrue("should not reach here", false);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2418
        } catch (IllegalStateException ex) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2419
            String msg = ex.getMessage();
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2420
            assertTrue(msg, msg.contains("site"));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2421
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2422
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2423
        // set new target after invoker is created, to make sure we track target
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2424
        site.setTarget(target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2425
        calledLog.clear();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2426
        result = inv.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2427
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2428
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2429
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2430
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2431
    static Object invokee(Object... args) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2432
        return called("invokee", args).hashCode();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2433
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2434
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2435
    private static final String MISSING_ARG = "missingArg";
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2436
    private static final String MISSING_ARG_2 = "missingArg#2";
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2437
    static Object targetIfEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2438
        return called("targetIfEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2439
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2440
    static Object fallbackIfNotEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2441
        return called("fallbackIfNotEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2442
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2443
    static Object targetIfEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2444
        assertEquals(x, MISSING_ARG);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2445
        return called("targetIfEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2446
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2447
    static Object fallbackIfNotEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2448
        assertFalse(x.toString(), x.equals(MISSING_ARG));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2449
        return called("fallbackIfNotEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2450
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2451
    static Object targetIfEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2452
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2453
        return called("targetIfEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2454
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2455
    static Object fallbackIfNotEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2456
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2457
        return called("fallbackIfNotEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2458
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2459
    static Object targetIfEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2460
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2461
        return called("targetIfEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2462
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2463
    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2464
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2465
        return called("fallbackIfNotEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2466
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2467
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2468
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2469
    public void testGuardWithTest() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2470
        CodeCacheOverflowProcessor.runMHTest(this::testGuardWithTest0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2471
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2472
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2473
    public void testGuardWithTest0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2474
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2475
        startTest("guardWithTest");
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2476
        for (int nargs = 0; nargs <= 50; nargs++) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2477
            if (CAN_TEST_LIGHTLY && nargs > 7)  break;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2478
            testGuardWithTest(nargs, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2479
            testGuardWithTest(nargs, String.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2480
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2481
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2482
    void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2483
        testGuardWithTest(nargs, 0, argClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2484
        if (nargs <= 5 || nargs % 10 == 3) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2485
            for (int testDrops = 1; testDrops <= nargs; testDrops++)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2486
                testGuardWithTest(nargs, testDrops, argClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2487
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2488
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2489
    void testGuardWithTest(int nargs, int testDrops, Class<?> argClass) throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2490
        countTest();
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2491
        int nargs1 = Math.min(3, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2492
        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2493
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs1));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2494
        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs1));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2495
        while (test.type().parameterCount() > nargs)
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2496
            // 0: test = constant(MISSING_ARG.equals(MISSING_ARG))
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2497
            // 1: test = lambda (_) MISSING_ARG.equals(_)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2498
            test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2499
        if (argClass != Object.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2500
            test = changeArgTypes(test, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2501
            target = changeArgTypes(target, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2502
            fallback = changeArgTypes(fallback, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2503
        }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2504
        int testArgs = nargs - testDrops;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2505
        assert(testArgs >= 0);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2506
        test = addTrailingArgs(test, Math.min(testArgs, nargs), argClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2507
        target = addTrailingArgs(target, nargs, argClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2508
        fallback = addTrailingArgs(fallback, nargs, argClass);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2509
        Object[][] argLists = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2510
            { },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2511
            { "foo" }, { MISSING_ARG },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2512
            { "foo", "foo" }, { "foo", "bar" },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2513
            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2514
        };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2515
        for (Object[] argList : argLists) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2516
            Object[] argList1 = argList;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2517
            if (argList.length != nargs) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2518
                if (argList.length != nargs1)  continue;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2519
                argList1 = Arrays.copyOf(argList, nargs);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2520
                Arrays.fill(argList1, nargs1, nargs, MISSING_ARG_2);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2521
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2522
            MethodHandle test1 = test;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2523
            if (test1.type().parameterCount() > testArgs) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2524
                int pc = test1.type().parameterCount();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2525
                test1 = MethodHandles.insertArguments(test, testArgs, Arrays.copyOfRange(argList1, testArgs, pc));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2526
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2527
            MethodHandle mh = MethodHandles.guardWithTest(test1, target, fallback);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2528
            assertEquals(target.type(), mh.type());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2529
            boolean equals;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2530
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2531
            case 0:   equals = true; break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2532
            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2533
            default:  equals = argList[0].equals(argList[1]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2534
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2535
            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2536
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2537
                System.out.println(logEntry(willCall, argList));
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2538
            Object result = mh.invokeWithArguments(argList1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2539
            assertCalled(willCall, argList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2540
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2541
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2542
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2543
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2544
    public void testThrowException() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2545
        CodeCacheOverflowProcessor.runMHTest(this::testThrowException0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2546
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2547
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2548
    public void testThrowException0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2549
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2550
        startTest("throwException");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2551
        testThrowException(int.class, new ClassCastException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2552
        testThrowException(void.class, new java.io.IOException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2553
        testThrowException(String.class, new LinkageError("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2554
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2555
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2556
    void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2557
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2558
        Class<? extends Throwable> exType = thrown.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2559
        MethodHandle target = MethodHandles.throwException(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2560
        //System.out.println("throwing with "+target+" : "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2561
        MethodType expectedType = MethodType.methodType(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2562
        assertEquals(expectedType, target.type());
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2563
        target = target.asType(target.type().generic());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2564
        Throwable caught = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2565
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2566
            Object res = target.invokeExact((Object) thrown);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2567
            fail("got "+res+" instead of throwing "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2568
        } catch (Throwable ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2569
            if (ex != thrown) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2570
                if (ex instanceof Error)  throw (Error)ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2571
                if (ex instanceof RuntimeException)  throw (RuntimeException)ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2572
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2573
            caught = ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2574
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2575
        assertSame(thrown, caught);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2576
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2577
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2578
    @Test
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2579
    public void testInterfaceCast() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2580
        CodeCacheOverflowProcessor.runMHTest(this::testInterfaceCast0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2581
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2582
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2583
    public void testInterfaceCast0() throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2584
        //if (CAN_SKIP_WORKING)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2585
        startTest("interfaceCast");
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2586
        assert( (((Object)"foo") instanceof CharSequence));
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2587
        assert(!(((Object)"foo") instanceof Iterable));
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2588
        for (MethodHandle mh : new MethodHandle[]{
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2589
            MethodHandles.identity(String.class),
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2590
            MethodHandles.identity(CharSequence.class),
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2591
            MethodHandles.identity(Iterable.class)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2592
        }) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2593
            if (verbosity > 0)  System.out.println("-- mh = "+mh);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2594
            for (Class<?> ctype : new Class<?>[]{
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2595
                Object.class, String.class, CharSequence.class,
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2596
                Number.class, Iterable.class
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2597
            }) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2598
                if (verbosity > 0)  System.out.println("---- ctype = "+ctype.getName());
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2599
                //                           doret  docast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2600
                testInterfaceCast(mh, ctype, false, false);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2601
                testInterfaceCast(mh, ctype, true,  false);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2602
                testInterfaceCast(mh, ctype, false, true);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2603
                testInterfaceCast(mh, ctype, true,  true);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2604
            }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2605
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2606
    }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2607
    private static Class<?> i2o(Class<?> c) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2608
        return (c.isInterface() ? Object.class : c);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2609
    }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2610
    public void testInterfaceCast(MethodHandle mh, Class<?> ctype,
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2611
                                                   boolean doret, boolean docast) throws Throwable {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2612
        MethodHandle mh0 = mh;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2613
        if (verbosity > 1)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2614
            System.out.println("mh="+mh+", ctype="+ctype.getName()+", doret="+doret+", docast="+docast);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2615
        String normalRetVal = "normal return value";
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2616
        MethodType mt = mh.type();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2617
        MethodType mt0 = mt;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2618
        if (doret)  mt = mt.changeReturnType(ctype);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2619
        else        mt = mt.changeParameterType(0, ctype);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2620
        if (docast) mh = MethodHandles.explicitCastArguments(mh, mt);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2621
        else        mh = mh.asType(mt);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2622
        assertEquals(mt, mh.type());
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2623
        MethodType mt1 = mt;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2624
        // this bit is needed to make the interface types disappear for invokeWithArguments:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2625
        mh = MethodHandles.explicitCastArguments(mh, mt.generic());
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2626
        Class<?>[] step = {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2627
            mt1.parameterType(0),  // param as passed to mh at first
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2628
            mt0.parameterType(0),  // param after incoming cast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2629
            mt0.returnType(),      // return value before cast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2630
            mt1.returnType(),      // return value after outgoing cast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2631
        };
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2632
        // where might a checkCast occur?
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2633
        boolean[] checkCast = new boolean[step.length];
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2634
        // the string value must pass each step without causing an exception
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2635
        if (!docast) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2636
            if (!doret) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2637
                if (step[0] != step[1])
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2638
                    checkCast[1] = true;  // incoming value is cast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2639
            } else {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2640
                if (step[2] != step[3])
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2641
                    checkCast[3] = true;  // outgoing value is cast
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2642
            }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2643
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2644
        boolean expectFail = false;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2645
        for (int i = 0; i < step.length; i++) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2646
            Class<?> c = step[i];
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2647
            if (!checkCast[i])  c = i2o(c);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2648
            if (!c.isInstance(normalRetVal)) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2649
                if (verbosity > 3)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2650
                    System.out.println("expect failure at step "+i+" in "+Arrays.toString(step)+Arrays.toString(checkCast));
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2651
                expectFail = true;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2652
                break;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2653
            }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2654
        }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2655
        countTest(!expectFail);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2656
        if (verbosity > 2)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2657
            System.out.println("expectFail="+expectFail+", mt="+mt);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2658
        Object res;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2659
        try {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2660
            res = mh.invokeWithArguments(normalRetVal);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2661
        } catch (Exception ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2662
            res = ex;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2663
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2664
        boolean sawFail = !(res instanceof String);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2665
        if (sawFail != expectFail) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2666
            System.out.println("*** testInterfaceCast: mh0 = "+mh0);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2667
            System.out.println("  retype using "+(docast ? "explicitCastArguments" : "asType")+" to "+mt+" => "+mh);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2668
            System.out.println("  call returned "+res);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2669
            System.out.println("  expected "+(expectFail ? "an exception" : normalRetVal));
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2670
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2671
        if (!expectFail) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2672
            assertFalse(res.toString(), sawFail);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2673
            assertEquals(normalRetVal, res);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2674
        } else {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2675
            assertTrue(res.toString(), sawFail);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2676
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2677
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2678
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2679
    @Test  // SLOW
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2680
    public void testCastFailure() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2681
        CodeCacheOverflowProcessor.runMHTest(this::testCastFailure0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2682
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2683
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2684
    public void testCastFailure0() throws Throwable {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2685
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2686
        startTest("testCastFailure");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2687
        testCastFailure("cast/argument", 11000);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2688
        if (CAN_TEST_LIGHTLY)  return;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2689
        testCastFailure("unbox/argument", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2690
        testCastFailure("cast/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2691
        testCastFailure("unbox/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2692
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2693
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2694
    static class Surprise {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2695
        public MethodHandle asMethodHandle() {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2696
            return VALUE.bindTo(this);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2697
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2698
        Object value(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2699
            trace("value", x);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2700
            if (boo != null)  return boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2701
            return x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2702
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2703
        Object boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2704
        void boo(Object x) { boo = x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2705
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2706
        static void trace(String x, Object y) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2707
            if (verbosity > 8) System.out.println(x+"="+y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2708
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2709
        static Object  refIdentity(Object x)  { trace("ref.x", x); return x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2710
        static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2711
        static int     intIdentity(int x)     { trace("int.x", x); return x; }
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2712
        static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2713
        static {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2714
            try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2715
                VALUE = PRIVATE.findVirtual(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2716
                    Surprise.class, "value",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2717
                        MethodType.methodType(Object.class, Object.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2718
                REF_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2719
                    Surprise.class, "refIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2720
                        MethodType.methodType(Object.class, Object.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2721
                BOX_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2722
                    Surprise.class, "boxIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2723
                        MethodType.methodType(Integer.class, Integer.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2724
                INT_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2725
                    Surprise.class, "intIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2726
                        MethodType.methodType(int.class, int.class));
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2727
            } catch (NoSuchMethodException | IllegalAccessException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2728
                throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2729
            }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2730
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2731
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2732
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2733
    @SuppressWarnings("ConvertToStringSwitch")
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2734
    void testCastFailure(String mode, int okCount) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2735
        countTest(false);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2736
        if (verbosity > 2)  System.out.println("mode="+mode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2737
        Surprise boo = new Surprise();
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2738
        MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2739
        if (mode.endsWith("/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2740
            if (mode.equals("unbox/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2741
                // fail on return to ((Integer)surprise).intValue
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2742
                surprise = surprise.asType(MethodType.methodType(int.class, Object.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2743
                identity = identity.asType(MethodType.methodType(int.class, Object.class));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2744
            } else if (mode.equals("cast/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2745
                // fail on return to (Integer)surprise
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2746
                surprise = surprise.asType(MethodType.methodType(Integer.class, Object.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2747
                identity = identity.asType(MethodType.methodType(Integer.class, Object.class));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2748
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2749
        } else if (mode.endsWith("/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2750
            MethodHandle callee = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2751
            if (mode.equals("unbox/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2752
                // fail on handing surprise to int argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2753
                callee = Surprise.INT_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2754
            } else if (mode.equals("cast/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2755
                // fail on handing surprise to Integer argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2756
                callee = Surprise.BOX_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2757
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2758
            if (callee != null) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2759
                callee = callee.asType(MethodType.genericMethodType(1));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2760
                surprise = MethodHandles.filterArguments(callee, 0, surprise);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2761
                identity = MethodHandles.filterArguments(callee, 0, identity);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2762
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2763
        }
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2764
        assertNotSame(mode, surprise, surprise0);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2765
        identity = identity.asType(MethodType.genericMethodType(1));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2766
        surprise = surprise.asType(MethodType.genericMethodType(1));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2767
        Object x = 42;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2768
        for (int i = 0; i < okCount; i++) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2769
            Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2770
            assertEquals(x, y);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2771
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2772
            assertEquals(x, z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2773
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2774
        boo.boo("Boo!");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2775
        Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2776
        assertEquals(x, y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2777
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2778
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2779
            System.out.println("Failed to throw; got z="+z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2780
            assertTrue(false);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2781
        } catch (ClassCastException ex) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2782
            if (verbosity > 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2783
                System.out.println("caught "+ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2784
            if (verbosity > 3)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2785
                ex.printStackTrace(System.out);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2786
            assertTrue(true);  // all is well
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2787
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2788
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2789
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2790
    static Example userMethod(Object o, String s, int i) {
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2791
        called("userMethod", o, s, i);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2792
        return null;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2793
    }
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2794
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2795
    @Test
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2796
    public void testUserClassInSignature() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2797
        CodeCacheOverflowProcessor.runMHTest(this::testUserClassInSignature0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2798
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2799
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2800
    public void testUserClassInSignature0() throws Throwable {
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2801
        if (CAN_SKIP_WORKING)  return;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2802
        startTest("testUserClassInSignature");
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2803
        Lookup lookup = MethodHandles.lookup();
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2804
        String name; MethodType mt; MethodHandle mh;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2805
        Object[] args;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2806
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2807
        // Try a static method.
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2808
        name = "userMethod";
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2809
        mt = MethodType.methodType(Example.class, Object.class, String.class, int.class);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2810
        mh = lookup.findStatic(lookup.lookupClass(), name, mt);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2811
        assertEquals(mt, mh.type());
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2812
        assertEquals(Example.class, mh.type().returnType());
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2813
        args = randomArgs(mh.type().parameterArray());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2814
        mh.invokeWithArguments(args);
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2815
        assertCalled(name, args);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2816
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2817
        // Try a virtual method.
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2818
        name = "v2";
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2819
        mt = MethodType.methodType(Object.class, Object.class, int.class);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2820
        mh = lookup.findVirtual(Example.class, name, mt);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2821
        assertEquals(mt, mh.type().dropParameterTypes(0,1));
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2822
        assertTrue(mh.type().parameterList().contains(Example.class));
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2823
        args = randomArgs(mh.type().parameterArray());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2824
        mh.invokeWithArguments(args);
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2825
        assertCalled(name, args);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2826
    }
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2827
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2828
    static void runForRunnable() {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2829
        called("runForRunnable");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2830
    }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2831
    public interface Fooable {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2832
        // overloads:
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2833
        Object  foo(Object x, String y);
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2834
        List<?> foo(String x, int y);
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2835
        Object  foo(String x);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2836
    }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2837
    static Object fooForFooable(String x, Object... y) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2838
        return called("fooForFooable/"+x, y);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2839
    }
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2840
    @SuppressWarnings("serial")  // not really a public API, just a test case
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2841
    public static class MyCheckedException extends Exception {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2842
    }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2843
    public interface WillThrow {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2844
        void willThrow() throws MyCheckedException;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2845
    }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2846
    /*non-public*/ interface PrivateRunnable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2847
        public void run();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2848
    }
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2849
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2850
    @Test
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2851
    public void testAsInterfaceInstance() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2852
        CodeCacheOverflowProcessor.runMHTest(this::testAsInterfaceInstance0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2853
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2854
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  2855
    public void testAsInterfaceInstance0() throws Throwable {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2856
        if (CAN_SKIP_WORKING)  return;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2857
        startTest("asInterfaceInstance");
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2858
        Lookup lookup = MethodHandles.lookup();
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2859
        // test typical case:  Runnable.run
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2860
        {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2861
            countTest();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2862
            if (verbosity >= 2)  System.out.println("Runnable");
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2863
            MethodType mt = MethodType.methodType(void.class);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2864
            MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "runForRunnable", mt);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2865
            Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2866
            proxy.run();
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2867
            assertCalled("runForRunnable");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2868
        }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2869
        // well known single-name overloaded interface:  Appendable.append
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2870
        {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2871
            countTest();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2872
            if (verbosity >= 2)  System.out.println("Appendable");
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2873
            ArrayList<List<?>> appendResults = new ArrayList<>();
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2874
            MethodHandle append = lookup.bind(appendResults, "add", MethodType.methodType(boolean.class, Object.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2875
            append = append.asType(MethodType.methodType(void.class, List.class)); // specialize the type
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2876
            MethodHandle asList = lookup.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2877
            MethodHandle mh = MethodHandles.filterReturnValue(asList, append).asVarargsCollector(Object[].class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2878
            Appendable proxy = MethodHandleProxies.asInterfaceInstance(Appendable.class, mh);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2879
            proxy.append("one");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2880
            proxy.append("two", 3, 4);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2881
            proxy.append('5');
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2882
            assertEquals(Arrays.asList(Arrays.asList("one"),
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2883
                                       Arrays.asList("two", 3, 4),
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2884
                                       Arrays.asList('5')),
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2885
                         appendResults);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2886
            if (verbosity >= 3)  System.out.println("appendResults="+appendResults);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2887
            appendResults.clear();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2888
            Formatter formatter = new Formatter(proxy);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2889
            String fmt = "foo str=%s char='%c' num=%d";
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2890
            Object[] fmtArgs = { "str!", 'C', 42 };
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2891
            String expect = String.format(fmt, fmtArgs);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2892
            formatter.format(fmt, fmtArgs);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2893
            String actual = "";
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2894
            if (verbosity >= 3)  System.out.println("appendResults="+appendResults);
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2895
            for (List<?> l : appendResults) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2896
                Object x = l.get(0);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2897
                switch (l.size()) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2898
                case 1:  actual += x; continue;
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2899
                case 3:  actual += ((String)x).substring((int)(Object)l.get(1), (int)(Object)l.get(2)); continue;
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2900
                }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2901
                actual += l;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2902
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2903
            if (verbosity >= 3)  System.out.println("expect="+expect);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2904
            if (verbosity >= 3)  System.out.println("actual="+actual);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2905
            assertEquals(expect, actual);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2906
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2907
        // test case of an single name which is overloaded:  Fooable.foo(...)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2908
        {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2909
            if (verbosity >= 2)  System.out.println("Fooable");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2910
            MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable",
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2911
                                                MethodType.methodType(Object.class, String.class, Object[].class));
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2912
            Fooable proxy = MethodHandleProxies.asInterfaceInstance(Fooable.class, mh);
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2913
            for (Method m : Fooable.class.getDeclaredMethods()) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2914
                countTest();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2915
                assertSame("foo", m.getName());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2916
                if (verbosity > 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2917
                    System.out.println("calling "+m);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2918
                MethodHandle invoker = lookup.unreflect(m);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2919
                MethodType mt = invoker.type();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2920
                Class<?>[] types = mt.parameterArray();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2921
                types[0] = int.class;  // placeholder
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2922
                Object[] args = randomArgs(types);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2923
                args[0] = proxy;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2924
                if (verbosity > 3)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2925
                    System.out.println("calling "+m+" on "+Arrays.asList(args));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2926
                Object result = invoker.invokeWithArguments(args);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2927
                if (verbosity > 4)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2928
                    System.out.println("result = "+result);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2929
                String name = "fooForFooable/"+args[1];
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2930
                Object[] argTail = Arrays.copyOfRange(args, 2, args.length);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2931
                assertCalled(name, argTail);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2932
                assertEquals(result, logEntry(name, argTail));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2933
            }
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2934
        }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2935
        // test processing of thrown exceptions:
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2936
        for (Throwable ex : new Throwable[] { new NullPointerException("ok"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2937
                                              new InternalError("ok"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2938
                                              new Throwable("fail"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2939
                                              new Exception("fail"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2940
                                              new MyCheckedException()
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2941
                                            }) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2942
            MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2943
            mh = MethodHandles.insertArguments(mh, 0, ex);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2944
            WillThrow proxy = MethodHandleProxies.asInterfaceInstance(WillThrow.class, mh);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2945
            try {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2946
                countTest();
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2947
                proxy.willThrow();
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2948
                System.out.println("Failed to throw: "+ex);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2949
                assertTrue(false);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2950
            } catch (Throwable ex1) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2951
                if (verbosity > 3) {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2952
                    System.out.println("throw "+ex);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2953
                    System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1));
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2954
                }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2955
                if (ex instanceof RuntimeException ||
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2956
                    ex instanceof Error) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2957
                    assertSame("must pass unchecked exception out without wrapping", ex, ex1);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2958
                } else if (ex instanceof MyCheckedException) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2959
                    assertSame("must pass declared exception out without wrapping", ex, ex1);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2960
                } else {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2961
                    assertNotSame("must pass undeclared checked exception with wrapping", ex, ex1);
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2962
                    if (!(ex1 instanceof UndeclaredThrowableException) || ex1.getCause() != ex) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  2963
                        ex1.printStackTrace(System.out);
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2964
                    }
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2965
                    assertSame(ex, ex1.getCause());
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2966
                    UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2967
                }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2968
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2969
        }
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2970
        // Test error checking on bad interfaces:
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2971
        for (Class<?> nonSMI : new Class<?>[] { Object.class,
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2972
                                             String.class,
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2973
                                             CharSequence.class,
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2974
                                             java.io.Serializable.class,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2975
                                             PrivateRunnable.class,
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2976
                                             Example.class }) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2977
            if (verbosity > 2)  System.out.println(nonSMI.getName());
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2978
            try {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2979
                countTest(false);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2980
                MethodHandleProxies.asInterfaceInstance(nonSMI, varargsArray(0));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2981
                assertTrue("Failed to throw on "+nonSMI.getName(), false);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2982
            } catch (IllegalArgumentException ex) {
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2983
                if (verbosity > 2)  System.out.println(nonSMI.getSimpleName()+": "+ex);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2984
                // Object: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2985
                //     not a public interface: java.lang.Object
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2986
                // String: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2987
                //     not a public interface: java.lang.String
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2988
                // CharSequence: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2989
                //     not a single-method interface: java.lang.CharSequence
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2990
                // Serializable: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2991
                //     not a single-method interface: java.io.Serializable
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2992
                // PrivateRunnable: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2993
                //     not a public interface: test.java.lang.invoke.MethodHandlesTest$PrivateRunnable
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2994
                // Example: java.lang.IllegalArgumentException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2995
                //     not a public interface: test.java.lang.invoke.MethodHandlesTest$Example
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2996
            }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2997
        }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  2998
        // Test error checking on interfaces with the wrong method type:
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  2999
        for (Class<?> intfc : new Class<?>[] { Runnable.class /*arity 0*/,
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3000
                                            Fooable.class /*arity 1 & 2*/ }) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3001
            int badArity = 1;  // known to be incompatible
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3002
            if (verbosity > 2)  System.out.println(intfc.getName());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3003
            try {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3004
                countTest(false);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3005
                MethodHandleProxies.asInterfaceInstance(intfc, varargsArray(badArity));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3006
                assertTrue("Failed to throw on "+intfc.getName(), false);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3007
            } catch (WrongMethodTypeException ex) {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3008
                if (verbosity > 2)  System.out.println(intfc.getSimpleName()+": "+ex);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3009
                // Runnable: java.lang.invoke.WrongMethodTypeException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3010
                //     cannot convert MethodHandle(Object)Object[] to ()void
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3011
                // Fooable: java.lang.invoke.WrongMethodTypeException:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 10080
diff changeset
  3012
                //     cannot convert MethodHandle(Object)Object[] to (Object,String)Object
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  3013
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  3014
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  3015
    }
10080
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3016
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3017
    @Test
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3018
    public void testRunnableProxy() throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  3019
        CodeCacheOverflowProcessor.runMHTest(this::testRunnableProxy0);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  3020
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  3021
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26467
diff changeset
  3022
    public void testRunnableProxy0() throws Throwable {
10080
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3023
        if (CAN_SKIP_WORKING)  return;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3024
        startTest("testRunnableProxy");
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3025
        MethodHandles.Lookup lookup = MethodHandles.lookup();
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3026
        MethodHandle run = lookup.findStatic(lookup.lookupClass(), "runForRunnable", MethodType.methodType(void.class));
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3027
        Runnable r = MethodHandleProxies.asInterfaceInstance(Runnable.class, run);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3028
        testRunnableProxy(r);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3029
        assertCalled("runForRunnable");
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3030
    }
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3031
    private static void testRunnableProxy(Runnable r) {
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3032
        //7058630: JSR 292 method handle proxy violates contract for Object methods
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3033
        r.run();
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3034
        Object o = r;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3035
        r = null;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3036
        boolean eq = (o == o);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3037
        int     hc = System.identityHashCode(o);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3038
        String  st = o.getClass().getName() + "@" + Integer.toHexString(hc);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3039
        Object expect = Arrays.asList(st, eq, hc);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3040
        if (verbosity >= 2)  System.out.println("expect st/eq/hc = "+expect);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3041
        Object actual = Arrays.asList(o.toString(), o.equals(o), o.hashCode());
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3042
        if (verbosity >= 2)  System.out.println("actual st/eq/hc = "+actual);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3043
        assertEquals(expect, actual);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  3044
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3045
}
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
  3046
// Local abbreviated copy of sun.invoke.util.ValueConversions
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  3047
// This guy tests access from outside the same package member, but inside
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  3048
// the package itself.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3049
class ValueConversions {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3050
    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3051
    private static final Object[] NO_ARGS_ARRAY = {};
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3052
    private static Object[] makeArray(Object... args) { return args; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3053
    private static Object[] array() { return NO_ARGS_ARRAY; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3054
    private static Object[] array(Object a0)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3055
                { return makeArray(a0); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3056
    private static Object[] array(Object a0, Object a1)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3057
                { return makeArray(a0, a1); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3058
    private static Object[] array(Object a0, Object a1, Object a2)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3059
                { return makeArray(a0, a1, a2); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3060
    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3061
                { return makeArray(a0, a1, a2, a3); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3062
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3063
                                  Object a4)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3064
                { return makeArray(a0, a1, a2, a3, a4); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3065
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3066
                                  Object a4, Object a5)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3067
                { return makeArray(a0, a1, a2, a3, a4, a5); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3068
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3069
                                  Object a4, Object a5, Object a6)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3070
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3071
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3072
                                  Object a4, Object a5, Object a6, Object a7)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3073
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3074
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3075
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3076
                                  Object a8)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3077
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3078
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3079
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3080
                                  Object a8, Object a9)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3081
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3082
    static MethodHandle[] makeArrays() {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  3083
        ArrayList<MethodHandle> arrays = new ArrayList<>();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3084
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3085
        for (;;) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3086
            int nargs = arrays.size();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3087
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3088
            String name = "array";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3089
            MethodHandle array = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3090
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3091
                array = lookup.findStatic(ValueConversions.class, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3092
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3093
                // break from loop!
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3094
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3095
            if (array == null)  break;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3096
            arrays.add(array);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3097
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3098
        assertTrue(arrays.size() == 11);  // current number of methods
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3099
        return arrays.toArray(new MethodHandle[0]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3100
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3101
    static final MethodHandle[] ARRAYS = makeArrays();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3102
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3103
    /** Return a method handle that takes the indicated number of Object
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3104
     *  arguments and returns an Object array of them, as if for varargs.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3105
     */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3106
    public static MethodHandle varargsArray(int nargs) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3107
        if (nargs < ARRAYS.length)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3108
            return ARRAYS[nargs];
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3109
        return MethodHandles.identity(Object[].class).asCollector(Object[].class, nargs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3110
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3111
    public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3112
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3113
        MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3114
        MethodHandle mh = varargsArray(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3115
        if (arrayType != Object[].class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3116
            mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3117
        return mh.asType(vaType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3118
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3119
    static Object changeArrayType(Class<?> arrayType, Object[] a) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3120
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3121
        if (!elemType.isPrimitive())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3122
            return Arrays.copyOf(a, a.length, arrayType.asSubclass(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3123
        Object b = java.lang.reflect.Array.newInstance(elemType, a.length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3124
        for (int i = 0; i < a.length; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3125
            java.lang.reflect.Array.set(b, i, a[i]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3126
        return b;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3127
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3128
    private static final MethodHandle CHANGE_ARRAY_TYPE;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3129
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3130
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3131
            CHANGE_ARRAY_TYPE = IMPL_LOOKUP.findStatic(ValueConversions.class, "changeArrayType",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3132
                                                       MethodType.methodType(Object.class, Class.class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3133
        } catch (NoSuchMethodException | IllegalAccessException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3134
            Error err = new InternalError("uncaught exception");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3135
            err.initCause(ex);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3136
            throw err;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3137
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  3138
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3139
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3140
    private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3141
    private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3142
    private static List<Object> list() { return NO_ARGS_LIST; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3143
    private static List<Object> list(Object a0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3144
                { return makeList(a0); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3145
    private static List<Object> list(Object a0, Object a1)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3146
                { return makeList(a0, a1); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3147
    private static List<Object> list(Object a0, Object a1, Object a2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3148
                { return makeList(a0, a1, a2); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3149
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3150
                { return makeList(a0, a1, a2, a3); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3151
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3152
                                     Object a4)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3153
                { return makeList(a0, a1, a2, a3, a4); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3154
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3155
                                     Object a4, Object a5)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3156
                { return makeList(a0, a1, a2, a3, a4, a5); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3157
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3158
                                     Object a4, Object a5, Object a6)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3159
                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3160
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3161
                                     Object a4, Object a5, Object a6, Object a7)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3162
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3163
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3164
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3165
                                     Object a8)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3166
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3167
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3168
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3169
                                     Object a8, Object a9)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3170
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3171
    static MethodHandle[] makeLists() {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  3172
        ArrayList<MethodHandle> lists = new ArrayList<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3173
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3174
        for (;;) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3175
            int nargs = lists.size();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3176
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3177
            String name = "list";
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3178
            MethodHandle list = null;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3179
            try {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3180
                list = lookup.findStatic(ValueConversions.class, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3181
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3182
                // break from loop!
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3183
            }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3184
            if (list == null)  break;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3185
            lists.add(list);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3186
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  3187
        assertTrue(lists.size() == 11);  // current number of methods
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3188
        return lists.toArray(new MethodHandle[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3189
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3190
    static final MethodHandle[] LISTS = makeLists();
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3191
    static final MethodHandle AS_LIST;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3192
    static {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3193
        try {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3194
            AS_LIST = IMPL_LOOKUP.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class));
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
  3195
        } catch (NoSuchMethodException | IllegalAccessException ex) { throw new RuntimeException(ex); }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3196
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3197
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3198
    /** Return a method handle that takes the indicated number of Object
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3199
     *  arguments and returns List.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3200
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3201
    public static MethodHandle varargsList(int nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3202
        if (nargs < LISTS.length)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3203
            return LISTS[nargs];
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  3204
        return AS_LIST.asCollector(Object[].class, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  3205
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3206
}
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3207
// This guy tests access from outside the same package member, but inside
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3208
// the package itself.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3209
class PackageSibling {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3210
    static Lookup lookup() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3211
        return MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3212
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  3213
}