jdk/test/java/lang/invoke/VarargsArrayTest.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
parent 31178 6ad68e35506d
child 37779 7c84df693837
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     1
/*
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     4
 *
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     8
 *
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    13
 * accompanied this code).
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    14
 *
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    18
 *
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    21
 * questions.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    22
 */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    23
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    24
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    25
import java.lang.invoke.MethodHandle;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    26
import java.lang.invoke.MethodHandleHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    27
import java.lang.invoke.MethodType;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    28
import sun.invoke.util.Wrapper;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    29
import java.util.Arrays;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    30
import java.util.Collections;
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    31
import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    32
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    33
/* @test
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    34
 * @summary unit tests for varargs array methods: MethodHandleInfo.varargsArray(int),
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    35
 *          MethodHandleInfo.varargsArray(Class,int) & MethodHandleInfo.varargsList(int)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    36
 * @modules java.base/sun.invoke.util
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    37
 * @library /lib/testlibrary /lib/testlibrary/jsr292
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    38
 * @compile/module=java.base java/lang/invoke/MethodHandleHelper.java
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    39
 * @run main/bootclasspath VarargsArrayTest
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    40
 * @run main/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.START_ARITY=250
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    41
 *                         VarargsArrayTest
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    42
 */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    43
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    44
/* This might take a while and burn lots of metadata:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    45
 * @run main/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.EXHAUSTIVE=true VarargsArrayTest
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    46
 */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    47
