jdk/test/java/lang/invoke/MethodHandlesTest.java
author jrose
Sat, 16 Jul 2011 15:40:13 -0700
changeset 10080 eb387b9bb282
parent 10078 944d876457df
child 10081 0f7b9636aa64
permissions -rw-r--r--
7058630: JSR 292 method handle proxy violates contract for Object methods Reviewed-by: never, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     1
/*
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
     2
 * Copyright (c) 2009, 2011, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    10
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    15
 * accompanied this code).
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    16
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    24
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    25
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    26
/* @test
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    27
 * @summary unit tests for java.lang.invoke.MethodHandles
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    28
 * @compile -source 7 -target 7 MethodHandlesTest.java
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    29
 * @run junit/othervm test.java.lang.invoke.MethodHandlesTest
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    30
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    31
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    32
package test.java.lang.invoke;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    33
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    34
import java.lang.invoke.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    35
import 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.*;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    40
import static org.junit.Assume.*;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    41
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    42
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    43
/**
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    44
 *
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    45
 * @author jrose
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    46
 */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    47
public class MethodHandlesTest {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    48
    // How much output?
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    49
    static int verbosity = 0;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    50
    static {
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    51
        String vstr = System.getProperty("test.java.lang.invoke.MethodHandlesTest.verbosity");
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    52
        if (vstr != null)  verbosity = Integer.parseInt(vstr);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
    53
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    54
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    55
    // 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
    56
    // a particular new, non-working test.  Tests which are known to
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    57
    // work (or have recently worked) test this flag and return on true.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    58
    static boolean CAN_SKIP_WORKING = false;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    59
    //static { CAN_SKIP_WORKING = true; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    60
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    61
    // Set true to test more calls.  If false, some tests are just
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    62
    // lookups, without exercising the actual method handle.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    63
    static boolean DO_MORE_CALLS = true;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    64
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    65
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    66
    public void testFirst() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    67
        verbosity += 9; try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    68
            // left blank for debugging
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    69
        } finally { printCounts(); verbosity -= 9; }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    70
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
    71
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    72
    // current failures
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    73
    @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    74
    public void testFail_1() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    75
        // AMH.<init>: IllegalArgumentException: bad adapter (conversion=0xfffab300): adapter pushes too many parameters
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    76
        testSpreadArguments(int.class, 0, 6);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    77
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    78
    @Test @Ignore("failure in JVM when expanding the stack using asm stub for _adapter_spread_args")
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    79
    public void testFail_2() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    80
        // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    81
        testSpreadArguments(Object.class, 0, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    82
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    83
    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    84
    public void testFail_3() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    85
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    86
        testSpreadArguments(int.class, 1, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    87
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    88
    @Test @Ignore("IllArgEx failure in call to ToGeneric.make")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    89
    public void testFail_4() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    90
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    91
        testCollectArguments(int.class, 1, 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    92
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    93
    @Test @Ignore("cannot collect leading primitive types")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    94
    public void testFail_5() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    95
        // ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    96
        testInvokers(MethodType.genericMethodType(2).changeParameterType(0, int.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    97
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    98
    @Test @Ignore("should not insert arguments beyond MethodHandlePushLimit")
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
    99
    public void testFail_6() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   100
        // ValueConversions.varargsArray: UnsupportedOperationException: NYI: cannot form a varargs array of length 13
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   101
        testInsertArguments(0, 0, MAX_ARG_INCREASE+10);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   102
    }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   103
    @Test @Ignore("permuteArguments has trouble with double slots")
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   104
    public void testFail_7() throws Throwable {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   105
        testPermuteArguments(new Object[]{10, 200L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   106
                             new Class<?>[]{Integer.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   107
                             new int[]{1,0});
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   108
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   109
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   110
                             new int[]{2,0,1}); //rot
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   111
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   112
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   113
                             new int[]{1,2,0}); //rot
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   114
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   115
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   116
                             new int[]{2,1,0}); //swap
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   117
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   118
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   119
                             new int[]{0,1,2,2}); //dup
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   120
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   121
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   122
                             new int[]{2,0,1,2});
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   123
        testPermuteArguments(new Object[]{10, 200L, 5000L},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   124
                             new Class<?>[]{Integer.class, long.class, long.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   125
                             new int[]{2,2,0,1});
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   126
        testPermuteArguments(4, Integer.class,  2, long.class,    6);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   127
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   128
    static final int MAX_ARG_INCREASE = 3;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   129
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   130
    public MethodHandlesTest() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   131
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   132
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   133
    String testName;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   134
    static int allPosTests, allNegTests;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   135
    int posTests, negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   136
    @After
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   137
    public void printCounts() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   138
        if (verbosity >= 2 && (posTests | negTests) != 0) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   139
            System.out.println();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   140
            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
   141
            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
   142
            allPosTests += posTests;
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   143
            allNegTests += negTests;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   144
            posTests = negTests = 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   145
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   146
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   147
    void countTest(boolean positive) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   148
        if (positive) ++posTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   149
        else          ++negTests;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   150
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   151
    void countTest() { countTest(true); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   152
    void startTest(String name) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   153
        if (testName != null)  printCounts();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   154
        if (verbosity >= 1)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   155
            System.out.println(name);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   156
        posTests = negTests = 0;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   157
        testName = name;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   158
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   159
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   160
    @BeforeClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   161
    public static void setUpClass() throws Exception {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   162
        calledLog.clear();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   163
        calledLog.add(null);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   164
        nextArgVal = INITIAL_ARG_VAL;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   165
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   166
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   167
    @AfterClass
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   168
    public static void tearDownClass() throws Exception {
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   169
        int posTests = allPosTests, negTests = allNegTests;
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   170
        if (verbosity >= 2 && (posTests | negTests) != 0) {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   171
            System.out.println();
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   172
            if (posTests != 0)  System.out.println("=== "+posTests+" total positive test cases");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   173
            if (negTests != 0)  System.out.println("=== "+negTests+" total negative test cases");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   174
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   175
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   176
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   177
    static List<Object> calledLog = new ArrayList<Object>();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   178
    static Object logEntry(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   179
        return Arrays.asList(name, Arrays.asList(args));
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
    static Object called(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   182
        Object entry = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   183
        calledLog.add(entry);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   184
        return entry;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   185
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   186
    static void assertCalled(String name, Object... args) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   187
        Object expected = logEntry(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   188
        Object actual   = calledLog.get(calledLog.size() - 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   189
        if (expected.equals(actual) && verbosity < 9)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   190
        System.out.println("assertCalled "+name+":");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   191
        System.out.println("expected:   "+expected);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   192
        System.out.println("actual:     "+actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   193
        System.out.println("ex. types:  "+getClasses(expected));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   194
        System.out.println("act. types: "+getClasses(actual));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   195
        assertEquals("previous method call", expected, actual);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   196
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   197
    static void printCalled(MethodHandle target, String name, Object... args) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   198
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   199
            System.out.println("calling MH="+target+" to "+name+Arrays.toString(args));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   200
    }
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
    static Object castToWrapper(Object value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   203
        Object wrap = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   204
        if (value instanceof Number)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   205
            wrap = castToWrapperOrNull(((Number)value).longValue(), dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   206
        if (value instanceof Character)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   207
            wrap = castToWrapperOrNull((char)(Character)value, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   208
        if (wrap != null)  return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   209
        return dst.cast(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   210
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   211
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   212
    static Object castToWrapperOrNull(long value, Class<?> dst) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   213
        if (dst == int.class || dst == Integer.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   214
            return (int)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   215
        if (dst == long.class || dst == Long.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   216
            return (long)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   217
        if (dst == char.class || dst == Character.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   218
            return (char)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   219
        if (dst == short.class || dst == Short.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   220
            return (short)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   221
        if (dst == float.class || dst == Float.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   222
            return (float)(value);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   223
        if (dst == double.class || dst == Double.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   224
            return (double)(value);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   225
        if (dst == byte.class || dst == Byte.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   226
            return (byte)(value);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   227
        if (dst == boolean.class || dst == boolean.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   228
            return ((value % 29) & 1) == 0;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   229
        return null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   230
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   231
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   232
    static final int ONE_MILLION = (1000*1000),  // first int value
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   233
                     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
   234
                     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
   235
    static long nextArgVal;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   236
    static long nextArg(boolean moreBits) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   237
        long val = nextArgVal++;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   238
        long sign = -(val & 1); // alternate signs
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   239
        val >>= 1;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   240
        if (moreBits)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   241
            // Guarantee some bits in the high word.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   242
            // In any case keep the decimal representation simple-looking,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   243
            // 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
   244
            // strings unnecessarily noisy.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   245
            val += (val % ONE_MILLION) * TEN_BILLION;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   246
        return val ^ sign;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   247
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   248
    static int nextArg() {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   249
        // Produce a 32-bit result something like ONE_MILLION+(smallint).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   250
        // Example: 1_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   251
        return (int) nextArg(false);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   252
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   253
    static long nextArg(Class<?> kind) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   254
        if (kind == long.class   || kind == Long.class ||
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   255
            kind == double.class || kind == Double.class)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   256
            // produce a 64-bit result something like
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   257
            // ((TEN_BILLION+1) * (ONE_MILLION+(smallint)))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   258
            // Example: 10_000_420_001_000_042.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   259
            return nextArg(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   260
        return (long) nextArg();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   261
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   262
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   263
    static Object randomArg(Class<?> param) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   264
        Object wrap = castToWrapperOrNull(nextArg(param), param);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   265
        if (wrap != null) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   266
            return wrap;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   267
        }
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   268
//        import sun.invoke.util.Wrapper;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   269
//        Wrapper wrap = Wrapper.forBasicType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   270
//        if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   271
//            wrap = Wrapper.forWrapperType(dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   272
//        if (wrap != Wrapper.OBJECT)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   273
//            return wrap.wrap(nextArg++);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   274
        if (param.isInterface()) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   275
            for (Class<?> c : param.getClasses()) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   276
                if (param.isAssignableFrom(c) && !c.isInterface())
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   277
                    { param = c; break; }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   278
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   279
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   280
        if (param.isInterface() || param.isAssignableFrom(String.class))
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   281
            return "#"+nextArg();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   282
        else
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   283
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   284
                return param.newInstance();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   285
            } catch (InstantiationException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   286
            } catch (IllegalAccessException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   287
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   288
        return null;  // random class not Object, String, Integer, etc.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   289
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   290
    static Object[] randomArgs(Class<?>... params) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   291
        Object[] args = new Object[params.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   292
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   293
            args[i] = randomArg(params[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   294
        return args;
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 Object[] randomArgs(int nargs, Class<?> param) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   297
        Object[] args = new Object[nargs];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   298
        for (int i = 0; i < args.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   299
            args[i] = randomArg(param);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   300
        return args;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   301
    }
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 <T, E extends T> T[] array(Class<T[]> atype, E... a) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   304
        return Arrays.copyOf(a, a.length, atype);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   305
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   306
    static <T> T[] cat(T[] a, T... b) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   307
        int alen = a.length, blen = b.length;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   308
        if (blen == 0)  return a;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   309
        T[] c = Arrays.copyOf(a, alen + blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   310
        System.arraycopy(b, 0, c, alen, blen);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   311
        return c;
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
    static Integer[] boxAll(int... vx) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   314
        Integer[] res = new Integer[vx.length];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   315
        for (int i = 0; i < res.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   316
            res[i] = vx[i];
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   317
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   318
        return res;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   319
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   320
    static Object getClasses(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   321
        if (x == null)  return x;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   322
        if (x instanceof String)  return x;  // keep the name
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   323
        if (x instanceof List) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   324
            // recursively report classes of the list elements
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   325
            Object[] xa = ((List)x).toArray();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   326
            for (int i = 0; i < xa.length; i++)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   327
                xa[i] = getClasses(xa[i]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   328
            return Arrays.asList(xa);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   329
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   330
        return x.getClass().getSimpleName();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   331
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   332
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   333
    /** Return lambda(arg...[arity]) { new Object[]{ arg... } } */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   334
    static MethodHandle varargsList(int arity) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   335
        return ValueConversions.varargsList(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   336
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   337
    /** Return lambda(arg...[arity]) { Arrays.asList(arg...) } */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   338
    static MethodHandle varargsArray(int arity) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   339
        return ValueConversions.varargsArray(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   340
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   341
    static MethodHandle varargsArray(Class<?> arrayType, int arity) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   342
        return ValueConversions.varargsArray(arrayType, arity);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   343
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   344
    /** Variation of varargsList, but with the given rtype. */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   345
    static MethodHandle varargsList(int arity, Class<?> rtype) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   346
        MethodHandle list = varargsList(arity);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   347
        MethodType listType = list.type().changeReturnType(rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   348
        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
   349
            // OK
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   350
        } else if (rtype.isAssignableFrom(String.class)) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   351
            if (LIST_TO_STRING == null)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   352
                try {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   353
                    LIST_TO_STRING = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToString",
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   354
                                                        MethodType.methodType(String.class, List.class));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   355
                } catch (Exception ex) { throw new RuntimeException(ex); }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   356
            list = MethodHandles.filterReturnValue(list, LIST_TO_STRING);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   357
        } else if (rtype.isPrimitive()) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   358
            if (LIST_TO_INT == null)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   359
                try {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   360
                    LIST_TO_INT = PRIVATE.findStatic(PRIVATE.lookupClass(), "listToInt",
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   361
                                                     MethodType.methodType(int.class, List.class));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   362
                } catch (Exception ex) { throw new RuntimeException(ex); }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   363
            list = MethodHandles.filterReturnValue(list, LIST_TO_INT);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   364
            list = MethodHandles.explicitCastArguments(list, listType);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   365
        } else {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   366
            throw new RuntimeException("varargsList: "+rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   367
        }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   368
        return list.asType(listType);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   369
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   370
    private static MethodHandle LIST_TO_STRING, LIST_TO_INT;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   371
    private static String listToString(List x) { return x.toString(); }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   372
    private static int listToInt(List x) { return x.toString().hashCode(); }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   373
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   374
    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   375
        return changeArgTypes(target, 0, 999, argType);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   376
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   377
    static MethodHandle changeArgTypes(MethodHandle target,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   378
            int beg, int end, Class<?> argType) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   379
        MethodType targetType = target.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   380
        end = Math.min(end, targetType.parameterCount());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   381
        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   382
        Collections.fill(argTypes.subList(beg, end), argType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   383
        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
   384
        return target.asType(ttype2);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   385
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   386
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   387
    // This lookup is good for all members in and under MethodHandlesTest.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   388
    static final Lookup PRIVATE = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   389
    // 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
   390
    static final Lookup PACKAGE = PackageSibling.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   391
    // This lookup is good only for public members.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   392
    static final Lookup PUBLIC  = MethodHandles.publicLookup();
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   393
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   394
    // Subject methods...
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   395
    static class Example implements IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   396
        final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   397
        public Example() { name = "Example#"+nextArg(); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   398
        protected Example(String name) { this.name = name; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   399
        protected Example(int x) { this(); called("protected <init>", this, x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   400
        @Override public String toString() { return name; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   401
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   402
        public void            v0()     { called("v0", this); }
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"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   406
        static void            pkg_s0() { called("pkg_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   407
        private static void    pri_s0() { called("pri_s0"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   408
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   409
        public Object          v1(Object x) { return called("v1", this, x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   410
        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
   411
        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
   412
        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
   413
        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
   414
        public static Object   s1(Object x) { return called("s1", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   415
        public static Object   s2(int x)    { return called("s2", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   416
        public static Object   s3(long x)   { return called("s3", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   417
        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
   418
        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
   419
        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
   420
        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
   421
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   422
        static final Lookup EXAMPLE = MethodHandles.lookup();  // for testing findSpecial
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   423
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   424
    static final Lookup EXAMPLE = Example.EXAMPLE;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   425
    public static class PubExample extends Example {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   426
        public PubExample() { super("PubExample#"+nextArg()); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   427
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   428
    static class SubExample extends Example {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   429
        @Override public void  v0()     { called("Sub/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   430
        @Override void         pkg_v0() { called("Sub/pkg_v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   431
        private      SubExample(int x)  { called("<init>", this, x); }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   432
        public SubExample() { super("SubExample#"+nextArg()); }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   433
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   434
    public static interface IntExample {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   435
        public void            v0();
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
   436
        public static class Impl implements IntExample {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   437
            public void        v0()     { called("Int/v0", this); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   438
            final String name;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   439
            public Impl() { name = "Impl#"+nextArg(); }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   440
            @Override public String toString() { return name; }
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
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   443
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   444
    static final Object[][][] ACCESS_CASES = {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   445
        { { false, PUBLIC }, { false, PACKAGE }, { false, PRIVATE }, { false, EXAMPLE } }, //[0]: all false
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   446
        { { false, PUBLIC }, { false, PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[1]: only PRIVATE
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   447
        { { false, PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[2]: PUBLIC false
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   448
        { { true,  PUBLIC }, { true,  PACKAGE }, { true,  PRIVATE }, { true,  EXAMPLE } }, //[3]: all true
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   449
    };
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   450
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   451
    static Object[][] accessCases(Class<?> defc, String name, boolean isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   452
        Object[][] cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   453
        if (name.contains("pri_") || isSpecial) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   454
            cases = ACCESS_CASES[1]; // PRIVATE only
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   455
        } else if (name.contains("pkg_") || !Modifier.isPublic(defc.getModifiers())) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   456
            cases = ACCESS_CASES[2]; // not PUBLIC
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   457
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   458
            assertTrue(name.indexOf('_') < 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   459
            boolean pubc = Modifier.isPublic(defc.getModifiers());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   460
            if (pubc)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   461
                cases = ACCESS_CASES[3]; // all access levels
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   462
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   463
                cases = ACCESS_CASES[2]; // PACKAGE but not PUBLIC
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   464
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   465
        if (defc != Example.class && cases[cases.length-1][1] == EXAMPLE)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   466
            cases = Arrays.copyOfRange(cases, 0, cases.length-1);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   467
        return cases;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   468
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   469
    static Object[][] accessCases(Class<?> defc, String name) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   470
        return accessCases(defc, name, false);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   471
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   472
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   473
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   474
    public void testFindStatic() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   475
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   476
        startTest("findStatic");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   477
        testFindStatic(PubExample.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   478
        testFindStatic(Example.class, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   479
        testFindStatic(Example.class, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   480
        testFindStatic(Example.class, void.class, "pri_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   481
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   482
        testFindStatic(Example.class, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   483
        testFindStatic(Example.class, Object.class, "s2", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   484
        testFindStatic(Example.class, Object.class, "s3", long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   485
        testFindStatic(Example.class, Object.class, "s4", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   486
        testFindStatic(Example.class, Object.class, "s5", long.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   487
        testFindStatic(Example.class, Object.class, "s6", int.class, long.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   488
        testFindStatic(Example.class, Object.class, "s7", float.class, double.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   489
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   490
        testFindStatic(false, PRIVATE, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   491
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   492
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   493
    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
   494
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   495
            testFindStatic((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   496
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   497
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   498
    void 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
   499
        testFindStatic(true, lookup, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   500
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   501
    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
   502
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   503
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   504
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   505
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   506
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   507
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   508
            target = lookup.in(defc).findStatic(defc, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   509
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   510
            noAccess = ex;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   511
            if (name.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   512
                assertTrue(noAccess instanceof NoSuchMethodException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   513
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   514
                assertTrue(noAccess instanceof IllegalAccessException);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   515
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   516
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   517
            System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   518
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   519
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   520
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   521
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   522
        assertEquals(type, target.type());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   523
        assertNameStringContains(target, name);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   524
        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   525
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   526
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   527
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   528
        assertCalled(name, args);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   529
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   530
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   531
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   532
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
   533
    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
   534
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   535
    // 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
   536
    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
   537
        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
   538
            // 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
   539
            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
   540
            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
   541
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   542
        if (x.toString().contains(s))  return;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   543
        assertEquals(s, x);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   544
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   545
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   546
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   547
    public void testFindVirtual() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   548
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   549
        startTest("findVirtual");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   550
        testFindVirtual(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   551
        testFindVirtual(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   552
        testFindVirtual(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   553
        testFindVirtual(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   554
        testFindVirtual(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   555
        testFindVirtual(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   556
        testFindVirtual(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   557
        testFindVirtual(Example.class, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   558
        testFindVirtual(false, PRIVATE, Example.class, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   559
        // test dispatch
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   560
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   561
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   562
        testFindVirtual(SubExample.class,      IntExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   563
        testFindVirtual(SubExample.class,      SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   564
        testFindVirtual(SubExample.class,         Example.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   565
        testFindVirtual(Example.class,         IntExample.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   566
        testFindVirtual(IntExample.Impl.class, IntExample.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   567
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   568
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   569
    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
   570
        Class<?> rcvc = defc;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   571
        testFindVirtual(rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   572
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   573
    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
   574
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   575
            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
   576
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   577
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   578
    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
   579
        testFindVirtual(true, lookup, rcvc, defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   580
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   581
    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
   582
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   583
        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
   584
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   585
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   586
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   587
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   588
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   589
            target = lookup.in(defc).findVirtual(defc, methodName, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   590
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   591
            noAccess = ex;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   592
            if (name.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   593
                assertTrue(noAccess instanceof NoSuchMethodException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   594
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   595
                assertTrue(noAccess instanceof IllegalAccessException);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   596
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   597
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   598
            System.out.println("findVirtual "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   599
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   600
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   601
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   602
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   603
        Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)defc), params);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   604
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   605
        assertEquals(typeWithSelf, target.type());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   606
        assertNameStringContains(target, methodName);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   607
        if (!DO_MORE_CALLS && lookup != PRIVATE)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   608
        Object[] argsWithSelf = randomArgs(paramsWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   609
        if (rcvc != defc)  argsWithSelf[0] = randomArg(rcvc);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   610
        printCalled(target, name, argsWithSelf);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   611
        target.invokeWithArguments(argsWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   612
        assertCalled(name, argsWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   613
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   614
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   615
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   616
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   617
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   618
    public void testFindSpecial() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   619
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   620
        startTest("findSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   621
        testFindSpecial(SubExample.class, Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   622
        testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   623
        // Do some negative testing:
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   624
        testFindSpecial(false, EXAMPLE, SubExample.class, Example.class, void.class, "bogus");
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   625
        testFindSpecial(false, PRIVATE, SubExample.class, Example.class, void.class, "bogus");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   626
        for (Lookup lookup : new Lookup[]{ PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   627
            testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   628
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   629
            testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   630
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   631
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   632
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   633
    void testFindSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   634
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   635
        testFindSpecial(true,  EXAMPLE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   636
        testFindSpecial(true,  PRIVATE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   637
        testFindSpecial(false, PACKAGE, specialCaller, defc, ret, name, params);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   638
        testFindSpecial(false, PUBLIC,  specialCaller, defc, ret, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   639
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   640
    void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   641
                         Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   642
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   643
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   644
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   645
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   646
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   647
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   648
            if (verbosity >= 5)  System.out.println("  lookup => "+lookup.in(specialCaller));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   649
            target = lookup.in(specialCaller).findSpecial(defc, name, type, specialCaller);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   650
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   651
            noAccess = ex;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   652
            if (name.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   653
                assertTrue(noAccess instanceof NoSuchMethodException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   654
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   655
                assertTrue(noAccess instanceof IllegalAccessException);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   656
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   657
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   658
            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
   659
                               +(target == null ? "" : target.type())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   660
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   661
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   662
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   663
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   664
        assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   665
        assertEquals(type,          target.type().dropParameterTypes(0,1));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   666
        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
   667
        MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   668
        assertNameStringContains(target, name);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   669
        if (!DO_MORE_CALLS && lookup != PRIVATE && lookup != EXAMPLE)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   670
        Object[] args = randomArgs(paramsWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   671
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   672
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   673
        assertCalled(name, args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   674
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   675
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   676
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   677
    public void testBind() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   678
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   679
        startTest("bind");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   680
        testBind(Example.class, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   681
        testBind(Example.class, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   682
        testBind(Example.class, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   683
        testBind(Example.class, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   684
        testBind(Example.class, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   685
        testBind(Example.class, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   686
        testBind(Example.class, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   687
        testBind(Example.class, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   688
        testBind(false, PRIVATE, Example.class, void.class, "bogus");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   689
        testBind(SubExample.class, void.class, "Sub/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   690
        testBind(SubExample.class, void.class, "Sub/pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   691
        testBind(IntExample.Impl.class, void.class, "Int/v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   692
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   693
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   694
    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
   695
        for (Object[] ac : accessCases(defc, name)) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   696
            testBind((Boolean)ac[0], (Lookup)ac[1], defc, ret, name, params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   697
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   698
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   699
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   700
    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
   701
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   702
        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
   703
        MethodType type = MethodType.methodType(ret, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   704
        Object receiver = randomArg(defc);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   705
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   706
        Exception noAccess = null;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   707
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   708
            if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   709
            target = lookup.in(defc).bind(receiver, methodName, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   710
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   711
            noAccess = ex;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   712
            if (name.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   713
                assertTrue(noAccess instanceof NoSuchMethodException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   714
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   715
                assertTrue(noAccess instanceof IllegalAccessException);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   716
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   717
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   718
            System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   719
                    +(noAccess == null ? "" : " !! "+noAccess));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   720
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   721
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   722
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   723
        assertEquals(type, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   724
        Object[] args = randomArgs(params);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   725
        printCalled(target, name, args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   726
        target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   727
        Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   728
        assertCalled(name, argsWithReceiver);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   729
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   730
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   731
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   732
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   733
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   734
    public void testUnreflect() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   735
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   736
        startTest("unreflect");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   737
        testUnreflect(Example.class, true, void.class, "s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   738
        testUnreflect(Example.class, true, void.class, "pkg_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   739
        testUnreflect(Example.class, true, void.class, "pri_s0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   740
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   741
        testUnreflect(Example.class, true, Object.class, "s1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   742
        testUnreflect(Example.class, true, Object.class, "s2", int.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   743
        testUnreflect(Example.class, true, Object.class, "s3", long.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   744
        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
   745
        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
   746
        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
   747
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   748
        testUnreflect(Example.class, false, void.class, "v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   749
        testUnreflect(Example.class, false, void.class, "pkg_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   750
        testUnreflect(Example.class, false, void.class, "pri_v0");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   751
        testUnreflect(Example.class, false, Object.class, "v1", Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   752
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   753
        testUnreflect(Example.class, false, Object.class, "v2", Object.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   754
        testUnreflect(Example.class, false, Object.class, "v2", int.class, Object.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   755
        testUnreflect(Example.class, false, Object.class, "v2", int.class, int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   756
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   757
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   758
    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
   759
        for (Object[] ac : accessCases(defc, name)) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   760
            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
   761
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   762
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   763
    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
   764
        for (Object[] ac : accessCases(defc, name)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   765
            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
   766
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   767
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   768
    void testUnreflectMaybeSpecial(Class<?> specialCaller,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   769
                                   boolean positive, Lookup lookup,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   770
                                   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
   771
        countTest(positive);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   772
        MethodType type = MethodType.methodType(ret, params);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   773
        Method rmethod = defc.getDeclaredMethod(name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   774
        MethodHandle target = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   775
        Exception noAccess = null;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   776
        boolean isStatic = (rcvc == null);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   777
        boolean isSpecial = (specialCaller != null);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   778
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   779
            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
   780
            if (isSpecial)
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   781
                target = lookup.in(specialCaller).unreflectSpecial(rmethod, specialCaller);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   782
            else
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   783
                target = lookup.in(defc).unreflect(rmethod);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   784
        } catch (ReflectiveOperationException ex) {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   785
            noAccess = ex;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   786
            if (name.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   787
                assertTrue(noAccess instanceof NoSuchMethodException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   788
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   789
                assertTrue(noAccess instanceof IllegalAccessException);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   790
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   791
        if (verbosity >= 3)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   792
            System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   793
                               +(!isSpecial ? "" : " specialCaller="+specialCaller)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   794
                               +( isStatic  ? "" : " receiver="+rcvc)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   795
                               +" => "+target
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   796
                               +(noAccess == null ? "" : " !! "+noAccess));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   797
        if (positive && noAccess != null)  throw noAccess;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   798
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   799
        if (!positive)  return; // negative test failed as expected
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   800
        assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   801
        Class<?>[] paramsMaybeWithSelf = params;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   802
        if (!isStatic) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   803
            paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   804
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   805
        MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   806
        if (isStatic) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   807
            assertEquals(typeMaybeWithSelf, target.type());
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   808
        } else {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   809
            if (isSpecial)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   810
                assertEquals(specialCaller, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   811
            else
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   812
                assertEquals(defc, target.type().parameterType(0));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   813
            assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   814
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   815
        Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   816
        printCalled(target, name, argsMaybeWithSelf);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   817
        target.invokeWithArguments(argsMaybeWithSelf);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   818
        assertCalled(name, argsMaybeWithSelf);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   819
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   820
            System.out.print(':');
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   821
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   822
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   823
    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
   824
        for (Object[] ac : accessCases(defc, name, true)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   825
            Class<?> specialCaller = rcvc;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   826
            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
   827
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   828
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   829
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   830
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   831
    public void testUnreflectSpecial() throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   832
        if (CAN_SKIP_WORKING)  return;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   833
        startTest("unreflectSpecial");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   834
        testUnreflectSpecial(Example.class,    Example.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   835
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   836
        testUnreflectSpecial(Example.class,    Example.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   837
        testUnreflectSpecial(Example.class, SubExample.class, void.class, "pkg_v0");
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   838
        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
   839
        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
   840
        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
   841
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   842
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   843
    public static class HasFields {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   844
        boolean fZ = false;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   845
        byte fB = (byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   846
        short fS = (short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   847
        char fC = 'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   848
        int fI = 'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   849
        long fJ = 'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   850
        float fF = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   851
        double fD = 'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   852
        static boolean sZ = true;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   853
        static byte sB = 1+(byte)'B';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   854
        static short sS = 1+(short)'S';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   855
        static char sC = 1+'C';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   856
        static int sI = 1+'I';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   857
        static long sJ = 1+'J';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   858
        static float sF = 1+'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   859
        static double sD = 1+'D';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   860
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   861
        Object fL = 'L';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   862
        String fR = "R";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   863
        static Object sL = 'M';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   864
        static String sR = "S";
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   865
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   866
        static final Object[][] CASES;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   867
        static {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   868
            ArrayList<Object[]> cases = new ArrayList<Object[]>();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   869
            Object types[][] = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   870
                {'L',Object.class}, {'R',String.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   871
                {'I',int.class}, {'J',long.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   872
                {'F',float.class}, {'D',double.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   873
                {'Z',boolean.class}, {'B',byte.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   874
                {'S',short.class}, {'C',char.class},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   875
            };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   876
            HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   877
            for (Object[] t : types) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   878
                for (int kind = 0; kind <= 1; kind++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   879
                    boolean isStatic = (kind != 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   880
                    char btc = (Character)t[0];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   881
                    String name = (isStatic ? "s" : "f") + btc;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   882
                    Class<?> type = (Class<?>) t[1];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   883
                    Object value;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   884
                    Field field;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   885
                        try {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   886
                        field = HasFields.class.getDeclaredField(name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   887
                    } catch (Exception ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   888
                        throw new InternalError("no field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   889
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   890
                    try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   891
                        value = field.get(fields);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   892
                    } catch (Exception ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   893
                        throw new InternalError("cannot fetch field HasFields."+name);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   894
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   895
                    if (type == float.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   896
                        float v = 'F';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   897
                        if (isStatic)  v++;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   898
                        assertTrue(value.equals(v));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   899
                    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   900
                    assertTrue(name.equals(field.getName()));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   901
                    assertTrue(type.equals(field.getType()));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   902
                    assertTrue(isStatic == (Modifier.isStatic(field.getModifiers())));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   903
                    cases.add(new Object[]{ field, value });
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   904
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   905
            }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   906
            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
   907
            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
   908
            CASES = cases.toArray(new Object[0][]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   909
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   910
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   911
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   912
    static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   913
    static boolean testModeMatches(int testMode, boolean isStatic) {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   914
        switch (testMode) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   915
        case TEST_FIND_STATIC:          return isStatic;
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   916
        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
   917
        case TEST_UNREFLECT:            return true;  // unreflect matches both
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   918
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   919
        throw new InternalError("testMode="+testMode);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   920
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   921
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   922
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   923
    public void testUnreflectGetter() throws Throwable {
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   924
        startTest("unreflectGetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   925
        testGetter(TEST_UNREFLECT);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   926
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   927
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   928
    public void testFindGetter() throws Throwable {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   929
        startTest("findGetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   930
        testGetter(TEST_FIND_FIELD);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   931
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   932
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   933
    public void testFindStaticGetter() throws Throwable {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   934
        startTest("findStaticGetter");
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   935
        testGetter(TEST_FIND_STATIC);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   936
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   937
    public void testGetter(int testMode) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
   938
        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
   939
        for (Object[] c : HasFields.CASES) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   940
            boolean positive = (c[1] != Error.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   941
            testGetter(positive, lookup, c[0], c[1], testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   942
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   943
        testGetter(true, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   944
                   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
   945
                   System.out, testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   946
        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
   947
            testGetter(false, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   948
                       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
   949
                       null, testMode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   950
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   951
    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   952
    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
   953
                           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
   954
        testAccessor(positive, lookup, fieldRef, value, testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   955
    }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   956
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   957
    public void testAccessor(boolean positive, MethodHandles.Lookup lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   958
                             Object fieldRef, Object value, int testMode0) throws Throwable {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   959
        boolean isGetter = ((testMode0 & TEST_SETTER) == 0);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   960
        int testMode = testMode0 & ~TEST_SETTER;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   961
        boolean isStatic;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   962
        Class<?> fclass;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   963
        String   fname;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   964
        Class<?> ftype;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   965
        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
   966
        if (f != null) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   967
            isStatic = Modifier.isStatic(f.getModifiers());
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   968
            fclass   = f.getDeclaringClass();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   969
            fname    = f.getName();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   970
            ftype    = f.getType();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   971
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   972
            Object[] scnt = (Object[]) fieldRef;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   973
            isStatic = (Boolean)  scnt[0];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   974
            fclass   = (Class<?>) scnt[1];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   975
            fname    = (String)   scnt[2];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   976
            ftype    = (Class<?>) scnt[3];
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   977
            try {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   978
                f = fclass.getDeclaredField(fname);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   979
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   980
                f = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   981
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   982
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
   983
        if (!testModeMatches(testMode, isStatic))  return;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   984
        if (f == null && testMode == TEST_UNREFLECT)  return;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   985
        countTest(positive);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   986
        MethodType expType;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   987
        if (isGetter)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   988
            expType = MethodType.methodType(ftype, HasFields.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   989
        else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   990
            expType = MethodType.methodType(void.class, HasFields.class, ftype);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
   991
        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
   992
        Exception noAccess = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   993
        MethodHandle mh;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   994
        try {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   995
            switch (testMode0) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   996
            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
   997
            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
   998
            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
   999
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1000
                 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
  1001
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1002
                 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
  1003
            case TEST_SETTER|
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1004
                 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
  1005
            default:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1006
                throw new InternalError("testMode="+testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1007
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1008
        } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1009
            mh = null;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1010
            noAccess = ex;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1011
            if (fname.contains("bogus"))
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1012
                assertTrue(noAccess instanceof NoSuchFieldException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1013
            else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1014
                assertTrue(noAccess instanceof IllegalAccessException);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1015
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1016
        if (verbosity >= 3)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1017
            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
  1018
                               +" => "+mh
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1019
                               +(noAccess == null ? "" : " !! "+noAccess));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1020
        if (positive && noAccess != null)  throw new RuntimeException(noAccess);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1021
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, mh != null);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1022
        if (!positive)  return; // negative test failed as expected
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1023
        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
  1024
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1025
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1026
        assertSame(mh.type(), expType);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1027
        assertNameStringContains(mh, fname);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1028
        HasFields fields = new HasFields();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1029
        Object sawValue;
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1030
        Class<?> vtype = ftype;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1031
        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
  1032
        if (isGetter) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1033
            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
  1034
                           .changeReturnType(vtype));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1035
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1036
            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
  1037
            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
  1038
                           .changeReturnType(void.class)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1039
                           .changeParameterType(last, vtype));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1040
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1041
        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
  1042
            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
  1043
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1044
        if (isGetter) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1045
            Object expValue = value;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1046
            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
  1047
                if (isStatic) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1048
                    if (ftype == int.class)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1049
                        sawValue = (int) mh.invokeExact();  // do these exactly
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1050
                    else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1051
                        sawValue = mh.invokeExact();
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1052
                } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1053
                    if (ftype == int.class)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1054
                        sawValue = (int) mh.invokeExact((Object) fields);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1055
                    else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1056
                        sawValue = mh.invokeExact((Object) fields);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1057
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1058
                assertEquals(sawValue, expValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1059
                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
  1060
                    && !Modifier.isFinal(f.getModifiers())) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1061
                    Object random = randomArg(ftype);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1062
                    f.set(fields, random);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1063
                    expValue = random;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1064
                } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1065
                    break;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1066
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1067
            }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1068
        } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1069
            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
  1070
                Object putValue = randomArg(ftype);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1071
                if (isStatic) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1072
                    if (ftype == int.class)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1073
                        mh.invokeExact((int)putValue);  // do these exactly
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1074
                    else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1075
                        mh.invokeExact(putValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1076
                } else {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1077
                    if (ftype == int.class)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1078
                        mh.invokeExact((Object) fields, (int)putValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1079
                    else
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1080
                        mh.invokeExact((Object) fields, putValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1081
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1082
                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
  1083
                    assertEquals(f.get(fields), putValue);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1084
                }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1085
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1086
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1087
        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
  1088
            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
  1089
        }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1090
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1091
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1092
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1093
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1094
    public void testUnreflectSetter() throws Throwable {
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1095
        startTest("unreflectSetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1096
        testSetter(TEST_UNREFLECT);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1097
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1098
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1099
    public void testFindSetter() throws Throwable {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1100
        startTest("findSetter");
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1101
        testSetter(TEST_FIND_FIELD);
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1102
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1103
    @Test
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1104
    public void testFindStaticSetter() throws Throwable {
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1105
        startTest("findStaticSetter");
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1106
        testSetter(TEST_FIND_STATIC);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1107
    }
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5723
diff changeset
  1108
    public void testSetter(int testMode) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1109
        Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1110
        startTest("unreflectSetter");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1111
        for (Object[] c : HasFields.CASES) {
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1112
            boolean positive = (c[1] != Error.class);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1113
            testSetter(positive, lookup, c[0], c[1], testMode);
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1114
        }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1115
        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
  1116
            testSetter(false, lookup,
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1117
                       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
  1118
                       null, testMode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1119
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1120
    }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1121
    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
  1122
                           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
  1123
        testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1124
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1125
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1126
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1127
    public void testArrayElementGetter() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1128
        startTest("arrayElementGetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1129
        testArrayElementGetterSetter(false);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1130
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1131
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1132
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1133
    public void testArrayElementSetter() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1134
        startTest("arrayElementSetter");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1135
        testArrayElementGetterSetter(true);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1136
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1137
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1138
    public void testArrayElementGetterSetter(boolean testSetter) throws Throwable {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1139
        testArrayElementGetterSetter(new Object[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1140
        testArrayElementGetterSetter(new String[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1141
        testArrayElementGetterSetter(new boolean[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1142
        testArrayElementGetterSetter(new byte[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1143
        testArrayElementGetterSetter(new char[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1144
        testArrayElementGetterSetter(new short[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1145
        testArrayElementGetterSetter(new int[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1146
        testArrayElementGetterSetter(new float[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1147
        testArrayElementGetterSetter(new long[10], testSetter);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1148
        testArrayElementGetterSetter(new double[10], testSetter);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1149
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1150
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1151
    public void testArrayElementGetterSetter(Object array, boolean testSetter) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1152
        countTest(true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1153
        if (verbosity >= 2)  System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]");
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1154
        Class<?> arrayType = array.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1155
        Class<?> elemType = arrayType.getComponentType();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1156
        MethodType expType = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1157
                ? MethodType.methodType(elemType,   arrayType, int.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1158
                : MethodType.methodType(void.class, arrayType, int.class, elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1159
        MethodHandle mh = !testSetter
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1160
                ? MethodHandles.arrayElementGetter(arrayType)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1161
                : MethodHandles.arrayElementSetter(arrayType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1162
        assertSame(mh.type(), expType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1163
        if (elemType != int.class && elemType != boolean.class) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1164
            // FIXME: change Integer.class and (Integer) below to int.class and (int) below.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1165
            MethodType gtype = mh.type().generic().changeParameterType(1, Integer.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1166
            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
  1167
            mh = mh.asType(gtype);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1168
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1169
        Object sawValue, expValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1170
        List<Object> model = array2list(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1171
        int length = Array.getLength(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1172
        for (int i = 0; i < length; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1173
            // update array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1174
            Object random = randomArg(elemType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1175
            model.set(i, random);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1176
            if (testSetter) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1177
                if (elemType == int.class)
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
  1178
                    mh.invokeExact((int[]) array, i, (int)random);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1179
                else if (elemType == boolean.class)
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
  1180
                    mh.invokeExact((boolean[]) array, i, (boolean)random);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1181
                else
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1182
                    mh.invokeExact(array, (Integer)i, random);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1183
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1184
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1185
                Array.set(array, i, random);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1186
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1187
            if (verbosity >= 5) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1188
                List<Object> array2list = array2list(array);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1189
                System.out.println("a["+i+"]="+random+" => "+array2list);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1190
                if (!array2list.equals(model))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1191
                    System.out.println("***   != "+model);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1192
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1193
            // observe array element
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1194
            sawValue = Array.get(array, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1195
            if (!testSetter) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1196
                expValue = sawValue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1197
                if (elemType == int.class)
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
  1198
                    sawValue = (int) mh.invokeExact((int[]) array, i);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1199
                else if (elemType == boolean.class)
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
  1200
                    sawValue = (boolean) mh.invokeExact((boolean[]) array, i);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1201
                else
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1202
                    sawValue = mh.invokeExact(array, (Integer)i);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1203
                assertEquals(sawValue, expValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1204
                assertEquals(model, array2list(array));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1205
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1206
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1207
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1208
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1209
    List<Object> array2list(Object array) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1210
        int length = Array.getLength(array);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1211
        ArrayList<Object> model = new ArrayList<Object>(length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1212
        for (int i = 0; i < length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1213
            model.add(Array.get(array, i));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1214
        return model;
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1215
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1216
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1217
    static class Callee {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1218
        static Object id() { return called("id"); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1219
        static Object id(Object x) { return called("id", x); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1220
        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
  1221
        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
  1222
        static Object id(Object... vx) { return called("id", vx); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1223
        static MethodHandle ofType(int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1224
            return ofType(Object.class, n);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1225
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1226
        static MethodHandle ofType(Class<?> rtype, int n) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1227
            if (n == -1)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1228
                return ofType(MethodType.methodType(rtype, Object[].class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1229
            return ofType(MethodType.genericMethodType(n).changeReturnType(rtype));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1230
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1231
        static MethodHandle ofType(Class<?> rtype, Class<?>... ptypes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1232
            return ofType(MethodType.methodType(rtype, ptypes));
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1233
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1234
        static MethodHandle ofType(MethodType type) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1235
            Class<?> rtype = type.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1236
            String pfx = "";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1237
            if (rtype != Object.class)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1238
                pfx = rtype.getSimpleName().substring(0, 1).toLowerCase();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1239
            String name = pfx+"id";
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1240
            try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1241
                return PRIVATE.findStatic(Callee.class, name, type);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1242
            } catch (Exception ex) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1243
                throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  1244
            }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1245
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1246
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1247
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1248
    @Test
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1249
    public void testConvertArguments() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1250
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1251
        startTest("convertArguments");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1252
        testConvert(Callee.ofType(1), null, "id", int.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1253
        testConvert(Callee.ofType(1), null, "id", String.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1254
        testConvert(Callee.ofType(1), null, "id", Integer.class);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1255
        testConvert(Callee.ofType(1), null, "id", short.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1256
        testConvert(Callee.ofType(1), null, "id", char.class);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1257
        testConvert(Callee.ofType(1), null, "id", byte.class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1258
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1259
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1260
    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
  1261
        testConvert(true, id, rtype, name, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1262
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1263
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1264
    void testConvert(boolean positive,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1265
                     MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1266
        countTest(positive);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1267
        MethodType idType = id.type();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1268
        if (rtype == null)  rtype = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1269
        for (int i = 0; i < params.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1270
            if (params[i] == null)  params[i] = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1271
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1272
        // simulate the pairwise conversion
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1273
        MethodType newType = MethodType.methodType(rtype, params);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1274
        Object[] args = randomArgs(newType.parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1275
        Object[] convArgs = args.clone();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1276
        for (int i = 0; i < args.length; i++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1277
            Class<?> src = newType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1278
            Class<?> dst = idType.parameterType(i);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1279
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1280
                convArgs[i] = castToWrapper(convArgs[i], dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1281
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1282
        Object convResult = id.invokeWithArguments(convArgs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1283
        {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1284
            Class<?> dst = newType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1285
            Class<?> src = idType.returnType();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1286
            if (src != dst)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1287
                convResult = castToWrapper(convResult, dst);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1288
        }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1289
        MethodHandle target = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1290
        RuntimeException error = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1291
        try {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1292
            target = id.asType(newType);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1293
        } catch (RuntimeException ex) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1294
            error = ex;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1295
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1296
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1297
            System.out.println("convert "+id+ " to "+newType+" => "+target
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1298
                    +(error == null ? "" : " !! "+error));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1299
        if (positive && error != null)  throw error;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1300
        assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1301
        if (!positive)  return; // negative test failed as expected
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1302
        assertEquals(newType, target.type());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1303
        printCalled(target, id.toString(), args);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1304
        Object result = target.invokeWithArguments(args);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1305
        assertCalled(name, convArgs);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1306
        assertEquals(convResult, result);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1307
        if (verbosity >= 1)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1308
            System.out.print(':');
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1309
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1310
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1311
    @Test
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1312
    public void testVarargsCollector() throws Throwable {
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1313
        MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called",
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1314
                               MethodType.methodType(Object.class, String.class, Object[].class));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1315
        vac0 = vac0.bindTo("vac");
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1316
        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
  1317
        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
  1318
        testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1319
        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
  1320
            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
  1321
            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
  1322
        }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1323
    }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1324
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1325
    @Test
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1326
    public void testPermuteArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1327
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1328
        startTest("permuteArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1329
        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
  1330
        testPermuteArguments(6, Integer.class,  0, null,         30);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1331
        //testPermuteArguments(4, Integer.class,  2, long.class,    6);  // FIXME Fail_7
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1332
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1333
    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
  1334
        if (verbosity >= 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1335
            System.out.println("permuteArguments "+max+"*"+type1.getName()
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1336
                    +(t2c==0?"":"/"+t2c+"*"+type2.getName())
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1337
                    +(dilution > 0 ? " with dilution "+dilution : ""));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1338
        int t2pos = t2c == 0 ? 0 : 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1339
        for (int inargs = t2pos+1; inargs <= max; inargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1340
            Class<?>[] types = new Class<?>[inargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1341
            Arrays.fill(types, type1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1342
            if (t2c != 0) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1343
                // Fill in a middle range with type2:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1344
                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
  1345
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1346
            Object[] args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1347
            int numcases = 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1348
            for (int outargs = 0; outargs <= max; outargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1349
                if (outargs - inargs >= MAX_ARG_INCREASE)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1350
                int casStep = dilution + 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1351
                // Avoid some common factors:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1352
                while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1353
                       (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1354
                    casStep++;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1355
                testPermuteArguments(args, types, outargs, numcases, casStep);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1356
                numcases *= inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1357
                if (dilution > 10 && outargs >= 4) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1358
                    int[] reorder = new int[outargs];
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1359
                    // Do some special patterns, which we probably missed.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1360
                    // Replication of a single argument or argument pair.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1361
                    for (int i = 0; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1362
                        Arrays.fill(reorder, i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1363
                        testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1364
                        for (int d = 1; d <= 2; d++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1365
                            if (i + d >= inargs)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1366
                            for (int j = 1; j < outargs; j += 2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1367
                                reorder[j] += 1;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1368
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1369
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1370
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1371
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1372
                    // Repetition of a sequence of 3 or more arguments.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1373
                    for (int i = 1; i < inargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1374
                        for (int len = 3; len <= inargs; len++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1375
                            for (int j = 0; j < outargs; j++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1376
                                reorder[j] = (i + (j % len)) % inargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1377
                            testPermuteArguments(args, types, reorder);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1378
                            testPermuteArguments(args, types, reverse(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1379
                        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1380
                    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1381
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1382
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1383
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1384
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1385
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1386
    public void testPermuteArguments(Object[] args, Class<?>[] types,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1387
                                     int outargs, int numcases, int casStep) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1388
        int inargs = args.length;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1389
        int[] reorder = new int[outargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1390
        for (int cas = 0; cas < numcases; cas += casStep) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1391
            for (int i = 0, c = cas; i < outargs; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1392
                reorder[i] = c % inargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1393
                c /= inargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1394
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1395
            testPermuteArguments(args, types, reorder);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1396
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1397
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1398
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1399
    static int[] reverse(int[] reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1400
        reorder = reorder.clone();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1401
        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
  1402
            int j = reorder.length - 1 - i;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1403
            int tem = reorder[i];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1404
            reorder[i] = reorder[j];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1405
            reorder[j] = tem;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1406
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1407
        return reorder;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1408
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1409
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1410
    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
  1411
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1412
        if (args == null && types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1413
            int max = 0;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1414
            for (int j : reorder) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1415
                if (max < j)  max = j;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1416
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1417
            args = randomArgs(max+1, Integer.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1418
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1419
        if (args == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1420
            args = randomArgs(types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1421
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1422
        if (types == null) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1423
            types = new Class<?>[args.length];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1424
            for (int i = 0; i < args.length; i++)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1425
                types[i] = args[i].getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1426
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1427
        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
  1428
        assertTrue(inargs == types.length);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1429
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1430
            System.out.println("permuteArguments "+Arrays.toString(reorder));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1431
        Object[] permArgs = new Object[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1432
        Class<?>[] permTypes = new Class<?>[outargs];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1433
        for (int i = 0; i < outargs; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1434
            permArgs[i] = args[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1435
            permTypes[i] = types[reorder[i]];
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1436
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1437
        if (verbosity >= 4) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1438
            System.out.println("in args:   "+Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1439
            System.out.println("out args:  "+Arrays.asList(permArgs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1440
            System.out.println("in types:  "+Arrays.asList(types));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1441
            System.out.println("out types: "+Arrays.asList(permTypes));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1442
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1443
        MethodType inType  = MethodType.methodType(Object.class, types);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1444
        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
  1445
        MethodHandle target = varargsList(outargs).asType(outType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1446
        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
  1447
        if (verbosity >= 5)  System.out.println("newTarget = "+newTarget);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1448
        Object result = newTarget.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1449
        Object expected = Arrays.asList(permArgs);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1450
        if (!expected.equals(result)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1451
            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
  1452
            System.out.println("in args:   "+Arrays.asList(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1453
            System.out.println("out args:  "+expected);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1454
            System.out.println("bad args:  "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1455
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1456
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1457
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1458
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1459
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1460
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1461
    public void testSpreadArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1462
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1463
        startTest("spreadArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1464
        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
  1465
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1466
                System.out.println("spreadArguments "+argType);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1467
            // FIXME: enable _adapter_spread_args and fix Fail_2
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1468
            for (int nargs = 0; nargs < 10; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1469
                if (argType == int.class && nargs >= 6)  continue; // FIXME Fail_1
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1470
                for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1471
                    if (argType == int.class && pos > 0)  continue; // FIXME Fail_3
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1472
                     testSpreadArguments(argType, pos, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1473
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1474
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1475
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1476
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1477
    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
  1478
        countTest();
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1479
        Class<?> arrayType = java.lang.reflect.Array.newInstance(argType, 0).getClass();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1480
        MethodHandle target2 = varargsArray(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1481
        MethodHandle target = target2.asType(target2.type().generic());
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1482
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1483
            System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1484
        Object[] args = randomArgs(target2.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1485
        // make sure the target does what we think it does:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1486
        if (pos == 0 && nargs < 5 && !argType.isPrimitive()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1487
            Object[] check = (Object[]) (Object) target.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1488
            assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1489
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1490
                case 0:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1491
                    check = (Object[]) (Object) target.invokeExact();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1492
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1493
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1494
                case 1:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1495
                    check = (Object[]) (Object) target.invokeExact(args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1496
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1497
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1498
                case 2:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1499
                    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
  1500
                    assertArrayEquals(args, check);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1501
                    break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1502
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1503
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1504
        List<Class<?>> newParams = new ArrayList<Class<?>>(target2.type().parameterList());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1505
        {   // modify newParams in place
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1506
            List<Class<?>> spreadParams = newParams.subList(pos, nargs);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1507
            spreadParams.clear(); spreadParams.add(arrayType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1508
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1509
        MethodType newType = MethodType.methodType(arrayType, newParams);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1510
        MethodHandle result = target2.asSpreader(arrayType, nargs-pos);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1511
        assert(result.type() == newType) : Arrays.asList(result, newType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1512
        result = result.asType(newType.generic());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1513
        Object returnValue;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1514
        if (pos == 0) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1515
            Object args2 = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1516
            returnValue = result.invokeExact(args2);
4537
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
            Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1519
            args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1520
            returnValue = result.invokeWithArguments(args1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1521
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1522
        String argstr = Arrays.toString(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1523
        if (!argType.isPrimitive()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1524
            Object[] rv = (Object[]) returnValue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1525
            String rvs = Arrays.toString(rv);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1526
            if (!Arrays.equals(args, rv)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1527
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1528
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1529
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1530
                assertArrayEquals(args, rv);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1531
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1532
        } else if (argType == int.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1533
            String rvs = Arrays.toString((int[]) returnValue);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1534
            if (!argstr.equals(rvs)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1535
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1536
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1537
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1538
                assertEquals(argstr, rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1539
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1540
        } else if (argType == long.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1541
            String rvs = Arrays.toString((long[]) returnValue);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1542
            if (!argstr.equals(rvs)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1543
                System.out.println("method:   "+result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1544
                System.out.println("expected: "+argstr);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1545
                System.out.println("returned: "+rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1546
                assertEquals(argstr, rvs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1547
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1548
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1549
            // cannot test...
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  1550
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1551
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1552
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1553
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1554
    public void testCollectArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1555
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1556
        startTest("collectArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1557
        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
  1558
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1559
                System.out.println("collectArguments "+argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1560
            for (int nargs = 0; nargs < 10; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1561
                for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1562
                    if (argType == int.class)  continue; // FIXME Fail_4
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1563
                    testCollectArguments(argType, pos, nargs);
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
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1566
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1567
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1568
    public void testCollectArguments(Class<?> argType, int pos, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1569
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1570
        // 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
  1571
        MethodHandle fake = varargsArray(nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1572
        fake = changeArgTypes(fake, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1573
        MethodType newType = fake.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1574
        Object[] args = randomArgs(newType.parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1575
        // here is what should happen:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1576
        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1577
        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1578
        // 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
  1579
        MethodHandle target = varargsArray(pos+1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1580
        target = changeArgTypes(target, 0, pos, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1581
        target = changeArgTypes(target, pos, pos+1, Object[].class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1582
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1583
            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
  1584
        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
  1585
        Object[] returnValue = (Object[]) result.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1586
//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1587
//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1588
//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1589
        assertArrayEquals(collectedArgs, returnValue);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1590
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1591
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1592
    @Test
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1593
    public void testInsertArguments() throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1594
        if (CAN_SKIP_WORKING)  return;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1595
        startTest("insertArguments");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1596
        for (int nargs = 0; nargs <= 4; nargs++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1597
            for (int ins = 0; ins <= 4; ins++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1598
                if (ins > MAX_ARG_INCREASE)  continue;  // FIXME Fail_6
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1599
                for (int pos = 0; pos <= nargs; pos++) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1600
                    testInsertArguments(nargs, pos, ins);
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
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1605
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1606
    void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1607
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1608
        MethodHandle target = varargsArray(nargs + ins);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1609
        Object[] args = randomArgs(target.type().parameterArray());
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1610
        List<Object> resList = Arrays.asList(args);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1611
        List<Object> argsToPass = new ArrayList<Object>(resList);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1612
        List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1613
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1614
            System.out.println("insert: "+argsToInsert+" into "+target);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1615
        MethodHandle target2 = MethodHandles.insertArguments(target, pos,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1616
                (Object[]) argsToInsert.toArray());
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1617
        argsToInsert.clear();  // remove from argsToInsert
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1618
        Object res2 = target2.invokeWithArguments(argsToPass);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1619
        Object res2List = Arrays.asList((Object[])res2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1620
        if (verbosity >= 3)
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1621
            System.out.println("result: "+res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1622
        //if (!resList.equals(res2List))
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1623
        //    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
  1624
        assertEquals(resList, res2List);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1625
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1626
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1627
    @Test
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1628
    public void testFilterReturnValue() throws Throwable {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1629
        if (CAN_SKIP_WORKING)  return;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1630
        startTest("filterReturnValue");
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1631
        Class<?> classOfVCList = varargsList(1).invokeWithArguments(0).getClass();
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1632
        assertTrue(List.class.isAssignableFrom(classOfVCList));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1633
        for (int nargs = 0; nargs <= 3; nargs++) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1634
            for (Class<?> rtype : new Class[] { Object.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1635
                                                List.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1636
                                                int.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1637
                                                //byte.class, //FIXME: add this
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1638
                                                //long.class, //FIXME: add this
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1639
                                                CharSequence.class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1640
                                                String.class }) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1641
                testFilterReturnValue(nargs, rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1642
            }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1643
        }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1644
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1645
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1646
    void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1647
        countTest();
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1648
        MethodHandle target = varargsList(nargs, rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1649
        MethodHandle filter;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1650
        if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class))
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1651
            filter = varargsList(1);  // add another layer of list-ness
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1652
        else
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1653
            filter = MethodHandles.identity(rtype);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1654
        filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype));
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1655
        Object[] argsToPass = randomArgs(nargs, Object.class);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1656
        if (verbosity >= 3)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1657
            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
  1658
        MethodHandle target2 = MethodHandles.filterReturnValue(target, filter);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1659
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1660
            System.out.println("filtered target: "+target2);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1661
        // Simulate expected effect of filter on return value:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1662
        Object unfiltered = target.invokeWithArguments(argsToPass);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1663
        Object expected = filter.invokeWithArguments(unfiltered);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1664
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1665
            System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1666
        if (verbosity >= 4)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1667
            System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1668
        Object result = target2.invokeWithArguments(argsToPass);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1669
        if (verbosity >= 3)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1670
            System.out.println("result: "+result+" : "+result.getClass().getSimpleName());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1671
        if (!expected.equals(result))
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1672
            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
  1673
        assertEquals(expected, result);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1674
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1675
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1676
    @Test
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1677
    public void testFilterArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1678
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1679
        startTest("filterArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1680
        for (int nargs = 1; nargs <= 6; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1681
            for (int pos = 0; pos < nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1682
                testFilterArguments(nargs, pos);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1683
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1684
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1685
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1686
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1687
    void testFilterArguments(int nargs, int pos) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1688
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1689
        MethodHandle target = varargsList(nargs);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1690
        MethodHandle filter = varargsList(1);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1691
        filter = filter.asType(filter.type().generic());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1692
        Object[] argsToPass = randomArgs(nargs, Object.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1693
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1694
            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
  1695
        MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1696
        // Simulate expected effect of filter on arglist:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1697
        Object[] filteredArgs = argsToPass.clone();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1698
        filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1699
        List<Object> expected = Arrays.asList(filteredArgs);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1700
        Object result = target2.invokeWithArguments(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1701
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1702
            System.out.println("result: "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1703
        if (!expected.equals(result))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1704
            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
  1705
        assertEquals(expected, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1706
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1707
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1708
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1709
    public void testFoldArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1710
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1711
        startTest("foldArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1712
        for (int nargs = 0; nargs <= 4; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1713
            for (int fold = 0; fold <= nargs; fold++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1714
                for (int pos = 0; pos <= nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1715
                    testFoldArguments(nargs, pos, fold);
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
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1718
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1719
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1720
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1721
    void testFoldArguments(int nargs, int pos, int fold) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1722
        if (pos != 0)  return;  // can fold only at pos=0 for now
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1723
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1724
        MethodHandle target = varargsList(1 + nargs);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1725
        MethodHandle combine = varargsList(fold).asType(MethodType.genericMethodType(fold));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1726
        List<Object> argsToPass = Arrays.asList(randomArgs(nargs, Object.class));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1727
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1728
            System.out.println("fold "+target+" with "+combine);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1729
        MethodHandle target2 = MethodHandles.foldArguments(target, combine);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1730
        // Simulate expected effect of combiner on arglist:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1731
        List<Object> expected = new ArrayList<Object>(argsToPass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1732
        List<Object> argsToFold = expected.subList(pos, pos + fold);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1733
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1734
            System.out.println("fold: "+argsToFold+" into "+target2);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1735
        Object foldedArgs = combine.invokeWithArguments(argsToFold);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1736
        argsToFold.add(0, foldedArgs);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1737
        Object result = target2.invokeWithArguments(argsToPass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1738
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1739
            System.out.println("result: "+result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1740
        if (!expected.equals(result))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1741
            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
  1742
        assertEquals(expected, result);
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
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1746
    public void testDropArguments() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1747
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1748
        startTest("dropArguments");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1749
        for (int nargs = 0; nargs <= 4; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1750
            for (int drop = 1; drop <= 4; drop++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1751
                for (int pos = 0; pos <= nargs; pos++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1752
                    testDropArguments(nargs, pos, drop);
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
    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
  1759
        countTest();
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1760
        MethodHandle target = varargsArray(nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1761
        Object[] args = randomArgs(target.type().parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1762
        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1763
                Collections.nCopies(drop, Object.class).toArray(new Class[0]));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1764
        List<Object> resList = Arrays.asList(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1765
        List<Object> argsToDrop = new ArrayList<Object>(resList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1766
        for (int i = drop; i > 0; i--) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1767
            argsToDrop.add(pos, "blort#"+i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1768
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1769
        Object res2 = target2.invokeWithArguments(argsToDrop);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1770
        Object res2List = Arrays.asList((Object[])res2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1771
        //if (!resList.equals(res2List))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1772
        //    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
  1773
        assertEquals(resList, res2List);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1774
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1775
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1776
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1777
    public void testInvokers() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1778
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1779
        startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1780
        // exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1781
        Set<MethodType> done = new HashSet<MethodType>();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1782
        for (int i = 0; i <= 6; i++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1783
            MethodType gtype = MethodType.genericMethodType(i);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1784
            for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1785
                for (int j = -1; j < i; j++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1786
                    MethodType type = gtype;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1787
                    if (j < 0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1788
                        type = type.changeReturnType(argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1789
                    else if (argType == void.class)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1790
                        continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1791
                    else
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1792
                        type = type.changeParameterType(j, argType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1793
                    if (argType.isPrimitive() && j != i-1)  continue; // FIXME Fail_5
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1794
                    if (done.add(type))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1795
                        testInvokers(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1796
                    MethodType vtype = type.changeReturnType(void.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1797
                    if (done.add(vtype))
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1798
                        testInvokers(vtype);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1799
                }
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
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1802
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1803
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1804
    public void testInvokers(MethodType type) throws Throwable {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1805
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1806
            System.out.println("test invokers for "+type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1807
        int nargs = type.parameterCount();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1808
        boolean testRetCode = type.returnType() != void.class;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1809
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1810
                                MethodType.genericMethodType(0, true));
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1811
        assertTrue(target.isVarargsCollector());
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1812
        target = target.asType(type);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1813
        Object[] args = randomArgs(type.parameterArray());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1814
        List<Object> targetPlusArgs = new ArrayList<Object>(Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1815
        targetPlusArgs.add(0, target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1816
        int code = (Integer) invokee(args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1817
        Object log = logEntry("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1818
        assertEquals(log.hashCode(), code);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1819
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1820
        MethodHandle inv;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1821
        Object result;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1822
        // exact invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1823
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1824
        calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1825
        inv = MethodHandles.exactInvoker(type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1826
        result = inv.invokeWithArguments(targetPlusArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1827
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1828
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1829
        // generic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1830
        countTest();
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
  1831
        inv = MethodHandles.invoker(type);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1832
        if (nargs <= 3 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1833
            calledLog.clear();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1834
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1835
            case 0:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1836
                result = inv.invokeExact(target);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1837
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1838
            case 1:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1839
                result = inv.invokeExact(target, args[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1840
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1841
            case 2:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1842
                result = inv.invokeExact(target, args[0], args[1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1843
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1844
            case 3:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1845
                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
  1846
                break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1847
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1848
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1849
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1850
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1851
        calledLog.clear();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1852
        result = inv.invokeWithArguments(targetPlusArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1853
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1854
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1855
        // varargs invoker #0
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1856
        calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1857
        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
  1858
        if (type.returnType() == Object.class) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1859
            result = inv.invokeExact(target, args);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1860
        } 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
  1861
            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
  1862
        } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1863
            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
  1864
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1865
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1866
        assertCalled("invokee", args);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1867
        if (nargs >= 1 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1868
            // varargs invoker #1
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1869
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1870
            inv = MethodHandles.spreadInvoker(type, 1);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1871
            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
  1872
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1873
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1874
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1875
        if (nargs >= 2 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1876
            // varargs invoker #2
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1877
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1878
            inv = MethodHandles.spreadInvoker(type, 2);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1879
            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
  1880
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1881
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1882
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1883
        if (nargs >= 3 && type == type.generic()) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1884
            // varargs invoker #3
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1885
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1886
            inv = MethodHandles.spreadInvoker(type, 3);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1887
            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
  1888
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1889
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1890
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1891
        for (int k = 0; k <= nargs; k++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1892
            // varargs invoker #0..N
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1893
            countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1894
            calledLog.clear();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1895
            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
  1896
            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
  1897
                                  .appendParameterTypes(Object[].class)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1898
                                  .insertParameterTypes(0, MethodHandle.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1899
            assertEquals(expType, inv.type());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1900
            List<Object> targetPlusVarArgs = new ArrayList<Object>(targetPlusArgs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1901
            List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1902
            Object[] tail = tailList.toArray();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1903
            tailList.clear(); tailList.add(tail);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1904
            result = inv.invokeWithArguments(targetPlusVarArgs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1905
            if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1906
            assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1907
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1908
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1909
        // dynamic invoker
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1910
        countTest();
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  1911
        CallSite site = new MutableCallSite(type);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  1912
        inv = site.dynamicInvoker();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1913
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1914
        // 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
  1915
        try {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1916
            result = inv.invokeWithArguments(args);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1917
            assertTrue("should not reach here", false);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1918
        } catch (IllegalStateException ex) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1919
            String msg = ex.getMessage();
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1920
            assertTrue(msg, msg.contains("site"));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1921
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1922
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1923
        // 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
  1924
        site.setTarget(target);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1925
        calledLog.clear();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1926
        result = inv.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1927
        if (testRetCode)  assertEquals(code, result);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1928
        assertCalled("invokee", args);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1929
    }
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
    static Object invokee(Object... args) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1932
        return called("invokee", args).hashCode();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1933
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1934
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1935
    private static final String MISSING_ARG = "missingArg";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1936
    static Object targetIfEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1937
        return called("targetIfEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1938
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1939
    static Object fallbackIfNotEquals() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1940
        return called("fallbackIfNotEquals");
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1941
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1942
    static Object targetIfEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1943
        assertEquals(x, MISSING_ARG);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1944
        return called("targetIfEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1945
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1946
    static Object fallbackIfNotEquals(Object x) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1947
        assertFalse(x.toString(), x.equals(MISSING_ARG));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1948
        return called("fallbackIfNotEquals", x);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1949
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1950
    static Object targetIfEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1951
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1952
        return called("targetIfEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1953
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1954
    static Object fallbackIfNotEquals(Object x, Object y) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1955
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1956
        return called("fallbackIfNotEquals", x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1957
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1958
    static Object targetIfEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1959
        assertEquals(x, y);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1960
        return called("targetIfEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1961
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1962
    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1963
        assertFalse(x.toString(), x.equals(y));
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1964
        return called("fallbackIfNotEquals", x, y, z);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1965
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  1966
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1967
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1968
    public void testGuardWithTest() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1969
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1970
        startTest("guardWithTest");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1971
        for (int nargs = 0; nargs <= 3; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1972
            if (nargs != 2)  continue;  // FIXME: test more later
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1973
            testGuardWithTest(nargs, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1974
            testGuardWithTest(nargs, String.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1975
        }
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
    void testGuardWithTest(int nargs, Class<?> argClass) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1978
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1979
        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1980
        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1981
        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1982
        while (test.type().parameterCount() < nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1983
            test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1984
        while (test.type().parameterCount() > nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1985
            test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1986
        if (argClass != Object.class) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1987
            test = changeArgTypes(test, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1988
            target = changeArgTypes(target, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1989
            fallback = changeArgTypes(fallback, argClass);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1990
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1991
        MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1992
        assertEquals(target.type(), mh.type());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1993
        Object[][] argLists = {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1994
            { },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1995
            { "foo" }, { MISSING_ARG },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1996
            { "foo", "foo" }, { "foo", "bar" },
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1997
            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1998
        };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  1999
        for (Object[] argList : argLists) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2000
            if (argList.length != nargs)  continue;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2001
            boolean equals;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2002
            switch (nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2003
            case 0:   equals = true; break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2004
            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2005
            default:  equals = argList[0].equals(argList[1]); break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2006
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2007
            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2008
            if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2009
                System.out.println(logEntry(willCall, argList));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2010
            Object result = mh.invokeWithArguments(argList);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2011
            assertCalled(willCall, argList);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2012
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2013
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2014
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2015
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2016
    public void testCatchException() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2017
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2018
        startTest("catchException");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2019
        for (int nargs = 2; nargs <= 6; nargs++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2020
            for (int ti = 0; ti <= 1; ti++) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2021
                boolean throwIt = (ti != 0);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2022
                testCatchException(int.class, new ClassCastException("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2023
                testCatchException(void.class, new java.io.IOException("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2024
                testCatchException(String.class, new LinkageError("testing"), throwIt, nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2025
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2026
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2027
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2028
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2029
    private static <T extends Throwable>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2030
    Object throwOrReturn(Object normal, T exception) throws T {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2031
        if (exception != null)  throw exception;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2032
        return normal;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2033
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2034
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2035
    void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2036
        countTest();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2037
        if (verbosity >= 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2038
            System.out.println("catchException rt="+returnType+" throw="+throwIt+" nargs="+nargs);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2039
        Class<? extends Throwable> exType = thrown.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2040
        MethodHandle throwOrReturn
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2041
                = PRIVATE.findStatic(MethodHandlesTest.class, "throwOrReturn",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2042
                    MethodType.methodType(Object.class, Object.class, Throwable.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  2043
        MethodHandle thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2044
        while (thrower.type().parameterCount() < nargs)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2045
            thrower = MethodHandles.dropArguments(thrower, thrower.type().parameterCount(), Object.class);
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2046
        MethodHandle catcher = varargsList(1+nargs).asType(MethodType.genericMethodType(1+nargs));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2047
        MethodHandle target = MethodHandles.catchException(thrower,
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
  2048
                thrown.getClass(), catcher);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2049
        assertEquals(thrower.type(), target.type());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2050
        //System.out.println("catching with "+target+" : "+throwOrReturn);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2051
        Object[] args = randomArgs(nargs, Object.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2052
        args[1] = (throwIt ? thrown : null);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2053
        Object returned = target.invokeWithArguments(args);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2054
        //System.out.println("return from "+target+" : "+returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2055
        if (!throwIt) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2056
            assertSame(args[0], returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2057
        } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2058
            List<Object> catchArgs = new ArrayList<Object>(Arrays.asList(args));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2059
            catchArgs.add(0, thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2060
            assertEquals(catchArgs, returned);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2061
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2062
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2063
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2064
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2065
    public void testThrowException() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2066
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2067
        startTest("throwException");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2068
        testThrowException(int.class, new ClassCastException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2069
        testThrowException(void.class, new java.io.IOException("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2070
        testThrowException(String.class, new LinkageError("testing"));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2071
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2072
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2073
    void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2074
        countTest();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2075
        Class<? extends Throwable> exType = thrown.getClass();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2076
        MethodHandle target = MethodHandles.throwException(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2077
        //System.out.println("throwing with "+target+" : "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2078
        MethodType expectedType = MethodType.methodType(returnType, exType);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2079
        assertEquals(expectedType, target.type());
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2080
        target = target.asType(target.type().generic());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2081
        Throwable caught = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2082
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2083
            Object res = target.invokeExact((Object) thrown);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2084
            fail("got "+res+" instead of throwing "+thrown);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2085
        } catch (Throwable ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2086
            if (ex != thrown) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2087
                if (ex instanceof Error)  throw (Error)ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2088
                if (ex instanceof RuntimeException)  throw (RuntimeException)ex;
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
            caught = ex;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2091
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2092
        assertSame(thrown, caught);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2093
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2094
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2095
    @Test
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2096
    public void testCastFailure() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2097
        if (CAN_SKIP_WORKING)  return;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2098
        startTest("testCastFailure");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2099
        testCastFailure("cast/argument", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2100
        testCastFailure("unbox/argument", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2101
        testCastFailure("cast/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2102
        testCastFailure("unbox/return", 11000);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2103
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2104
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2105
    static class Surprise {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2106
        public MethodHandle asMethodHandle() {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2107
            return VALUE.bindTo(this);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2108
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2109
        Object value(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2110
            trace("value", x);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2111
            if (boo != null)  return boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2112
            return x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2113
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2114
        Object boo;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2115
        void boo(Object x) { boo = x; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2116
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2117
        static void trace(String x, Object y) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2118
            if (verbosity > 8) System.out.println(x+"="+y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2119
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2120
        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
  2121
        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
  2122
        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
  2123
        static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2124
        static {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2125
            try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2126
                VALUE = PRIVATE.findVirtual(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2127
                    Surprise.class, "value",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2128
                        MethodType.methodType(Object.class, Object.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2129
                REF_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2130
                    Surprise.class, "refIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2131
                        MethodType.methodType(Object.class, Object.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2132
                BOX_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2133
                    Surprise.class, "boxIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2134
                        MethodType.methodType(Integer.class, Integer.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2135
                INT_IDENTITY = PRIVATE.findStatic(
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2136
                    Surprise.class, "intIdentity",
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2137
                        MethodType.methodType(int.class, int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2138
            } catch (Exception ex) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2139
                throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2140
            }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2141
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2142
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2143
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2144
    void testCastFailure(String mode, int okCount) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2145
        countTest(false);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2146
        if (verbosity > 2)  System.out.println("mode="+mode);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2147
        Surprise boo = new Surprise();
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2148
        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
  2149
        if (mode.endsWith("/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2150
            if (mode.equals("unbox/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2151
                // 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
  2152
                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
  2153
                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
  2154
            } else if (mode.equals("cast/return")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2155
                // 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
  2156
                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
  2157
                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
  2158
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2159
        } else if (mode.endsWith("/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2160
            MethodHandle callee = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2161
            if (mode.equals("unbox/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2162
                // fail on handing surprise to int argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2163
                callee = Surprise.INT_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2164
            } else if (mode.equals("cast/argument")) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2165
                // fail on handing surprise to Integer argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2166
                callee = Surprise.BOX_IDENTITY;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2167
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2168
            if (callee != null) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2169
                callee = callee.asType(MethodType.genericMethodType(1));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2170
                surprise = MethodHandles.filterArguments(callee, 0, surprise);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2171
                identity = MethodHandles.filterArguments(callee, 0, identity);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2172
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2173
        }
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
  2174
        assertNotSame(mode, surprise, surprise0);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2175
        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
  2176
        surprise = surprise.asType(MethodType.genericMethodType(1));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2177
        Object x = 42;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2178
        for (int i = 0; i < okCount; i++) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2179
            Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2180
            assertEquals(x, y);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2181
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2182
            assertEquals(x, z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2183
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2184
        boo.boo("Boo!");
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2185
        Object y = identity.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2186
        assertEquals(x, y);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2187
        try {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2188
            Object z = surprise.invokeExact(x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2189
            System.out.println("Failed to throw; got z="+z);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2190
            assertTrue(false);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2191
        } catch (ClassCastException ex) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2192
            if (verbosity > 2)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2193
                System.out.println("caught "+ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2194
            if (verbosity > 3)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2195
                ex.printStackTrace();
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2196
            assertTrue(true);  // all is well
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2197
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2198
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2199
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2200
    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
  2201
        called("userMethod", o, s, i);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2202
        return null;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2203
    }
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2204
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2205
    @Test
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2206
    public void testUserClassInSignature() throws Throwable {
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2207
        if (CAN_SKIP_WORKING)  return;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2208
        startTest("testUserClassInSignature");
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2209
        Lookup lookup = MethodHandles.lookup();
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2210
        String name; MethodType mt; MethodHandle mh;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2211
        Object[] args;
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2212
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2213
        // Try a static method.
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2214
        name = "userMethod";
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2215
        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
  2216
        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
  2217
        assertEquals(mt, mh.type());
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2218
        assertEquals(Example.class, mh.type().returnType());
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2219
        args = randomArgs(mh.type().parameterArray());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2220
        mh.invokeWithArguments(args);
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2221
        assertCalled(name, args);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2222
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2223
        // Try a virtual method.
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2224
        name = "v2";
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2225
        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
  2226
        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
  2227
        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
  2228
        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
  2229
        args = randomArgs(mh.type().parameterArray());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2230
        mh.invokeWithArguments(args);
5723
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2231
        assertCalled(name, args);
a58a0eed34b0 6939196: method handle signatures off the boot class path get linkage errors
jrose
parents: 5722
diff changeset
  2232
    }
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2233
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2234
    static void runForRunnable() {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2235
        called("runForRunnable");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2236
    }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2237
    public interface Fooable {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2238
        Object foo(Fooable x, Object y);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2239
        // this is for randomArg:
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2240
        public class Impl implements Fooable {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2241
            public Object foo(Fooable x, Object y) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2242
                throw new RuntimeException("do not call");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2243
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2244
            final String name;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2245
            public Impl() { name = "Fooable#"+nextArg(); }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2246
            @Override public String toString() { return name; }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2247
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2248
    }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2249
    static Object fooForFooable(Fooable x, Object y) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2250
        return called("fooForFooable", x, y);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2251
    }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2252
    public static class MyCheckedException extends Exception {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2253
    }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2254
    public interface WillThrow {
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2255
        void willThrow() throws MyCheckedException;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2256
    }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2257
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2258
    @Test
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2259
    public void testAsInstance() throws Throwable {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2260
        if (CAN_SKIP_WORKING)  return;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2261
        Lookup lookup = MethodHandles.lookup();
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2262
        {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2263
            MethodType mt = MethodType.methodType(void.class);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2264
            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
  2265
            Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2266
            proxy.run();
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2267
            assertCalled("runForRunnable");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2268
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2269
        {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2270
            MethodType mt = MethodType.methodType(Object.class, Fooable.class, Object.class);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2271
            MethodHandle mh = lookup.findStatic(MethodHandlesTest.class, "fooForFooable", mt);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2272
            Fooable proxy = MethodHandleProxies.asInterfaceInstance(Fooable.class, mh);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2273
            Object[] args = randomArgs(mt.parameterArray());
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2274
            Object result = proxy.foo((Fooable) args[0], args[1]);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2275
            assertCalled("fooForFooable", args);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2276
            assertEquals(result, logEntry("fooForFooable", args));
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2277
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2278
        for (Throwable ex : new Throwable[] { new NullPointerException("ok"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2279
                                              new InternalError("ok"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2280
                                              new Throwable("fail"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2281
                                              new Exception("fail"),
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2282
                                              new MyCheckedException()
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2283
                                            }) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2284
            MethodHandle mh = MethodHandles.throwException(void.class, Throwable.class);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2285
            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
  2286
            WillThrow proxy = MethodHandleProxies.asInterfaceInstance(WillThrow.class, mh);
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2287
            try {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2288
                proxy.willThrow();
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2289
                System.out.println("Failed to throw: "+ex);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2290
                assertTrue(false);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2291
            } catch (Throwable ex1) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2292
                if (verbosity > 2) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2293
                    System.out.println("throw "+ex);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2294
                    System.out.println("catch "+(ex == ex1 ? "UNWRAPPED" : ex1));
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2295
                }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2296
                if (ex instanceof RuntimeException ||
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2297
                    ex instanceof Error) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2298
                    assertSame("must pass unchecked exception out without wrapping", ex, ex1);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2299
                } else if (ex instanceof MyCheckedException) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2300
                    assertSame("must pass declared exception out without wrapping", ex, ex1);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2301
                } else {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2302
                    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
  2303
                    if (!(ex1 instanceof UndeclaredThrowableException) || ex1.getCause() != ex) {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2304
                        ex1.printStackTrace();
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2305
                    }
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2306
                    assertSame(ex, ex1.getCause());
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2307
                    UndeclaredThrowableException utex = (UndeclaredThrowableException) ex1;
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2308
                }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2309
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2310
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2311
        // Test error checking:
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2312
        for (Class<?> nonSAM : new Class[] { Object.class,
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2313
                                             String.class,
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2314
                                             CharSequence.class,
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2315
                                             Example.class }) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2316
            try {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2317
                MethodHandleProxies.asInterfaceInstance(nonSAM, varargsArray(0));
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2318
                System.out.println("Failed to throw");
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2319
                assertTrue(false);
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2320
            } catch (IllegalArgumentException ex) {
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2321
            }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2322
        }
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
  2323
    }
10080
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2324
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2325
    @Test
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2326
    public void testRunnableProxy() throws Throwable {
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2327
        if (CAN_SKIP_WORKING)  return;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2328
        startTest("testRunnableProxy");
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2329
        MethodHandles.Lookup lookup = MethodHandles.lookup();
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2330
        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
  2331
        Runnable r = MethodHandleProxies.asInterfaceInstance(Runnable.class, run);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2332
        testRunnableProxy(r);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2333
        assertCalled("runForRunnable");
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2334
    }
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2335
    private static void testRunnableProxy(Runnable r) {
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2336
        //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
  2337
        r.run();
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2338
        Object o = r;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2339
        r = null;
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2340
        boolean eq = (o == o);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2341
        int     hc = System.identityHashCode(o);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2342
        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
  2343
        Object expect = Arrays.asList(st, eq, hc);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2344
        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
  2345
        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
  2346
        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
  2347
        assertEquals(expect, actual);
eb387b9bb282 7058630: JSR 292 method handle proxy violates contract for Object methods
jrose
parents: 10078
diff changeset
  2348
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2349
}
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
  2350
// Local abbreviated copy of sun.invoke.util.ValueConversions
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2351
class ValueConversions {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2352
    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2353
    private static final Object[] NO_ARGS_ARRAY = {};
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2354
    private static Object[] makeArray(Object... args) { return args; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2355
    private static Object[] array() { return NO_ARGS_ARRAY; }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2356
    private static Object[] array(Object a0)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2357
                { return makeArray(a0); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2358
    private static Object[] array(Object a0, Object a1)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2359
                { return makeArray(a0, a1); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2360
    private static Object[] array(Object a0, Object a1, Object a2)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2361
                { return makeArray(a0, a1, a2); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2362
    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
  2363
                { return makeArray(a0, a1, a2, a3); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2364
    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
  2365
                                  Object a4)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2366
                { return makeArray(a0, a1, a2, a3, a4); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2367
    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
  2368
                                  Object a4, Object a5)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2369
                { return makeArray(a0, a1, a2, a3, a4, a5); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2370
    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
  2371
                                  Object a4, Object a5, Object a6)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2372
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2373
    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
  2374
                                  Object a4, Object a5, Object a6, Object a7)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2375
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2376
    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
  2377
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2378
                                  Object a8)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2379
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2380
    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
  2381
                                  Object a4, Object a5, Object a6, Object a7,
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2382
                                  Object a8, Object a9)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2383
                { 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
  2384
    static MethodHandle[] makeArrays() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2385
        ArrayList<MethodHandle> arrays = new ArrayList<MethodHandle>();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2386
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2387
        for (;;) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2388
            int nargs = arrays.size();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2389
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2390
            String name = "array";
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2391
            MethodHandle array = null;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2392
            try {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2393
                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
  2394
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2395
                // break from loop!
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2396
            }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2397
            if (array == null)  break;
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2398
            arrays.add(array);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2399
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2400
        assertTrue(arrays.size() == 11);  // current number of methods
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2401
        return arrays.toArray(new MethodHandle[0]);
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2402
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2403
    static final MethodHandle[] ARRAYS = makeArrays();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2404
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2405
    /** Return a method handle that takes the indicated number of Object
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2406
     *  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
  2407
     */
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2408
    public static MethodHandle varargsArray(int nargs) {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2409
        if (nargs < ARRAYS.length)
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2410
            return ARRAYS[nargs];
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2411
        return MethodHandles.identity(Object[].class).asCollector(Object[].class, nargs);
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2412
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2413
    public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2414
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2415
        MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2416
        MethodHandle mh = varargsArray(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2417
        if (arrayType != Object[].class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2418
            mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2419
        return mh.asType(vaType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2420
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2421
    static Object changeArrayType(Class<?> arrayType, Object[] a) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2422
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2423
        if (!elemType.isPrimitive())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2424
            return Arrays.copyOf(a, a.length, arrayType.asSubclass(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2425
        Object b = java.lang.reflect.Array.newInstance(elemType, a.length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2426
        for (int i = 0; i < a.length; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2427
            java.lang.reflect.Array.set(b, i, a[i]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2428
        return b;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2429
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2430
    private static final MethodHandle CHANGE_ARRAY_TYPE;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2431
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2432
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2433
            CHANGE_ARRAY_TYPE = IMPL_LOOKUP.findStatic(ValueConversions.class, "changeArrayType",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2434
                                                       MethodType.methodType(Object.class, Class.class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2435
        } catch (NoSuchMethodException | IllegalAccessException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2436
            Error err = new InternalError("uncaught exception");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2437
            err.initCause(ex);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2438
            throw err;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2439
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2440
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2441
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2442
    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
  2443
    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
  2444
    private static List<Object> list() { return NO_ARGS_LIST; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2445
    private static List<Object> list(Object a0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2446
                { return makeList(a0); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2447
    private static List<Object> list(Object a0, Object a1)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2448
                { return makeList(a0, a1); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2449
    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
  2450
                { return makeList(a0, a1, a2); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2451
    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
  2452
                { return makeList(a0, a1, a2, a3); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2453
    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
  2454
                                     Object a4)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2455
                { return makeList(a0, a1, a2, a3, a4); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2456
    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
  2457
                                     Object a4, Object a5)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2458
                { return makeList(a0, a1, a2, a3, a4, a5); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2459
    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
  2460
                                     Object a4, Object a5, Object a6)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2461
                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2462
    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
  2463
                                     Object a4, Object a5, Object a6, Object a7)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2464
                { 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
  2465
    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
  2466
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2467
                                     Object a8)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2468
                { 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
  2469
    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
  2470
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2471
                                     Object a8, Object a9)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2472
                { 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
  2473
    static MethodHandle[] makeLists() {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2474
        ArrayList<MethodHandle> lists = new ArrayList<MethodHandle>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2475
        MethodHandles.Lookup lookup = IMPL_LOOKUP;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2476
        for (;;) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2477
            int nargs = lists.size();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2478
            MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2479
            String name = "list";
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2480
            MethodHandle list = null;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2481
            try {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2482
                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
  2483
            } catch (ReflectiveOperationException ex) {
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2484
                // break from loop!
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2485
            }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2486
            if (list == null)  break;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2487
            lists.add(list);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2488
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2489
        assertTrue(lists.size() == 11);  // current number of methods
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2490
        return lists.toArray(new MethodHandle[0]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2491
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2492
    static final MethodHandle[] LISTS = makeLists();
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2493
    static final MethodHandle AS_LIST;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2494
    static {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2495
        try {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2496
            AS_LIST = IMPL_LOOKUP.findStatic(Arrays.class, "asList", MethodType.methodType(List.class, Object[].class));
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2497
        } catch (Exception ex) { throw new RuntimeException(ex); }
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2498
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2499
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2500
    /** 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
  2501
     *  arguments and returns List.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2502
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2503
    public static MethodHandle varargsList(int nargs) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2504
        if (nargs < LISTS.length)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2505
            return LISTS[nargs];
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
  2506
        return AS_LIST.asCollector(Object[].class, nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 4535
diff changeset
  2507
    }
4535
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2508
}
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2509
// 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
  2510
// the package itself.
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2511
class PackageSibling {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2512
    static Lookup lookup() {
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2513
        return MethodHandles.lookup();
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2514
    }
50426a1436f0 6891770: JSR 292 API needs initial unit tests
jrose
parents:
diff changeset
  2515
}