jdk/test/java/lang/invoke/VarargsArrayTest.java
author vlivanov
Wed, 10 Sep 2014 19:19:47 +0400
changeset 26466 3bbb6a284bd4
child 31178 6ad68e35506d
permissions -rw-r--r--
8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke Reviewed-by: psandoz, iignatyev
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
/*
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
package java.lang.invoke;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    25
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    26
import sun.invoke.util.Wrapper;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    27
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    28
import java.util.Arrays;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    29
import java.util.Collections;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    30
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    31
/* @test
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    32
 * @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
    33
 *          MethodHandleInfo.varargsArray(Class,int) & MethodHandleInfo.varargsList(int)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    34
 *
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    35
 * @run main/bootclasspath java.lang.invoke.VarargsArrayTest
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    36
 * @run main/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.START_ARITY=250
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    37
 *                         java.lang.invoke.VarargsArrayTest
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    38
 */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    39
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    40
/* This might take a while and burn lots of metadata:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    41
 * @run main/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.EXHAUSTIVE=true java.lang.invoke.VarargsArrayTest
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
public class VarargsArrayTest {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    44
    private static final Class<?> CLASS = VarargsArrayTest.class;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    45
    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
    46
    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
    47
    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
    48
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    49
    public static void main(String[] args) throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    50
        testVarargsArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    51
        testVarargsReferenceArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    52
        testVarargsPrimitiveArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    53
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    54
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    55
    public static void testVarargsArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    56
        final int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    57
        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
    58
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    59
            MethodHandle target = MethodHandleImpl.varargsArray(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    60
            Object[] args = new Object[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    61
            for (int i = 0; i < nargs; i++)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    62
                args[i] = "#"+i;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    63
            Object res = target.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    64
            assertArrayEquals(args, (Object[])res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    65
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    66
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    67
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    68
    public static void testVarargsReferenceArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    69
        testTypedVarargsArray(Object[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    70
        testTypedVarargsArray(String[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    71
        testTypedVarargsArray(Number[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    72
    }
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
    public static void testVarargsPrimitiveArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    75
        testTypedVarargsArray(int[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    76
        testTypedVarargsArray(long[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    77
        testTypedVarargsArray(byte[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    78
        testTypedVarargsArray(boolean[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    79
        testTypedVarargsArray(short[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    80
        testTypedVarargsArray(char[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    81
        testTypedVarargsArray(float[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    82
        testTypedVarargsArray(double[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    83
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    84
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    85
    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
    86
        if (EXHAUSTIVE)  return nargs + 1;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    87
        if (nargs >= MAX)  return Integer.MAX_VALUE;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    88
        int BOT = 20, TOP = MAX-5;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    89
        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
    90
        if (nargs <= BOT || nargs >= TOP) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    91
            ++nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    92
        } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    93
            int bump = Math.max(1, 100 / density);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    94
            nargs += bump;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    95
            if (nargs > TOP)  nargs = TOP;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    96
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    97
        return nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    98
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    99
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   100
    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
   101
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   102
        int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   103
        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
   104
        int density = 3;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   105
        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
   106
        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
   107
        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
   108
            Object[] args = makeTestArray(elemType, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   109
            MethodHandle varargsArray = MethodHandleImpl.varargsArray(arrayType, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   110
            MethodType vaType = varargsArray.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   111
            assertEquals(arrayType, vaType.returnType());
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   112
            if (nargs != 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   113
                assertEquals(elemType, vaType.parameterType(0));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   114
                assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   115
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   116
            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
   117
                         vaType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   118
            Object res = varargsArray.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   119
            assertEquals(res.getClass(), arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   120
            String resString = toArrayString(res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   121
            assertEquals(Arrays.toString(args), resString);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   122
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   123
            MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   124
            MethodType stype = spreader.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   125
            assert(stype == MethodType.methodType(arrayType, arrayType));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   126
            if (nargs <= 5) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   127
                // invoke target as a spreader also:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   128
                @SuppressWarnings("cast")
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   129
                Object res2 = spreader.invokeWithArguments((Object)res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   130
                String res2String = toArrayString(res2);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   131
                assertEquals(Arrays.toString(args), res2String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   132
                // 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
   133
                try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   134
                    Object res3 = spreader.invokeWithArguments((Object)args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   135
                    String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   136
                    assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   137
                    assertEquals(Arrays.toString(args), res3String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   138
                } catch (ClassCastException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   139
                    assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   140
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   141
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   142
            if (nargs == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   143
                // invoke spreader on null arglist
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   144
                Object res3 = spreader.invokeWithArguments((Object)null);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   145
                String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   146
                assertEquals(Arrays.toString(args), res3String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   147
            }
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
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   151
    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
   152
        Wrapper elem = null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   153
        if (elemType.isPrimitive())
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   154
            elem = Wrapper.forPrimitiveType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   155
        else if (Wrapper.isWrapperType(elemType))
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   156
            elem = Wrapper.forWrapperType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   157
        Object[] args = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   158
        for (int i = 0; i < len; i++) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   159
            Object arg = i * 100;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   160
            if (elem == null) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   161
                if (elemType == String.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   162
                    arg = "#"+arg;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   163
                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
   164
            } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   165
                switch (elem) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   166
                    case BOOLEAN: arg = (i % 3 == 0);           break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   167
                    case CHAR:    arg = 'a' + i;                break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   168
                    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
   169
                    case FLOAT:   arg = (float)i / 100;         break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   170
                    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
   171
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   172
                arg = elem.cast(arg, elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   173
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   174
            args[i] = arg;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   175
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   176
        return args;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   177
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   178
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   179
    private static String toArrayString(Object a) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   180
        if (a == null)  return "null";
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   181
        Class<?> elemType = a.getClass().getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   182
        if (elemType == null)  return a.toString();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   183
        if (elemType.isPrimitive()) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   184
            switch (Wrapper.forPrimitiveType(elemType)) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   185
                case INT:      return Arrays.toString((int[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   186
                case BYTE:     return Arrays.toString((byte[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   187
                case BOOLEAN:  return Arrays.toString((boolean[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   188
                case SHORT:    return Arrays.toString((short[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   189
                case CHAR:     return Arrays.toString((char[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   190
                case FLOAT:    return Arrays.toString((float[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   191
                case LONG:     return Arrays.toString((long[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   192
                case DOUBLE:   return Arrays.toString((double[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   193
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   194
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   195
        return Arrays.toString((Object[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   196
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   197
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   198
    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
   199
        if (arr1 == null && arr2 == null)  return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   200
        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
   201
            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
   202
                assertEquals(arr1[i], arr2[i]);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   203
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   204
            return;
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
        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
   207
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   208
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   209
    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
   210
        if (o1 == null && o2 == null)    return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   211
        if (o1 != null && o1.equals(o2)) return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   212
        throw new AssertionError(o1 + " != " + o2);
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
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   215
    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
   216
        if (!b) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   217
            throw new AssertionError(msg);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   218
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   219
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   220
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   221
    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
   222
        assertTrue(msg, !b);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   223
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   224
}