blob: 242df8974d191e8e39b504ee50ccb7ceedf84bd1 [file] [log] [blame]
Søren Gjesse75996df2022-12-12 13:08:20 +01001// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package varhandle;
5
6import java.lang.invoke.MethodHandles;
7import java.lang.invoke.VarHandle;
8
9public class InstanceByteField {
10
11 private byte field;
12
13 public static void testSet(VarHandle varHandle) {
14 System.out.println("testGet");
15
16 InstanceByteField instance = new InstanceByteField();
17 System.out.println(varHandle.get(instance));
18
19 // byte and Byte values.
20 varHandle.set(instance, (byte) 1);
21 System.out.println((byte) varHandle.get(instance));
22 varHandle.set(instance, Byte.valueOf((byte) 2));
23 System.out.println(varHandle.get(instance));
24 }
25
26 public static void testCompareAndSet(VarHandle varHandle) {
27 System.out.println("testCompareAndSet");
28
29 InstanceByteField instance = new InstanceByteField();
30
31 // byte and Byte values.
32 varHandle.compareAndSet(instance, (byte) 1, (byte) 2);
33 System.out.println((byte) varHandle.get(instance));
34 varHandle.compareAndSet(instance, (byte) 0, (byte) 1);
35 System.out.println((byte) varHandle.get(instance));
36 varHandle.compareAndSet(instance, Byte.valueOf((byte) 1), (byte) 2);
37 System.out.println(varHandle.get(instance));
38 varHandle.compareAndSet(instance, (byte) 2, Byte.valueOf((byte) 3));
39 System.out.println(varHandle.get(instance));
40 varHandle.compareAndSet(instance, Byte.valueOf((byte) 3), Byte.valueOf((byte) 4));
41 System.out.println(varHandle.get(instance));
42 }
43
44 public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
45 VarHandle varHandle;
46 try {
47 varHandle =
48 MethodHandles.lookup().findVarHandle(InstanceByteField.class, "field", byte.class);
49 } catch (UnsupportedOperationException e) {
50 System.out.println("Got UnsupportedOperationException");
51 return;
52 }
53
54 testSet(varHandle);
55 testCompareAndSet(varHandle);
56 }
57}