Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/CallSiteTest.java
41149 views
1
/*
2
* Copyright (c) 2011, 2012, 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
/**
25
* @test
26
* @summary smoke tests for CallSite
27
* @library /test/lib
28
*
29
* @build indify.Indify
30
* @compile CallSiteTest.java
31
* @run main/othervm/timeout=3600 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -Xbatch
32
* indify.Indify
33
* --expand-properties --classpath ${test.classes}
34
* --java test.java.lang.invoke.CallSiteTest
35
*/
36
37
package test.java.lang.invoke;
38
39
import java.io.*;
40
41
import java.lang.invoke.*;
42
import static java.lang.invoke.MethodHandles.*;
43
import static java.lang.invoke.MethodType.*;
44
import static jdk.test.lib.Asserts.*;
45
46
public class CallSiteTest {
47
private static final Class<?> CLASS = CallSiteTest.class;
48
49
private static CallSite mcs;
50
private static CallSite vcs;
51
private static MethodHandle mh_foo;
52
private static MethodHandle mh_bar;
53
54
static {
55
try {
56
57
mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class));
58
mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class));
59
mcs = new MutableCallSite(mh_foo);
60
vcs = new VolatileCallSite(mh_foo);
61
} catch (Exception e) {
62
e.printStackTrace();
63
throw new Error(e);
64
}
65
}
66
67
public static void main(String... av) throws Throwable {
68
testConstantCallSite();
69
testMutableCallSite();
70
testVolatileCallSite();
71
}
72
73
private static final int N = Integer.MAX_VALUE / 100;
74
private static final int RESULT1 = 762786192;
75
private static final int RESULT2 = -21474836;
76
77
static final CallSite MCS = new MutableCallSite(methodType(void.class));
78
static final MethodHandle MCS_INVOKER = MCS.dynamicInvoker();
79
80
static void test(boolean shouldThrow) {
81
try {
82
MCS_INVOKER.invokeExact();
83
if (shouldThrow) {
84
throw new AssertionError("should throw");
85
}
86
} catch (IllegalStateException ise) {
87
if (!shouldThrow) {
88
throw new AssertionError("should not throw", ise);
89
}
90
} catch (Throwable e) {
91
throw new Error(e);
92
}
93
}
94
95
static class MyCCS extends ConstantCallSite {
96
public MyCCS(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
97
super(targetType, createTargetHook);
98
}
99
}
100
101
private static MethodHandle testConstantCallSiteHandler(CallSite cs, CallSite[] holder) throws Throwable {
102
holder[0] = cs; // capture call site instance for subsequent checks
103
104
MethodType csType = cs.type(); // should not throw on partially constructed instance
105
106
// Truly dynamic invoker for constant call site
107
MethodHandle getTarget = lookup().findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class))
108
.bindTo(cs);
109
MethodHandle invoker = MethodHandles.exactInvoker(csType);
110
MethodHandle target = MethodHandles.foldArguments(invoker, getTarget);
111
112
MCS.setTarget(target);
113
// warmup
114
for (int i = 0; i < 20_000; i++) {
115
test(true); // should throw IllegalStateException
116
}
117
118
return MethodHandles.empty(csType); // initialize cs with an empty method handle
119
}
120
121
private static void testConstantCallSite() throws Throwable {
122
CallSite[] holder = new CallSite[1];
123
MethodHandle handler = lookup().findStatic(CLASS, "testConstantCallSiteHandler", MethodType.methodType(MethodHandle.class, CallSite.class, CallSite[].class));
124
handler = MethodHandles.insertArguments(handler, 1, new Object[] { holder } );
125
126
CallSite ccs = new MyCCS(MCS.type(), handler); // trigger call to handler
127
128
if (ccs != holder[0]) {
129
throw new AssertionError("different call site instances");
130
}
131
for (int i = 0; i < 20_000; i++) {
132
test(false); // should not throw
133
}
134
}
135
136
private static void testMutableCallSite() throws Throwable {
137
// warm-up
138
for (int i = 0; i < 20000; i++) {
139
mcs.setTarget(mh_foo);
140
}
141
// run
142
for (int n = 0; n < 2; n++) {
143
mcs.setTarget(mh_foo);
144
for (int i = 0; i < 5; i++) {
145
assertEQ(RESULT1, runMutableCallSite());
146
}
147
mcs.setTarget(mh_bar);
148
for (int i = 0; i < 5; i++) {
149
assertEQ(RESULT2, runMutableCallSite());
150
}
151
}
152
}
153
private static void testVolatileCallSite() throws Throwable {
154
// warm-up
155
for (int i = 0; i < 20000; i++) {
156
vcs.setTarget(mh_foo);
157
}
158
// run
159
for (int n = 0; n < 2; n++) {
160
vcs.setTarget(mh_foo);
161
for (int i = 0; i < 5; i++) {
162
assertEQ(RESULT1, runVolatileCallSite());
163
}
164
vcs.setTarget(mh_bar);
165
for (int i = 0; i < 5; i++) {
166
assertEQ(RESULT2, runVolatileCallSite());
167
}
168
}
169
}
170
171
private static int runMutableCallSite() throws Throwable {
172
int sum = 0;
173
for (int i = 0; i < N; i++) {
174
sum += (int) INDY_mcs().invokeExact(i, i+1);
175
}
176
return sum;
177
}
178
private static int runVolatileCallSite() throws Throwable {
179
int sum = 0;
180
for (int i = 0; i < N; i++) {
181
sum += (int) INDY_vcs().invokeExact(i, i+1);
182
}
183
return sum;
184
}
185
186
static int foo(int a, int b) { return a + b; }
187
static int bar(int a, int b) { return a - b; }
188
189
private static MethodType MT_bsm() {
190
shouldNotCallThis();
191
return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
192
}
193
194
private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
195
return mcs;
196
}
197
private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException {
198
shouldNotCallThis();
199
return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm());
200
}
201
private static MethodHandle INDY_mcs() throws Throwable {
202
shouldNotCallThis();
203
return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
204
}
205
206
private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
207
return vcs;
208
}
209
private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException {
210
shouldNotCallThis();
211
return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm());
212
}
213
private static MethodHandle INDY_vcs() throws Throwable {
214
shouldNotCallThis();
215
return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
216
}
217
218
private static void shouldNotCallThis() {
219
// if this gets called, the transformation has not taken place
220
throw new AssertionError("this code should be statically transformed away by Indify");
221
}
222
}
223
224