Path: blob/master/test/jdk/java/lang/Class/forName/arrayClass/ExceedMaxDim.java
41159 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 704428225* @build Class1 Class2 Class3 Class426* @run main ExceedMaxDim27* @summary Make sure you can't get an array class of dimension > 255.28*/2930// Class1, Class2, Class3 and Class4 should not have been loaded prior to the31// calls to forName3233public class ExceedMaxDim {34//012345678901234567890123456789012345678935private String brackets = "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +36"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +37"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +38"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +39"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +40"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +41"[[[[[[[[[[[[[[";42private String name254 = brackets + "Ljava.lang.String;";43private String name255 = "[" + name254;44private String name256 = "[" + name255;45private String name1 = "[Ljava.lang.String;";46private String bigName;47private int error = 0;4849private static final ClassLoader IMPLICIT_LOADER = null;5051public ExceedMaxDim() {52super();5354StringBuilder sb = new StringBuilder(Short.MAX_VALUE + 50);55for (int i = 0; i < Short.MAX_VALUE + 20; i++)56sb.append('[');57sb.append("Ljava.lang.String;");58bigName = sb.toString();5960if (name256.lastIndexOf('[') != 255) // 256:th [61throw new RuntimeException("Test broken");62}6364public static void main(String[] args) throws Exception {65ExceedMaxDim test = new ExceedMaxDim();66test.testImplicitLoader();67test.testOtherLoader();6869if (test.error != 0)70throw new RuntimeException("Test failed, was able to create array with dim > 255." +71" See log for details.");72}7374private void testImplicitLoader() throws Exception {75// These four should succeed76assertSucceedForName(name1, IMPLICIT_LOADER);77assertSucceedForName(name254, IMPLICIT_LOADER);78assertSucceedForName(name255, IMPLICIT_LOADER);79assertSucceedForName(brackets + "[LClass1;", IMPLICIT_LOADER);8081// The following three should fail82assertFailForName(name256, IMPLICIT_LOADER);83assertFailForName(bigName, IMPLICIT_LOADER);84assertFailForName(brackets + "[[LClass2;", IMPLICIT_LOADER);85}8687private void testOtherLoader() throws Exception {88ClassLoader cl = ExceedMaxDim.class.getClassLoader();8990// These four should succeed91assertSucceedForName(name1, cl);92assertSucceedForName(name254,cl);93assertSucceedForName(name255, cl);94assertSucceedForName(brackets + "[LClass3;", cl);9596// The following three should fail97assertFailForName(name256, cl);98assertFailForName(bigName, cl);99assertFailForName(brackets + "[[Class4;", cl);100}101102private void assertFailForName(String name, ClassLoader cl) {103Class<?> c;104try {105if (cl == null)106c = Class.forName(name);107else108c = Class.forName(name, true, cl);109error++;110System.err.println("ERROR: could create " + c);111} catch (ClassNotFoundException e) {112;// ok113}114}115116private void assertSucceedForName(String name, ClassLoader cl) {117Class<?> c;118try {119if (cl == null)120c = Class.forName(name);121else122c = Class.forName(name, true, cl);123} catch (ClassNotFoundException e) {124error++;125System.err.println("ERROR: could not create " + name);126}127}128}129130131