public class VarargsArrayTest {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    48
    private static final Class<?> CLASS = VarargsArrayTest.class;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    49
    private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    50
    private static final int START_ARITY = Integer.getInteger(CLASS.getSimpleName()+".START_ARITY", 0);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    51
    private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE");
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    52
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    53
    public static void main(String[] args) throws Throwable {
31178
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    54
        CodeCacheOverflowProcessor.runMHTest(VarargsArrayTest::test);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    55
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    56
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    57
    public static void test() throws Throwable {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    58
        testVarargsArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    59
        testVarargsReferenceArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    60
        testVarargsPrimitiveArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    61
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    62
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    63
    public static void testVarargsArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    64
        final int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    65
        final int MAX = MAX_ARITY-2;  // 253+1 would cause parameter overflow with 'this' added
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    66
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    67
            MethodHandle target = MethodHandleHelper.varargsArray(nargs);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    68
            Object[] args = new Object[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    69
            for (int i = 0; i < nargs; i++)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    70
                args[i] = "#"+i;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    71
            Object res = target.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    72
            assertArrayEquals(args, (Object[])res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    73
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    74
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    75
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    76
    public static void testVarargsReferenceArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    77
        testTypedVarargsArray(Object[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    78
        testTypedVarargsArray(String[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    79
        testTypedVarargsArray(Number[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    80
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    81
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    82
    public static void testVarargsPrimitiveArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    83
        testTypedVarargsArray(int[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    84
        testTypedVarargsArray(long[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    85
        testTypedVarargsArray(byte[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    86
        testTypedVarargsArray(boolean[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    87
        testTypedVarargsArray(short[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    88
        testTypedVarargsArray(char[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    89
        testTypedVarargsArray(float[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    90
        testTypedVarargsArray(double[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    91
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    92
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    93
    private static int nextArgCount(int nargs, int density, int MAX) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    94
        if (EXHAUSTIVE)  return nargs + 1;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    95
        if (nargs >= MAX)  return Integer.MAX_VALUE;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    96
        int BOT = 20, TOP = MAX-5;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    97
        if (density < 10) { BOT = 10; MAX = TOP-2; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    98
        if (nargs <= BOT || nargs >= TOP) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    99
            ++nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   100
        } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   101
            int bump = Math.max(1, 100 / density);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   102
            nargs += bump;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   103
            if (nargs > TOP)  nargs = TOP;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   104
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   105
        return nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   106
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   107
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   108
    private static void testTypedVarargsArray(Class<?> arrayType) throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   109
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   110
        int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   111
        int MAX = MAX_ARITY-2;  // 253+1 would cause parameter overflow with 'this' added
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   112
        int density = 3;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   113
        if (elemType == int.class || elemType == long.class)  density = 7;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   114
        if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   115
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   116
            Object[] args = makeTestArray(elemType, nargs);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
   117
            MethodHandle varargsArray = MethodHandleHelper.varargsArray(arrayType, nargs);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   118
            MethodType vaType = varargsArray.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   119
            assertEquals(arrayType, vaType.returnType());
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   120
            if (nargs != 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   121
                assertEquals(elemType, vaType.parameterType(0));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   122
                assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   123
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   124
            assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)),
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   125
                         vaType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   126
            Object res = varargsArray.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   127
            assertEquals(res.getClass(), arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   128
            String resString = toArrayString(res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   129
            assertEquals(Arrays.toString(args), resString);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   130
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   131
            MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   132
            MethodType stype = spreader.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   133
            assert(stype == MethodType.methodType(arrayType, arrayType));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   134
            if (nargs <= 5) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   135
                // invoke target as a spreader also:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   136
                @SuppressWarnings("cast")
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   137
                Object res2 = spreader.invokeWithArguments((Object)res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   138
                String res2String = toArrayString(res2);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   139
                assertEquals(Arrays.toString(args), res2String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   140
                // invoke the spreader on a generic Object[] array; check for error
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   141
                try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   142
                    Object res3 = spreader.invokeWithArguments((Object)args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   143
                    String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   144
                    assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   145
                    assertEquals(Arrays.toString(args), res3String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   146
                } catch (ClassCastException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   147
                    assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   148
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   149
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   150
            if (nargs == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   151
                // invoke spreader on null arglist
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   152
                Object res3 = spreader.invokeWithArguments((Object)null);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   153
                String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   154
                assertEquals(Arrays.toString(args), res3String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   155
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   156
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   157
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   158
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   159
    private static Object[] makeTestArray(Class<?> elemType, int len) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   160
        Wrapper elem = null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   161
        if (elemType.isPrimitive())
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   162
            elem = Wrapper.forPrimitiveType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   163
        else if (Wrapper.isWrapperType(elemType))
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   164
            elem = Wrapper.forWrapperType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   165
        Object[] args = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   166
        for (int i = 0; i < len; i++) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   167
            Object arg = i * 100;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   168
            if (elem == null) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   169
                if (elemType == String.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   170
                    arg = "#"+arg;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   171
                arg = elemType.cast(arg);  // just to make sure
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   172
            } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   173
                switch (elem) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   174
                    case BOOLEAN: arg = (i % 3 == 0);           break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   175
                    case CHAR:    arg = 'a' + i;                break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   176
                    case LONG:    arg = (long)i * 1000_000_000; break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   177
                    case FLOAT:   arg = (float)i / 100;         break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   178
                    case DOUBLE:  arg = (double)i / 1000_000;   break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   179
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   180
                arg = elem.cast(arg, elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   181
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   182
            args[i] = arg;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   183
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   184
        return args;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   185
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   186
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   187
    private static String toArrayString(Object a) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   188
        if (a == null)  return "null";
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   189
        Class<?> elemType = a.getClass().getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   190
        if (elemType == null)  return a.toString();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   191
        if (elemType.isPrimitive()) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   192
            switch (Wrapper.forPrimitiveType(elemType)) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   193
                case INT:      return Arrays.toString((int[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   194
                case BYTE:     return Arrays.toString((byte[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   195
                case BOOLEAN:  return Arrays.toString((boolean[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   196
                case SHORT:    return Arrays.toString((short[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   197
                case CHAR:     return Arrays.toString((char[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   198
                case FLOAT:    return Arrays.toString((float[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   199
                case LONG:     return Arrays.toString((long[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   200
                case DOUBLE:   return Arrays.toString((double[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   201
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   202
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   203
        return Arrays.toString((Object[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   204
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   205
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   206
    public static void assertArrayEquals(Object[] arr1, Object[] arr2) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   207
        if (arr1 == null && arr2 == null)  return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   208
        if (arr1 != null && arr2 != null && arr1.length == arr2.length) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   209
            for (int i = 0; i < arr1.length; i++) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   210
                assertEquals(arr1[i], arr2[i]);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   211
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   212
            return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   213
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   214
        throw new AssertionError(Arrays.deepToString(arr1) + " != " + Arrays.deepToString(arr2));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   215
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   216
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   217
    public static void assertEquals(Object o1, Object o2) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   218
        if (o1 == null && o2 == null)    return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   219
        if (o1 != null && o1.equals(o2)) return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   220
        throw new AssertionError(o1 + " != " + o2);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   221
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   222
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   223
    public static void assertTrue(String msg, boolean b) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   224
        if (!b) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   225
            throw new AssertionError(msg);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   226
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   227
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   228
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   229
    public static void assertFalse(String msg, boolean b) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   230
        assertTrue(msg, !b);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   231
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   232
}