Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 1 | // 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. |
| 4 | package varhandle; |
| 5 | |
| 6 | import java.lang.invoke.MethodHandles; |
| 7 | import java.lang.invoke.VarHandle; |
| 8 | |
| 9 | public class InstanceLongField { |
| 10 | |
| 11 | private long field; |
| 12 | |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 13 | private static void checkJavaLangInvokeWrongMethodTypeException(RuntimeException e) { |
| 14 | if (e.getClass().getCanonicalName().equals("java.lang.invoke.WrongMethodTypeException") |
| 15 | || e.getMessage().equals("java.lang.invoke.WrongMethodTypeException")) { |
| 16 | return; |
| 17 | } |
| 18 | throw e; |
| 19 | } |
| 20 | |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 21 | public static void testSet(VarHandle varHandle) { |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 22 | System.out.println("testSet"); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 23 | |
| 24 | InstanceLongField instance = new InstanceLongField(); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 25 | System.out.println((long) varHandle.get(instance)); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 26 | |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 27 | // Long value. |
| 28 | varHandle.set(instance, (long) 1); |
| 29 | System.out.println((long) varHandle.get(instance)); |
| 30 | varHandle.set(instance, Long.valueOf(2)); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 31 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 32 | |
| 33 | // Long compatible values. |
| 34 | varHandle.set(instance, (byte) 3); |
| 35 | System.out.println((long) varHandle.get(instance)); |
| 36 | varHandle.set(instance, Byte.valueOf((byte) 4)); |
| 37 | System.out.println((long) varHandle.get(instance)); |
| 38 | varHandle.set(instance, '0'); |
| 39 | System.out.println((long) varHandle.get(instance)); |
| 40 | varHandle.set(instance, Character.valueOf('1')); |
| 41 | System.out.println((long) varHandle.get(instance)); |
| 42 | varHandle.set(instance, (short) 5); |
| 43 | System.out.println((long) varHandle.get(instance)); |
| 44 | varHandle.set(instance, Short.valueOf((short) 6)); |
| 45 | System.out.println((long) varHandle.get(instance)); |
| 46 | varHandle.set(instance, (int) 7); |
| 47 | System.out.println((long) varHandle.get(instance)); |
| 48 | varHandle.set(instance, Integer.valueOf(8)); |
| 49 | System.out.println((long) varHandle.get(instance)); |
| 50 | |
| 51 | // Long non-compatible values. |
| 52 | try { |
| 53 | varHandle.set(instance, true); |
| 54 | } catch (RuntimeException e) { |
| 55 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 56 | System.out.println(varHandle.get(instance)); |
| 57 | } |
| 58 | try { |
| 59 | varHandle.set(instance, "3"); |
| 60 | } catch (RuntimeException e) { |
| 61 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 62 | System.out.println(varHandle.get(instance)); |
| 63 | } |
| 64 | try { |
| 65 | varHandle.set(instance, 3.0f); |
| 66 | } catch (RuntimeException e) { |
| 67 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 68 | System.out.println(varHandle.get(instance)); |
| 69 | } |
| 70 | try { |
| 71 | varHandle.set(instance, 3.0); |
| 72 | } catch (RuntimeException e) { |
| 73 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 74 | System.out.println(varHandle.get(instance)); |
| 75 | } |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public static void testCompareAndSet(VarHandle varHandle) { |
| 79 | System.out.println("testCompareAndSet"); |
| 80 | |
| 81 | InstanceLongField instance = new InstanceLongField(); |
| 82 | |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 83 | // Long value. |
| 84 | varHandle.compareAndSet(instance, 1L, 2L); |
| 85 | System.out.println((long) varHandle.get(instance)); |
| 86 | varHandle.compareAndSet(instance, 0L, 1L); |
| 87 | System.out.println((long) varHandle.get(instance)); |
| 88 | varHandle.compareAndSet(instance, Long.valueOf(1), 2); |
| 89 | System.out.println((long) varHandle.get(instance)); |
| 90 | varHandle.compareAndSet(instance, 2, Long.valueOf(3)); |
| 91 | System.out.println((long) varHandle.get(instance)); |
| 92 | varHandle.compareAndSet(instance, Long.valueOf(3), Long.valueOf(4)); |
| 93 | System.out.println((long) varHandle.get(instance)); |
| 94 | |
| 95 | // Long compatible values. |
| 96 | varHandle.compareAndSet(instance, (byte) 4, 5); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 97 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 98 | varHandle.compareAndSet(instance, 5, (byte) 6); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 99 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame^] | 100 | varHandle.compareAndSet(instance, (byte) 6, (byte) 7); |
| 101 | System.out.println(varHandle.get(instance)); |
| 102 | varHandle.compareAndSet(instance, Byte.valueOf((byte) 7), (byte) 8); |
| 103 | System.out.println(varHandle.get(instance)); |
| 104 | varHandle.compareAndSet(instance, (byte) 8, Byte.valueOf((byte) 9)); |
| 105 | System.out.println(varHandle.get(instance)); |
| 106 | varHandle.compareAndSet(instance, Byte.valueOf((byte) 9), Byte.valueOf((byte) 10)); |
| 107 | System.out.println(varHandle.get(instance)); |
| 108 | varHandle.compareAndSet(instance, 10, '0'); |
| 109 | System.out.println(varHandle.get(instance)); |
| 110 | varHandle.compareAndSet(instance, '0', 49); |
| 111 | System.out.println(varHandle.get(instance)); |
| 112 | varHandle.compareAndSet(instance, '1', '2'); |
| 113 | System.out.println(varHandle.get(instance)); |
| 114 | varHandle.compareAndSet(instance, (byte) 50, '3'); |
| 115 | System.out.println(varHandle.get(instance)); |
| 116 | varHandle.compareAndSet(instance, '3', (byte) 52); |
| 117 | System.out.println(varHandle.get(instance)); |
| 118 | varHandle.compareAndSet(instance, '4', '5'); |
| 119 | System.out.println(varHandle.get(instance)); |
| 120 | varHandle.compareAndSet(instance, '5', (int) 11); |
| 121 | System.out.println(varHandle.get(instance)); |
| 122 | varHandle.compareAndSet(instance, (int) 11, (int) 12); |
| 123 | System.out.println(varHandle.get(instance)); |
| 124 | varHandle.compareAndSet(instance, (int) 12, Integer.valueOf(13)); |
| 125 | System.out.println(varHandle.get(instance)); |
| 126 | varHandle.compareAndSet(instance, Integer.valueOf(13), Integer.valueOf(14)); |
| 127 | System.out.println(varHandle.get(instance)); |
| 128 | |
| 129 | // Long non-compatible values. |
| 130 | try { |
| 131 | varHandle.compareAndSet(instance, 6, 7.0f); |
| 132 | } catch (RuntimeException e) { |
| 133 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 134 | System.out.println(varHandle.get(instance)); |
| 135 | } |
| 136 | try { |
| 137 | varHandle.compareAndSet(instance, 6.0f, 7); |
| 138 | } catch (RuntimeException e) { |
| 139 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 140 | System.out.println(varHandle.get(instance)); |
| 141 | } |
| 142 | try { |
| 143 | varHandle.compareAndSet(instance, 6.0f, 7.0f); |
| 144 | } catch (RuntimeException e) { |
| 145 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 146 | System.out.println(varHandle.get(instance)); |
| 147 | } |
| 148 | try { |
| 149 | varHandle.compareAndSet(instance, 6, 7.0); |
| 150 | } catch (RuntimeException e) { |
| 151 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 152 | System.out.println(varHandle.get(instance)); |
| 153 | } |
| 154 | try { |
| 155 | varHandle.compareAndSet(instance, 6.0, 7); |
| 156 | } catch (RuntimeException e) { |
| 157 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 158 | System.out.println(varHandle.get(instance)); |
| 159 | } |
| 160 | try { |
| 161 | varHandle.compareAndSet(instance, 6.0, 7.0); |
| 162 | } catch (RuntimeException e) { |
| 163 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 164 | System.out.println(varHandle.get(instance)); |
| 165 | } |
| 166 | |
| 167 | try { |
| 168 | varHandle.compareAndSet(instance, 6, "7"); |
| 169 | } catch (RuntimeException e) { |
| 170 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 171 | System.out.println(varHandle.get(instance)); |
| 172 | } |
| 173 | try { |
| 174 | varHandle.compareAndSet(instance, "6", 7); |
| 175 | } catch (RuntimeException e) { |
| 176 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 177 | System.out.println(varHandle.get(instance)); |
| 178 | } |
| 179 | try { |
| 180 | varHandle.compareAndSet(instance, "6", "7"); |
| 181 | } catch (RuntimeException e) { |
| 182 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 183 | System.out.println(varHandle.get(instance)); |
| 184 | } |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
| 188 | VarHandle varHandle = |
| 189 | MethodHandles.lookup().findVarHandle(InstanceLongField.class, "field", long.class); |
| 190 | testSet(varHandle); |
| 191 | testCompareAndSet(varHandle); |
| 192 | } |
| 193 | } |