1 /*
2 * Copyright (c) 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 /*
25 * @test
26 * @run testng/othervm -Diters=10 -Xint VarHandleTestAccess$Type$
27 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
28 * @run testng/othervm -Diters=20000 VarHandleTestAccess$Type$
29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccess$Type$
30 */
31
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35
36 import java.lang.invoke.MethodHandles;
37 import java.lang.invoke.VarHandle;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42 import static org.testng.Assert.*;
43
44 public class VarHandleTestAccess$Type$ extends VarHandleBaseTest {
45 static final $type$ static_final_v = $value1$;
46
47 static $type$ static_v;
48
49 final $type$ final_v = $value1$;
50
51 $type$ v;
52
53 VarHandle vhFinalField;
54
55 VarHandle vhField;
56
57 VarHandle vhStaticField;
58
59 VarHandle vhStaticFinalField;
60
61 VarHandle vhArray;
62
63 @BeforeClass
64 public void setup() throws Exception {
65 vhFinalField = MethodHandles.lookup().findVarHandle(
66 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
67
68 vhField = MethodHandles.lookup().findVarHandle(
69 VarHandleTestAccess$Type$.class, "v", $type$.class);
70
71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
72 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
73
74 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
75 VarHandleTestAccess$Type$.class, "static_v", $type$.class);
76
77 vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
78 }
79
80
81 @DataProvider
82 public Object[][] varHandlesProvider() throws Exception {
83 List<VarHandle> vhs = new ArrayList<>();
84 vhs.add(vhField);
85 vhs.add(vhStaticField);
86 vhs.add(vhArray);
87
88 return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
89 }
90
91 @Test(dataProvider = "varHandlesProvider")
92 public void testIsAccessModeSupported(VarHandle vh) {
93 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
225 cases.add(new VarHandleAccessTestCase("Instance field",
226 vhField, vh -> testInstanceField(this, vh)));
227 cases.add(new VarHandleAccessTestCase("Instance field unsupported",
228 vhField, vh -> testInstanceFieldUnsupported(this, vh),
229 false));
230
231 cases.add(new VarHandleAccessTestCase("Static field",
232 vhStaticField, VarHandleTestAccess$Type$::testStaticField));
233 cases.add(new VarHandleAccessTestCase("Static field unsupported",
234 vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
235 false));
236
237 cases.add(new VarHandleAccessTestCase("Array",
238 vhArray, VarHandleTestAccess$Type$::testArray));
239 cases.add(new VarHandleAccessTestCase("Array unsupported",
240 vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
241 false));
242 cases.add(new VarHandleAccessTestCase("Array index out of bounds",
243 vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
244 false));
245
246 // Work around issue with jtreg summary reporting which truncates
247 // the String result of Object.toString to 30 characters, hence
248 // the first dummy argument
249 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
250 }
251
252 @Test(dataProvider = "accessTestCaseProvider")
253 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
254 T t = atc.get();
255 int iters = atc.requiresLoop() ? ITERS : 1;
256 for (int c = 0; c < iters; c++) {
257 atc.testAccess(t);
258 }
259 }
260
261
262
263
264 static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
387 });
388
389 checkUOE(() -> {
390 $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
391 });
392
393 checkUOE(() -> {
394 $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
395 });
396
397 checkUOE(() -> {
398 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
399 });
400
401 checkUOE(() -> {
402 $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
403 });
404 #end[Bitwise]
405 }
406
407
408 static void testStaticFinalField(VarHandle vh) {
409 // Plain
410 {
411 $type$ x = ($type$) vh.get();
412 assertEquals(x, $value1$, "get $type$ value");
413 }
414
415
416 // Volatile
417 {
418 $type$ x = ($type$) vh.getVolatile();
419 assertEquals(x, $value1$, "getVolatile $type$ value");
420 }
421
422 // Lazy
423 {
424 $type$ x = ($type$) vh.getAcquire();
425 assertEquals(x, $value1$, "getRelease $type$ value");
426 }
|
1 /*
2 * Copyright (c) 2015, 2018, 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 #if[Value]
25 /*
26 * @test
27 * @compile -XDenableValueTypes Value.java
28 * @run testng/othervm -Xverify:none -Diters=10 -Xint VarHandleTestAccess$Type$
29 */
30 /* Disabled temporarily for lworld
31 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
32 * @run testng/othervm -Diters=20000 VarHandleTestAccess$Type$
33 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccess$Type$
34 */
35 #else[Value]
36 /*
37 * @test
38 * @run testng/othervm -Diters=10 -Xint VarHandleTestAccess$Type$
39 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
40 * @run testng/othervm -Diters=20000 VarHandleTestAccess$Type$
41 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccess$Type$
42 */
43 #end[Value]
44
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48
49 import java.lang.invoke.MethodHandles;
50 import java.lang.invoke.VarHandle;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.List;
54
55 import static org.testng.Assert.*;
56
57 public class VarHandleTestAccess$Type$ extends VarHandleBaseTest {
58 static final $type$ static_final_v = $value1$;
59
60 static $type$ static_v;
61
62 final $type$ final_v = $value1$;
63
64 $type$ v;
65
66 VarHandle vhFinalField;
67
68 VarHandle vhField;
69
70 VarHandle vhStaticField;
71
72 VarHandle vhStaticFinalField;
73
74 VarHandle vhArray;
75
76 #if[Value]
77 VarHandle vhValueTypeField;
78 #end[Value]
79
80 @BeforeClass
81 public void setup() throws Exception {
82 vhFinalField = MethodHandles.lookup().findVarHandle(
83 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
84
85 vhField = MethodHandles.lookup().findVarHandle(
86 VarHandleTestAccess$Type$.class, "v", $type$.class);
87
88 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
89 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
90
91 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
92 VarHandleTestAccess$Type$.class, "static_v", $type$.class);
93
94 vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
95
96 #if[Value]
97 vhValueTypeField = MethodHandles.lookup().findVarHandle(
98 Value.class, "$type$_v", $type$.class);
99 #end[Value]
100 }
101
102
103 @DataProvider
104 public Object[][] varHandlesProvider() throws Exception {
105 List<VarHandle> vhs = new ArrayList<>();
106 vhs.add(vhField);
107 vhs.add(vhStaticField);
108 vhs.add(vhArray);
109
110 return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
111 }
112
113 @Test(dataProvider = "varHandlesProvider")
114 public void testIsAccessModeSupported(VarHandle vh) {
115 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
116 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
117 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
118 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
119 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
247 cases.add(new VarHandleAccessTestCase("Instance field",
248 vhField, vh -> testInstanceField(this, vh)));
249 cases.add(new VarHandleAccessTestCase("Instance field unsupported",
250 vhField, vh -> testInstanceFieldUnsupported(this, vh),
251 false));
252
253 cases.add(new VarHandleAccessTestCase("Static field",
254 vhStaticField, VarHandleTestAccess$Type$::testStaticField));
255 cases.add(new VarHandleAccessTestCase("Static field unsupported",
256 vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
257 false));
258
259 cases.add(new VarHandleAccessTestCase("Array",
260 vhArray, VarHandleTestAccess$Type$::testArray));
261 cases.add(new VarHandleAccessTestCase("Array unsupported",
262 vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
263 false));
264 cases.add(new VarHandleAccessTestCase("Array index out of bounds",
265 vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
266 false));
267 #if[Value]
268 cases.add(new VarHandleAccessTestCase("Value type field",
269 vhValueTypeField, vh -> testValueTypeField(Value.VT, vh)));
270 cases.add(new VarHandleAccessTestCase("Value type field unsupported",
271 vhValueTypeField, vh -> testValueTypeFieldUnsupported(Value.VT, vh),
272 false));
273 #end[Value]
274
275 // Work around issue with jtreg summary reporting which truncates
276 // the String result of Object.toString to 30 characters, hence
277 // the first dummy argument
278 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
279 }
280
281 @Test(dataProvider = "accessTestCaseProvider")
282 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
283 T t = atc.get();
284 int iters = atc.requiresLoop() ? ITERS : 1;
285 for (int c = 0; c < iters; c++) {
286 atc.testAccess(t);
287 }
288 }
289
290
291
292
293 static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
416 });
417
418 checkUOE(() -> {
419 $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
420 });
421
422 checkUOE(() -> {
423 $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
424 });
425
426 checkUOE(() -> {
427 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
428 });
429
430 checkUOE(() -> {
431 $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
432 });
433 #end[Bitwise]
434 }
435
436 #if[Value]
437 static void testValueTypeField(Value recv, VarHandle vh) {
438 // Plain
439 {
440 $type$ x = ($type$) vh.get(recv);
441 assertEquals(x, $value1$, "get $type$ value");
442 }
443 }
444
445 static void testValueTypeFieldUnsupported(Value recv, VarHandle vh) {
446 checkUOE(() -> {
447 vh.set(recv, $value2$);
448 });
449 }
450 #end[Value]
451
452 static void testStaticFinalField(VarHandle vh) {
453 // Plain
454 {
455 $type$ x = ($type$) vh.get();
456 assertEquals(x, $value1$, "get $type$ value");
457 }
458
459
460 // Volatile
461 {
462 $type$ x = ($type$) vh.getVolatile();
463 assertEquals(x, $value1$, "getVolatile $type$ value");
464 }
465
466 // Lazy
467 {
468 $type$ x = ($type$) vh.getAcquire();
469 assertEquals(x, $value1$, "getRelease $type$ value");
470 }
|