Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/ConstantCallSiteTest.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9.jsr292;
23
24
import org.testng.annotations.Test;
25
import org.testng.AssertJUnit;
26
import java.lang.invoke.CallSite;
27
import java.lang.invoke.ConstantCallSite;
28
import java.lang.invoke.MethodHandle;
29
import java.lang.invoke.MethodHandles;
30
import java.lang.invoke.MethodType;
31
import java.lang.invoke.MutableCallSite;
32
import java.lang.invoke.WrongMethodTypeException;
33
import examples.PackageExamples;
34
35
/**
36
* @author mesbah
37
* This class contains tests for ConstantCallSite API
38
*/
39
public class ConstantCallSiteTest {
40
41
/**
42
* A basic sanity test for ConstantCallSite
43
* @throws Throwable
44
*/
45
@Test(groups = { "level.extended" })
46
public void testBasic_ConstantCallSite() throws Throwable {
47
MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "addPublicStatic", MethodType.methodType(int.class,int.class,int.class));
48
ConstantCallSite ccs = new ConstantCallSite(mh);
49
int result = (int)ccs.dynamicInvoker().invokeExact(1,2);
50
51
AssertJUnit.assertEquals(3,result);
52
53
AssertJUnit.assertEquals(mh,ccs.getTarget());
54
55
AssertJUnit.assertEquals(mh.type(),ccs.type());
56
}
57
58
/**
59
* A basic negative test for ConstantCallSite
60
* @throws Throwable
61
*/
62
@Test(groups = { "level.extended" })
63
public void testBasicNegative_ConstantCallSite() throws Throwable {
64
MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "addPublicStatic", MethodType.methodType(int.class,int.class,int.class));
65
ConstantCallSite ccs = new ConstantCallSite(mh);
66
boolean wmtHit = false;
67
try {
68
int result = (int)ccs.dynamicInvoker().invokeExact(1,2,3);
69
AssertJUnit.assertEquals(3,result);
70
}
71
catch(WrongMethodTypeException e) {
72
wmtHit = true;
73
}
74
75
AssertJUnit.assertTrue(wmtHit);
76
}
77
78
/**
79
* Negative test - attempts to set a target to a ConstantCallSite instance
80
* @throws Throwable
81
*/
82
@Test(groups = { "level.extended" })
83
public void testConstantCallSiteSetTarget() throws Throwable {
84
MethodHandle mh1 = MethodHandles.lookup().findStatic(SamePackageExample.class, "addPublicStatic", MethodType.methodType(int.class,int.class,int.class));
85
MethodHandle mh2 = MethodHandles.lookup().findStatic(PackageExamples.class, "addPublicStatic", MethodType.methodType(int.class,int.class,int.class));
86
ConstantCallSite ccs = new ConstantCallSite(mh1);
87
boolean uoHit = false;
88
89
try {
90
ccs.setTarget(null);
91
}
92
catch(UnsupportedOperationException e) {
93
uoHit = true;
94
}
95
96
AssertJUnit.assertTrue(uoHit);
97
98
uoHit = false;
99
100
try {
101
ccs.setTarget(mh2);
102
}
103
catch(UnsupportedOperationException e) {
104
uoHit = true;
105
}
106
107
AssertJUnit.assertTrue(uoHit);
108
109
uoHit = false;
110
111
try {
112
ccs.setTarget(mh1);
113
}
114
catch(UnsupportedOperationException e) {
115
uoHit = true;
116
}
117
AssertJUnit.assertTrue(uoHit);
118
}
119
120
/**
121
* Tests protected ConstantCallSite(MethodType targetType,MethodHandle createTargetHook) throws Throwable
122
* A subclass of ConstantCallSite is used to invoke the protected constructor
123
* @throws Throwable
124
*/
125
@Test(groups = { "level.extended" })
126
public void testTargetHookConstructor() throws Throwable {
127
128
MethodType targetType = MethodType.methodType(int.class);
129
MethodHandle hook = MethodHandles.lookup().findStatic(ConstantCallSiteTest.class, "createTargetHook", MethodType.methodType(MethodHandle.class,ConstantCallSite.class));
130
131
MyConstantCallSite myCcs = new MyConstantCallSite(targetType,hook);
132
133
int returnVal = (int)myCcs.dynamicInvoker().invokeExact();
134
135
AssertJUnit.assertEquals(1,returnVal);
136
}
137
138
/**
139
* Negative test for protected ConstantCallSite(MethodType targetType,MethodHandle createTargetHook).
140
* There is a mismatch between the MT passed in and the MH.type() returned by the hook.
141
* A subclass of ConstantCallSite is used to invoke the protected constructor
142
* @throws Throwable
143
*/
144
@Test(groups = { "level.extended" })
145
public void testTypeMismatch_TargetHookConstructor() throws Throwable {
146
147
MethodType targetType = MethodType.methodType(String.class);
148
MethodHandle hook = MethodHandles.lookup().findStatic(ConstantCallSiteTest.class, "createTargetHook", MethodType.methodType(MethodHandle.class,ConstantCallSite.class));
149
boolean wmtHit = false;
150
151
try {
152
MyConstantCallSite myCcs = new MyConstantCallSite(targetType,hook);
153
154
}catch ( WrongMethodTypeException e ) {
155
wmtHit = true;
156
}
157
158
AssertJUnit.assertTrue(wmtHit);
159
}
160
161
/**
162
* Negative test for protected ConstantCallSite(MethodType targetType,MethodHandle createTargetHook).
163
* The hook returns null.
164
* A subclass of ConstantCallSite is used to invoke the protected constructor
165
* @throws Throwable
166
*/
167
@Test(groups = { "level.extended" })
168
public void testNullHook_TargetHookConstructor() throws Throwable {
169
170
MethodType targetType = MethodType.methodType(String.class);
171
MethodHandle hook = MethodHandles.lookup().findStatic(ConstantCallSiteTest.class, "createTargetHook_Null", MethodType.methodType(MethodHandle.class,ConstantCallSite.class));
172
boolean npeHit = false;
173
174
try {
175
MyConstantCallSite myCcs = new MyConstantCallSite(targetType,hook);
176
177
}catch ( NullPointerException e ) {
178
npeHit = true;
179
}
180
181
AssertJUnit.assertTrue(npeHit);
182
}
183
184
185
/**
186
* Negative test for protected ConstantCallSite(MethodType targetType,MethodHandle createTargetHook).
187
* The hook does not return a MethodHandle object.
188
* A subclass of ConstantCallSite is used to invoke the protected constructor
189
* @throws Throwable
190
*/
191
@Test(groups = { "level.extended" })
192
public void testClassCast_TargetHookConstructor() throws Throwable {
193
194
MethodType targetType = MethodType.methodType(String.class);
195
MethodHandle hook = MethodHandles.lookup().findStatic(ConstantCallSiteTest.class, "createTargetHook_NonMH", MethodType.methodType(ConstantCallSiteTest.class,ConstantCallSite.class));
196
boolean cceHit = false;
197
198
try {
199
MyConstantCallSite myCcs = new MyConstantCallSite(targetType,hook);
200
201
}catch ( ClassCastException e ) {
202
cceHit = true;
203
}
204
205
AssertJUnit.assertTrue(cceHit);
206
}
207
208
/**
209
* Test to validate that a partially constructed ConstantCallSite can't be incorrectly used
210
* @throws Throwable
211
*/
212
CallSite unconstructedCS;
213
214
@Test(groups = { "level.extended" })
215
public void testUnconstructedCCS() throws Throwable {
216
ConstantCallSiteTest ccs = new ConstantCallSiteTest();
217
MethodHandle hookMH = MethodHandles.lookup().bind(ccs, "hook", MethodType.methodType(MethodHandle.class, ConstantCallSite.class));
218
219
try {
220
ConstantCallSite cs = new MyConstantCallSite(MethodType.methodType(void.class), hookMH);
221
} catch(NullPointerException e) {};
222
223
boolean illegalState = false;
224
225
try {
226
ccs.unconstructedCS.dynamicInvoker();
227
} catch(IllegalStateException e) {
228
illegalState = true;
229
}
230
231
AssertJUnit.assertTrue(illegalState);
232
233
illegalState = false;
234
try {
235
ccs.unconstructedCS.getTarget();
236
} catch(IllegalStateException e) {
237
illegalState = true;
238
}
239
240
AssertJUnit.assertTrue(illegalState);
241
242
illegalState = false;
243
try {
244
ccs.unconstructedCS.type();
245
} catch(IllegalStateException e) {
246
illegalState = true;
247
}
248
249
AssertJUnit.assertFalse(illegalState);
250
}
251
252
public MethodHandle hook(ConstantCallSite cs) {
253
unconstructedCS = cs;
254
throw new NullPointerException();
255
}
256
257
/**
258
* Subclass of ConstantCallSite where protected ConstantCallSite(MethodType targetType,MethodHandle createTargetHook) is invoked
259
*/
260
private class MyConstantCallSite extends ConstantCallSite {
261
public MyConstantCallSite(MethodType target, MethodHandle createTargetHook) throws Throwable {
262
super(target,createTargetHook);
263
}
264
}
265
266
/**
267
* This method is used to produce the final target method handle that will be hooked to the constant call site
268
* @param c
269
* @return
270
* @throws Throwable
271
*/
272
public static MethodHandle createTargetHook(ConstantCallSite c) throws Throwable {
273
MethodHandle targetHook = MethodHandles.lookup().findStatic(ConstantCallSiteTest.class, "getOne", MethodType.methodType(int.class));
274
return targetHook;
275
}
276
277
/**
278
* This method is used to produce the null target method handle that will be hooked to the constant call site
279
* @param c
280
* @return
281
* @throws Throwable
282
*/
283
public static MethodHandle createTargetHook_Null(ConstantCallSite c) throws Throwable {
284
return null;
285
}
286
287
/**
288
* This method is used to produce the null target method handle that will be hooked to the constant call site
289
* @param c
290
* @return
291
* @throws Throwable
292
*/
293
public static ConstantCallSiteTest createTargetHook_NonMH(ConstantCallSite c) throws Throwable {
294
return new ConstantCallSiteTest();
295
}
296
297
298
/**
299
* Target method to be hooked to the call site
300
* @return always returns 1
301
*/
302
public static int getOne() {
303
return 1;
304
}
305
306
/**
307
* Test for ConstantCallSite.type()
308
* @throws Throwable
309
*/
310
@Test(groups = { "level.extended" })
311
public void testType_ConstantCallSite() throws Throwable {
312
MethodType mt = MethodType.methodType(int.class,int.class,int.class);
313
MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "addPublicStatic",mt);
314
ConstantCallSite ccs = new ConstantCallSite(mh);
315
AssertJUnit.assertEquals(mt,ccs.type());
316
}
317
}
318
319