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