Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/VarargsArrayTest.java
47209 views
1
/*
2
* Copyright (c) 2014, 2015, 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
package java.lang.invoke;
25
26
import sun.invoke.util.Wrapper;
27
import java.util.Arrays;
28
import java.util.Collections;
29
import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;
30
31
/* @test
32
* @summary unit tests for varargs array methods: MethodHandleInfo.varargsArray(int),
33
* MethodHandleInfo.varargsArray(Class,int) & MethodHandleInfo.varargsList(int)
34
* @library /lib/testlibrary /lib/testlibrary/jsr292
35
* @run main/othervm/bootclasspath java.lang.invoke.VarargsArrayTest
36
* @run main/othervm/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.START_ARITY=250 java.lang.invoke.VarargsArrayTest
37
*/
38
39
/* This might take a while and burn lots of metadata:
40
* @run main/othervm/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.EXHAUSTIVE=true java.lang.invoke.VarargsArrayTest
41
*/
42
public class VarargsArrayTest {
43
private static final Class<?> CLASS = VarargsArrayTest.class;
44
private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40);
45
private static final int START_ARITY = Integer.getInteger(CLASS.getSimpleName()+".START_ARITY", 0);
46
private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE");
47
48
public static void main(String[] args) throws Throwable {
49
CodeCacheOverflowProcessor.runMHTest(VarargsArrayTest::test);
50
}
51
52
public static void test() throws Throwable {
53
testVarargsArray();
54
testVarargsReferenceArray();
55
testVarargsPrimitiveArray();
56
}
57
58
public static void testVarargsArray() throws Throwable {
59
final int MIN = START_ARITY;
60
final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added
61
for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
62
MethodHandle target = MethodHandleImpl.varargsArray(nargs);
63
Object[] args = new Object[nargs];
64
for (int i = 0; i < nargs; i++)
65
args[i] = "#"+i;
66
Object res = target.invokeWithArguments(args);
67
assertArrayEquals(args, (Object[])res);
68
}
69
}
70
71
public static void testVarargsReferenceArray() throws Throwable {
72
testTypedVarargsArray(Object[].class);
73
testTypedVarargsArray(String[].class);
74
testTypedVarargsArray(Number[].class);
75
}
76
77
public static void testVarargsPrimitiveArray() throws Throwable {
78
testTypedVarargsArray(int[].class);
79
testTypedVarargsArray(long[].class);
80
testTypedVarargsArray(byte[].class);
81
testTypedVarargsArray(boolean[].class);
82
testTypedVarargsArray(short[].class);
83
testTypedVarargsArray(char[].class);
84
testTypedVarargsArray(float[].class);
85
testTypedVarargsArray(double[].class);
86
}
87
88
private static int nextArgCount(int nargs, int density, int MAX) {
89
if (EXHAUSTIVE) return nargs + 1;
90
if (nargs >= MAX) return Integer.MAX_VALUE;
91
int BOT = 20, TOP = MAX-5;
92
if (density < 10) { BOT = 10; MAX = TOP-2; }
93
if (nargs <= BOT || nargs >= TOP) {
94
++nargs;
95
} else {
96
int bump = Math.max(1, 100 / density);
97
nargs += bump;
98
if (nargs > TOP) nargs = TOP;
99
}
100
return nargs;
101
}
102
103
private static void testTypedVarargsArray(Class<?> arrayType) throws Throwable {
104
Class<?> elemType = arrayType.getComponentType();
105
int MIN = START_ARITY;
106
int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added
107
int density = 3;
108
if (elemType == int.class || elemType == long.class) density = 7;
109
if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; }
110
for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) {
111
Object[] args = makeTestArray(elemType, nargs);
112
MethodHandle varargsArray = MethodHandleImpl.varargsArray(arrayType, nargs);
113
MethodType vaType = varargsArray.type();
114
assertEquals(arrayType, vaType.returnType());
115
if (nargs != 0) {
116
assertEquals(elemType, vaType.parameterType(0));
117
assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
118
}
119
assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)),
120
vaType);
121
Object res = varargsArray.invokeWithArguments(args);
122
assertEquals(res.getClass(), arrayType);
123
String resString = toArrayString(res);
124
assertEquals(Arrays.toString(args), resString);
125
126
MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
127
MethodType stype = spreader.type();
128
assert(stype == MethodType.methodType(arrayType, arrayType));
129
if (nargs <= 5) {
130
// invoke target as a spreader also:
131
@SuppressWarnings("cast")
132
Object res2 = spreader.invokeWithArguments((Object)res);
133
String res2String = toArrayString(res2);
134
assertEquals(Arrays.toString(args), res2String);
135
// invoke the spreader on a generic Object[] array; check for error
136
try {
137
Object res3 = spreader.invokeWithArguments((Object)args);
138
String res3String = toArrayString(res3);
139
assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
140
assertEquals(Arrays.toString(args), res3String);
141
} catch (ClassCastException ex) {
142
assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
143
}
144
}
145
if (nargs == 0) {
146
// invoke spreader on null arglist
147
Object res3 = spreader.invokeWithArguments((Object)null);
148
String res3String = toArrayString(res3);
149
assertEquals(Arrays.toString(args), res3String);
150
}
151
}
152
}
153
154
private static Object[] makeTestArray(Class<?> elemType, int len) {
155
Wrapper elem = null;
156
if (elemType.isPrimitive())
157
elem = Wrapper.forPrimitiveType(elemType);
158
else if (Wrapper.isWrapperType(elemType))
159
elem = Wrapper.forWrapperType(elemType);
160
Object[] args = new Object[len];
161
for (int i = 0; i < len; i++) {
162
Object arg = i * 100;
163
if (elem == null) {
164
if (elemType == String.class)
165
arg = "#"+arg;
166
arg = elemType.cast(arg); // just to make sure
167
} else {
168
switch (elem) {
169
case BOOLEAN: arg = (i % 3 == 0); break;
170
case CHAR: arg = 'a' + i; break;
171
case LONG: arg = (long)i * 1000_000_000; break;
172
case FLOAT: arg = (float)i / 100; break;
173
case DOUBLE: arg = (double)i / 1000_000; break;
174
}
175
arg = elem.cast(arg, elemType);
176
}
177
args[i] = arg;
178
}
179
return args;
180
}
181
182
private static String toArrayString(Object a) {
183
if (a == null) return "null";
184
Class<?> elemType = a.getClass().getComponentType();
185
if (elemType == null) return a.toString();
186
if (elemType.isPrimitive()) {
187
switch (Wrapper.forPrimitiveType(elemType)) {
188
case INT: return Arrays.toString((int[])a);
189
case BYTE: return Arrays.toString((byte[])a);
190
case BOOLEAN: return Arrays.toString((boolean[])a);
191
case SHORT: return Arrays.toString((short[])a);
192
case CHAR: return Arrays.toString((char[])a);
193
case FLOAT: return Arrays.toString((float[])a);
194
case LONG: return Arrays.toString((long[])a);
195
case DOUBLE: return Arrays.toString((double[])a);
196
}
197
}
198
return Arrays.toString((Object[])a);
199
}
200
201
public static void assertArrayEquals(Object[] arr1, Object[] arr2) {
202
if (arr1 == null && arr2 == null) return;
203
if (arr1 != null && arr2 != null && arr1.length == arr2.length) {
204
for (int i = 0; i < arr1.length; i++) {
205
assertEquals(arr1[i], arr2[i]);
206
}
207
return;
208
}
209
throw new AssertionError(Arrays.deepToString(arr1) + " != " + Arrays.deepToString(arr2));
210
}
211
212
public static void assertEquals(Object o1, Object o2) {
213
if (o1 == null && o2 == null) return;
214
if (o1 != null && o1.equals(o2)) return;
215
throw new AssertionError(o1 + " != " + o2);
216
}
217
218
public static void assertTrue(String msg, boolean b) {
219
if (!b) {
220
throw new AssertionError(msg);
221
}
222
}
223
224
public static void assertFalse(String msg, boolean b) {
225
assertTrue(msg, !b);
226
}
227
}
228
229