test/jdk/java/lang/invoke/ArrayLengthTest.java
author mchung
Fri, 17 Nov 2017 08:12:02 -0600
changeset 47856 76519338df34
parent 47216 71c04702a3d5
permissions -rw-r--r--
8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior Reviewed-by: psandoz, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     1
/*
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     4
 *
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    10
 *
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    15
 * accompanied this code).
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    16
 *
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    20
 *
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    23
 * questions.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    24
 */
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    25
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    26
/* @test
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    27
 * @run testng/othervm -ea -esa test.java.lang.invoke.ArrayLengthTest
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    28
 */
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    29
package test.java.lang.invoke;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    30
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    31
import java.lang.invoke.MethodHandle;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    32
import java.lang.invoke.MethodHandles;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    33
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    34
import static org.testng.AssertJUnit.*;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    35
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    36
import org.testng.annotations.*;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    37
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    38
public class ArrayLengthTest {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    39
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    40
    @DataProvider
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    41
    Object[][] arrayClasses() {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    42
        return new Object[][] {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    43
                {int[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    44
                {long[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    45
                {float[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    46
                {double[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    47
                {boolean[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    48
                {byte[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    49
                {short[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    50
                {char[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    51
                {Object[].class},
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    52
                {StringBuffer[].class}
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    53
        };
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    54
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    55
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    56
    @Test(dataProvider = "arrayClasses")
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    57
    public void testArrayLength(Class<?> arrayClass) throws Throwable {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    58
        MethodHandle arrayLength = MethodHandles.arrayLength(arrayClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    59
        assertEquals(int.class, arrayLength.type().returnType());
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    60
        assertEquals(arrayClass, arrayLength.type().parameterType(0));
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    61
        Object array = MethodHandles.arrayConstructor(arrayClass).invoke(10);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    62
        assertEquals(10, arrayLength.invoke(array));
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    63
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    64
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    65
    @Test(dataProvider = "arrayClasses", expectedExceptions = NullPointerException.class)
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    66
    public void testArrayLengthInvokeNPE(Class<?> arrayClass) throws Throwable {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    67
        MethodHandle arrayLength = MethodHandles.arrayLength(arrayClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    68
        arrayLength.invoke(null);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    69
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    70
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    71
    @Test(expectedExceptions = IllegalArgumentException.class)
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    72
    public void testArrayLengthNoArray() {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    73
        MethodHandles.arrayLength(String.class);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    74
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    75
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    76
    @Test(expectedExceptions = NullPointerException.class)
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    77
    public void testArrayLengthNPE() {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    78
        MethodHandles.arrayLength(null);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    79
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    80
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    81
    @Test(expectedExceptions = NullPointerException.class)
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    82
    public void testNullReference() throws Throwable {
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    83
        MethodHandle arrayLength = MethodHandles.arrayLength(String[].class);
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    84
        int len = (int)arrayLength.invokeExact((String[])null);
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    85
    }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents:
diff changeset
    86
}