test/jdk/java/lang/invoke/ArrayConstructorTest.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:
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     1
/*
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     4
 *
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    10
 *
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    15
 * accompanied this code).
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    16
 *
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    20
 *
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    23
 * questions.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    24
 */
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    25
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    26
/* @test
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    27
 * @bug 8155106
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    28
 * @run testng/othervm -ea -esa test.java.lang.invoke.ArrayConstructorTest
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    29
 */
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    30
package test.java.lang.invoke;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    31
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    34
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    35
import static java.lang.invoke.MethodType.methodType;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    36
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    37
import static org.testng.AssertJUnit.*;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    38
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    39
import org.testng.annotations.*;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    40
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    41
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    42
public class ArrayConstructorTest {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    43
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    44
    static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    45
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    46
    @Test
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    47
    public static void testFindConstructorArray() {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    48
        boolean caught = false;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    49
        try {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    50
            MethodHandle h = LOOKUP.findConstructor(Object[].class, methodType(void.class));
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    51
        } catch (NoSuchMethodException nsme) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    52
            assertEquals("no constructor for array class: [Ljava.lang.Object;", nsme.getMessage());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    53
            caught = true;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    54
        } catch (Exception e) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    55
            throw new AssertionError("unexpected exception: " + e);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    56
        }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    57
        assertTrue(caught);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    58
    }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    59
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    60
    @DataProvider
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    61
    static Object[][] arrayConstructorNegative() {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    62
        return new Object[][]{
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    63
                {String.class, IllegalArgumentException.class, "not an array class: java.lang.String"},
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    64
                {null, NullPointerException.class, null}
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    65
        };
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    66
    }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    67
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    68
    @Test(dataProvider = "arrayConstructorNegative")
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    69
    public static void testArrayConstructorNegative(Class<?> clazz, Class<?> exceptionClass, String message) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    70
        boolean caught = false;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    71
        try {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    72
            MethodHandle h = MethodHandles.arrayConstructor(clazz);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    73
        } catch (Exception e) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    74
            assertEquals(exceptionClass, e.getClass());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    75
            if (message != null) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    76
                assertEquals(message, e.getMessage());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    77
            }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    78
            caught = true;
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    79
        }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    80
        assertTrue(caught);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    81
    }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    82
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    83
    @Test
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    84
    public static void testArrayConstructor() throws Throwable {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    85
        MethodHandle h = MethodHandles.arrayConstructor(String[].class);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    86
        assertEquals(methodType(String[].class, int.class), h.type());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    87
        String[] a = (String[]) h.invoke(17);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    88
        assertEquals(17, a.length);
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    89
    }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    90
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    91
    @Test(expectedExceptions = {NegativeArraySizeException.class})
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    92
    public static void testArrayConstructorNegativeIndex() throws Throwable {
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    93
        MethodHandle h = MethodHandles.arrayConstructor(String[].class);
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    94
        assertEquals(methodType(String[].class, int.class), h.type());
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    95
        h.invoke(-1); // throws exception
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    96
    }
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
    97
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents:
diff changeset
    98
}