Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Class/forName/arrayClass/ExceedMaxDim.java
41159 views
1
/*
2
* Copyright (c) 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 7044282
26
* @build Class1 Class2 Class3 Class4
27
* @run main ExceedMaxDim
28
* @summary Make sure you can't get an array class of dimension > 255.
29
*/
30
31
// Class1, Class2, Class3 and Class4 should not have been loaded prior to the
32
// calls to forName
33
34
public class ExceedMaxDim {
35
//0123456789012345678901234567890123456789
36
private String brackets = "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
37
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
38
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
39
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
40
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
41
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
42
"[[[[[[[[[[[[[[";
43
private String name254 = brackets + "Ljava.lang.String;";
44
private String name255 = "[" + name254;
45
private String name256 = "[" + name255;
46
private String name1 = "[Ljava.lang.String;";
47
private String bigName;
48
private int error = 0;
49
50
private static final ClassLoader IMPLICIT_LOADER = null;
51
52
public ExceedMaxDim() {
53
super();
54
55
StringBuilder sb = new StringBuilder(Short.MAX_VALUE + 50);
56
for (int i = 0; i < Short.MAX_VALUE + 20; i++)
57
sb.append('[');
58
sb.append("Ljava.lang.String;");
59
bigName = sb.toString();
60
61
if (name256.lastIndexOf('[') != 255) // 256:th [
62
throw new RuntimeException("Test broken");
63
}
64
65
public static void main(String[] args) throws Exception {
66
ExceedMaxDim test = new ExceedMaxDim();
67
test.testImplicitLoader();
68
test.testOtherLoader();
69
70
if (test.error != 0)
71
throw new RuntimeException("Test failed, was able to create array with dim > 255." +
72
" See log for details.");
73
}
74
75
private void testImplicitLoader() throws Exception {
76
// These four should succeed
77
assertSucceedForName(name1, IMPLICIT_LOADER);
78
assertSucceedForName(name254, IMPLICIT_LOADER);
79
assertSucceedForName(name255, IMPLICIT_LOADER);
80
assertSucceedForName(brackets + "[LClass1;", IMPLICIT_LOADER);
81
82
// The following three should fail
83
assertFailForName(name256, IMPLICIT_LOADER);
84
assertFailForName(bigName, IMPLICIT_LOADER);
85
assertFailForName(brackets + "[[LClass2;", IMPLICIT_LOADER);
86
}
87
88
private void testOtherLoader() throws Exception {
89
ClassLoader cl = ExceedMaxDim.class.getClassLoader();
90
91
// These four should succeed
92
assertSucceedForName(name1, cl);
93
assertSucceedForName(name254,cl);
94
assertSucceedForName(name255, cl);
95
assertSucceedForName(brackets + "[LClass3;", cl);
96
97
// The following three should fail
98
assertFailForName(name256, cl);
99
assertFailForName(bigName, cl);
100
assertFailForName(brackets + "[[Class4;", cl);
101
}
102
103
private void assertFailForName(String name, ClassLoader cl) {
104
Class<?> c;
105
try {
106
if (cl == null)
107
c = Class.forName(name);
108
else
109
c = Class.forName(name, true, cl);
110
error++;
111
System.err.println("ERROR: could create " + c);
112
} catch (ClassNotFoundException e) {
113
;// ok
114
}
115
}
116
117
private void assertSucceedForName(String name, ClassLoader cl) {
118
Class<?> c;
119
try {
120
if (cl == null)
121
c = Class.forName(name);
122
else
123
c = Class.forName(name, true, cl);
124
} catch (ClassNotFoundException e) {
125
error++;
126
System.err.println("ERROR: could not create " + name);
127
}
128
}
129
}
130
131