test/jdk/java/lang/invoke/VarargsArrayTest.java
author iignatyev
Fri, 14 Sep 2018 14:02:57 -0700
changeset 51754 594919232b8f
parent 47216 71c04702a3d5
child 51769 0ae80830256e
permissions -rw-r--r--
8210732: remove jdk.testlibrary.Utils Reviewed-by: alanb, jcbeyler
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
/*
45283
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
     2
 * Copyright (c) 2014, 2017, 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
45283
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    24
import sun.invoke.util.Wrapper;
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    25
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    26
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    27
import java.lang.invoke.MethodHandle;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    28
import java.lang.invoke.MethodHandleHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    29
import java.lang.invoke.MethodType;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    30
import java.util.Arrays;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    31
import java.util.Collections;
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
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 47216
diff changeset
    37
 * @library /test/lib /java/lang/invoke/common
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
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    40
 * @run main/bootclasspath/othervm -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;
45283
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    49
    private static final int MAX_ARITY = Integer.getInteger(
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    50
            CLASS.getSimpleName()+".MAX_ARITY", 40);
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    51
    private static final int START_ARITY = Integer.getInteger(
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    52
            CLASS.getSimpleName()+".START_ARITY", 0);
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    53
    private static final boolean EXHAUSTIVE = Boolean.getBoolean(
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
    54
            CLASS.getSimpleName()+".EXHAUSTIVE");
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    55
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    56
    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
    57
        CodeCacheOverflowProcessor.runMHTest(VarargsArrayTest::test);
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    58
    }
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    59
6ad68e35506d 8067005: Several java/lang/invoke tests fail due to exhausted code cache
kshefov
parents: 26466
diff changeset
    60
    public static void test() throws Throwable {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    61
        testVarargsArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    62
        testVarargsReferenceArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    63
        testVarargsPrimitiveArray();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    64
    }
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
    public static void testVarargsArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    67
        final int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    68
        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
    69
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
    70
            MethodHandle target = MethodHandleHelper.varargsArray(nargs);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    71
            Object[] args = new Object[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    72
            for (int i = 0; i < nargs; i++)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    73
                args[i] = "#"+i;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    74
            Object res = target.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    75
            assertArrayEquals(args, (Object[])res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    76
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    77
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    78
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    79
    public static void testVarargsReferenceArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    80
        testTypedVarargsArray(Object[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    81
        testTypedVarargsArray(String[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    82
        testTypedVarargsArray(Number[].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
    public static void testVarargsPrimitiveArray() throws Throwable {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    86
        testTypedVarargsArray(int[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    87
        testTypedVarargsArray(long[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    88
        testTypedVarargsArray(byte[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    89
        testTypedVarargsArray(boolean[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    90
        testTypedVarargsArray(short[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    91
        testTypedVarargsArray(char[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    92
        testTypedVarargsArray(float[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    93
        testTypedVarargsArray(double[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    94
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    95
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    96
    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
    97
        if (EXHAUSTIVE)  return nargs + 1;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    98
        if (nargs >= MAX)  return Integer.MAX_VALUE;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
    99
        int BOT = 20, TOP = MAX-5;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   100
        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
   101
        if (nargs <= BOT || nargs >= TOP) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   102
            ++nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   103
        } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   104
            int bump = Math.max(1, 100 / density);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   105
            nargs += bump;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   106
            if (nargs > TOP)  nargs = TOP;
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
        return nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   109
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   110
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   111
    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
   112
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   113
        int MIN = START_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   114
        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
   115
        int density = 3;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   116
        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
   117
        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
   118
        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
   119
            Object[] args = makeTestArray(elemType, nargs);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31178
diff changeset
   120
            MethodHandle varargsArray = MethodHandleHelper.varargsArray(arrayType, nargs);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   121
            MethodType vaType = varargsArray.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   122
            assertEquals(arrayType, vaType.returnType());
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   123
            if (nargs != 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   124
                assertEquals(elemType, vaType.parameterType(0));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   125
                assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   126
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   127
            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
   128
                         vaType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   129
            Object res = varargsArray.invokeWithArguments(args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   130
            assertEquals(res.getClass(), arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   131
            String resString = toArrayString(res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   132
            assertEquals(Arrays.toString(args), resString);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   133
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   134
            MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   135
            MethodType stype = spreader.type();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   136
            assert(stype == MethodType.methodType(arrayType, arrayType));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   137
            if (nargs <= 5) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   138
                // invoke target as a spreader also:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   139
                @SuppressWarnings("cast")
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   140
                Object res2 = spreader.invokeWithArguments((Object)res);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   141
                String res2String = toArrayString(res2);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   142
                assertEquals(Arrays.toString(args), res2String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   143
                // 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
   144
                try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   145
                    Object res3 = spreader.invokeWithArguments((Object)args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   146
                    String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   147
                    assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   148
                    assertEquals(Arrays.toString(args), res3String);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   149
                } catch (ClassCastException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   150
                    assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   151
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   152
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   153
            if (nargs == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   154
                // invoke spreader on null arglist
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   155
                Object res3 = spreader.invokeWithArguments((Object)null);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   156
                String res3String = toArrayString(res3);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   157
                assertEquals(Arrays.toString(args), res3String);
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
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   160
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   161
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   162
    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
   163
        Wrapper elem = null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   164
        if (elemType.isPrimitive())
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   165
            elem = Wrapper.forPrimitiveType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   166
        else if (Wrapper.isWrapperType(elemType))
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   167
            elem = Wrapper.forWrapperType(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   168
        Object[] args = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   169
        for (int i = 0; i < len; i++) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   170
            Object arg = i * 100;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   171
            if (elem == null) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   172
                if (elemType == String.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   173
                    arg = "#"+arg;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   174
                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
   175
            } else {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   176
                switch (elem) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   177
                    case BOOLEAN: arg = (i % 3 == 0);           break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   178
                    case CHAR:    arg = 'a' + i;                break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   179
                    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
   180
                    case FLOAT:   arg = (float)i / 100;         break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   181
                    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
   182
                }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   183
                arg = elem.cast(arg, elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   184
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   185
            args[i] = arg;
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
        return args;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   188
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   189
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   190
    private static String toArrayString(Object a) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   191
        if (a == null)  return "null";
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   192
        Class<?> elemType = a.getClass().getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   193
        if (elemType == null)  return a.toString();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   194
        if (elemType.isPrimitive()) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   195
            switch (Wrapper.forPrimitiveType(elemType)) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   196
                case INT:      return Arrays.toString((int[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   197
                case BYTE:     return Arrays.toString((byte[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   198
                case BOOLEAN:  return Arrays.toString((boolean[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   199
                case SHORT:    return Arrays.toString((short[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   200
                case CHAR:     return Arrays.toString((char[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   201
                case FLOAT:    return Arrays.toString((float[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   202
                case LONG:     return Arrays.toString((long[])a);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   203
                case DOUBLE:   return Arrays.toString((double[])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
        return Arrays.toString((Object[])a);
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 assertArrayEquals(Object[] arr1, Object[] arr2) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   210
        if (arr1 == null && arr2 == null)  return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   211
        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
   212
            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
   213
                assertEquals(arr1[i], arr2[i]);
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
            return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   216
        }
45283
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
   217
        throw new AssertionError(Arrays.deepToString(arr1)
fcb2af038805 8180890: move c.o.testlibrary.jsr292 classes to jdk/test/java/lang/invoke directory
iignatyev
parents: 37779
diff changeset
   218
                + " != " + Arrays.deepToString(arr2));
26466
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 assertEquals(Object o1, Object o2) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   222
        if (o1 == null && o2 == null)    return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   223
        if (o1 != null && o1.equals(o2)) return;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   224
        throw new AssertionError(o1 + " != " + o2);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   225
    }
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
    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
   228
        if (!b) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   229
            throw new AssertionError(msg);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   230
        }
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
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   233
    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
   234
        assertTrue(msg, !b);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   235
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents:
diff changeset
   236
}