ExceedMaxDim.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 1998, 2013, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4100814 7044282 26 * @summary Make sure you can't create an array of dimension > 256. 27 */ 28 29import java.lang.reflect.Array; 30 31public class ExceedMaxDim { 32 33 public static void main(String[] args) throws Exception { 34 newInstanceOne(); 35 newInstanceMulti(); 36 zeroDimension(); 37 } 38 39 private static void newInstanceOne() throws Exception { 40 Object o = getArrayOf256Dimensions(); 41 try { 42 o = Array.newInstance(o.getClass(), 1); 43 } catch (IllegalArgumentException iae) { 44 System.out.println("success: newInstanceOne test"); 45 return; 46 } 47 throw new Exception("NewArray allowed dimensions > MAXDIM"); 48 } 49 50 private static void newInstanceMulti() throws Exception { 51 Object o = getArrayOf256Dimensions(); 52 try { 53 o = Array.newInstance(o.getClass(), new int[] { 1, 1 }); 54 o = Array.newInstance(o.getClass(), new int[] { 1 }); 55 } catch (IllegalArgumentException iae) { 56 System.out.println("success: newInstanceMulti test"); 57 return; 58 } 59 throw new Exception("MultiNewArray allowed dimensions > MAXDIM"); 60 } 61 62 private static void zeroDimension() throws Exception { 63 try { 64 Array.newInstance(Integer.TYPE, new int[0]); 65 } catch (IllegalArgumentException iae) { 66 System.out.println("success: zeroDimension test"); 67 return; 68 } 69 throw new Exception("MultiNewArray allowed dimension == 0"); 70 } 71 72 private static Object getArrayOf256Dimensions() { 73 /* 1 dimension */ 74 Object o = Array.newInstance(Integer.TYPE, 0); 75 76 /* 254 more dimension. */ 77 for (int i = 1; i <= 254; i++) { 78 o = Array.newInstance(o.getClass(), 1); 79 } 80 81 /* 255 in all. */ 82 return o; 83 } 84} 85