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 | ca343fd | 2022-12-20 16:22:00 +0100 | [diff] [blame] | 21 | public static void testGet(VarHandle varHandle) { |
| 22 | System.out.println("testGet"); |
| 23 | |
| 24 | InstanceLongField instance = new InstanceLongField(); |
| 25 | varHandle.set(instance, 1L); |
| 26 | |
| 27 | System.out.println(varHandle.get(instance)); |
| 28 | System.out.println((Object) varHandle.get(instance)); |
| 29 | System.out.println((long) varHandle.get(instance)); |
| 30 | System.out.println((float) varHandle.get(instance)); |
| 31 | System.out.println((double) varHandle.get(instance)); |
| 32 | try { |
| 33 | System.out.println((boolean) varHandle.get(instance)); |
| 34 | System.out.println("Unexpected success"); |
| 35 | } catch (RuntimeException e) { |
| 36 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 37 | } |
| 38 | try { |
| 39 | System.out.println((byte) varHandle.get(instance)); |
| 40 | System.out.println("Unexpected success"); |
| 41 | } catch (RuntimeException e) { |
| 42 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 43 | } |
| 44 | try { |
| 45 | System.out.println((short) varHandle.get(instance)); |
| 46 | System.out.println("Unexpected success"); |
| 47 | } catch (RuntimeException e) { |
| 48 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 49 | } |
| 50 | try { |
| 51 | System.out.println((char) varHandle.get(instance)); |
| 52 | System.out.println("Unexpected success"); |
| 53 | } catch (RuntimeException e) { |
| 54 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 55 | } |
| 56 | try { |
| 57 | System.out.println((int) varHandle.get(instance)); |
| 58 | System.out.println("Unexpected success 5"); |
| 59 | } catch (RuntimeException e) { |
| 60 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 61 | } |
| 62 | try { |
| 63 | System.out.println((String) varHandle.get(instance)); |
| 64 | System.out.println("Unexpected success"); |
| 65 | } catch (RuntimeException e) { |
| 66 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 67 | } |
| 68 | } |
| 69 | |
Søren Gjesse | deab69d | 2023-01-02 15:25:36 +0100 | [diff] [blame] | 70 | public static void testGetVolatile(VarHandle varHandle) { |
| 71 | System.out.println("testGetVolatile"); |
| 72 | |
| 73 | InstanceLongField instance = new InstanceLongField(); |
| 74 | varHandle.set(instance, 1); |
| 75 | |
| 76 | System.out.println(varHandle.getVolatile(instance)); |
| 77 | System.out.println((Object) varHandle.getVolatile(instance)); |
| 78 | System.out.println((long) varHandle.getVolatile(instance)); |
| 79 | System.out.println((float) varHandle.getVolatile(instance)); |
| 80 | System.out.println((double) varHandle.getVolatile(instance)); |
| 81 | try { |
| 82 | System.out.println((boolean) varHandle.getVolatile(instance)); |
| 83 | System.out.println("Unexpected success"); |
| 84 | } catch (RuntimeException e) { |
| 85 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 86 | } |
| 87 | try { |
| 88 | System.out.println((byte) varHandle.getVolatile(instance)); |
| 89 | System.out.println("Unexpected success"); |
| 90 | } catch (RuntimeException e) { |
| 91 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 92 | } |
| 93 | try { |
| 94 | System.out.println((short) varHandle.getVolatile(instance)); |
| 95 | System.out.println("Unexpected success"); |
| 96 | } catch (RuntimeException e) { |
| 97 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 98 | } |
| 99 | try { |
| 100 | System.out.println((char) varHandle.getVolatile(instance)); |
| 101 | System.out.println("Unexpected success"); |
| 102 | } catch (RuntimeException e) { |
| 103 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 104 | } |
| 105 | try { |
| 106 | System.out.println((int) varHandle.getVolatile(instance)); |
| 107 | System.out.println("Unexpected success"); |
| 108 | } catch (RuntimeException e) { |
| 109 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 110 | } |
| 111 | try { |
| 112 | System.out.println((String) varHandle.getVolatile(instance)); |
| 113 | System.out.println("Unexpected success"); |
| 114 | } catch (RuntimeException e) { |
| 115 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 116 | } |
| 117 | } |
| 118 | |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 119 | public static void testSet(VarHandle varHandle) { |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 120 | System.out.println("testSet"); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 121 | |
| 122 | InstanceLongField instance = new InstanceLongField(); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 123 | System.out.println((long) varHandle.get(instance)); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 124 | |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 125 | // Long value. |
| 126 | varHandle.set(instance, (long) 1); |
| 127 | System.out.println((long) varHandle.get(instance)); |
| 128 | varHandle.set(instance, Long.valueOf(2)); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 129 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 130 | |
| 131 | // Long compatible values. |
| 132 | varHandle.set(instance, (byte) 3); |
| 133 | System.out.println((long) varHandle.get(instance)); |
| 134 | varHandle.set(instance, Byte.valueOf((byte) 4)); |
| 135 | System.out.println((long) varHandle.get(instance)); |
| 136 | varHandle.set(instance, '0'); |
| 137 | System.out.println((long) varHandle.get(instance)); |
| 138 | varHandle.set(instance, Character.valueOf('1')); |
| 139 | System.out.println((long) varHandle.get(instance)); |
| 140 | varHandle.set(instance, (short) 5); |
| 141 | System.out.println((long) varHandle.get(instance)); |
| 142 | varHandle.set(instance, Short.valueOf((short) 6)); |
| 143 | System.out.println((long) varHandle.get(instance)); |
| 144 | varHandle.set(instance, (int) 7); |
| 145 | System.out.println((long) varHandle.get(instance)); |
| 146 | varHandle.set(instance, Integer.valueOf(8)); |
| 147 | System.out.println((long) varHandle.get(instance)); |
| 148 | |
| 149 | // Long non-compatible values. |
| 150 | try { |
| 151 | varHandle.set(instance, true); |
| 152 | } catch (RuntimeException e) { |
| 153 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 154 | System.out.println(varHandle.get(instance)); |
| 155 | } |
| 156 | try { |
| 157 | varHandle.set(instance, "3"); |
| 158 | } catch (RuntimeException e) { |
| 159 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 160 | System.out.println(varHandle.get(instance)); |
| 161 | } |
| 162 | try { |
| 163 | varHandle.set(instance, 3.0f); |
| 164 | } catch (RuntimeException e) { |
| 165 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 166 | System.out.println(varHandle.get(instance)); |
| 167 | } |
| 168 | try { |
| 169 | varHandle.set(instance, 3.0); |
| 170 | } catch (RuntimeException e) { |
| 171 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 172 | System.out.println(varHandle.get(instance)); |
| 173 | } |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Søren Gjesse | 0a319f6 | 2023-01-02 15:25:47 +0100 | [diff] [blame^] | 176 | public static void testSetVolatile(VarHandle varHandle) { |
| 177 | System.out.println("testSetVolatile"); |
| 178 | |
| 179 | InstanceLongField instance = new InstanceLongField(); |
| 180 | System.out.println((long) varHandle.get(instance)); |
| 181 | |
| 182 | // Long value. |
| 183 | varHandle.setVolatile(instance, (long) 1); |
| 184 | System.out.println((long) varHandle.get(instance)); |
| 185 | varHandle.setVolatile(instance, Long.valueOf(2)); |
| 186 | System.out.println(varHandle.get(instance)); |
| 187 | |
| 188 | // Long compatible values. |
| 189 | varHandle.setVolatile(instance, (byte) 3); |
| 190 | System.out.println((long) varHandle.get(instance)); |
| 191 | varHandle.setVolatile(instance, Byte.valueOf((byte) 4)); |
| 192 | System.out.println((long) varHandle.get(instance)); |
| 193 | varHandle.setVolatile(instance, '0'); |
| 194 | System.out.println((long) varHandle.get(instance)); |
| 195 | varHandle.setVolatile(instance, Character.valueOf('1')); |
| 196 | System.out.println((long) varHandle.get(instance)); |
| 197 | varHandle.setVolatile(instance, (short) 5); |
| 198 | System.out.println((long) varHandle.get(instance)); |
| 199 | varHandle.setVolatile(instance, Short.valueOf((short) 6)); |
| 200 | System.out.println((long) varHandle.get(instance)); |
| 201 | varHandle.setVolatile(instance, (int) 7); |
| 202 | System.out.println((long) varHandle.get(instance)); |
| 203 | varHandle.setVolatile(instance, Integer.valueOf(8)); |
| 204 | System.out.println((long) varHandle.get(instance)); |
| 205 | |
| 206 | // Long non-compatible values. |
| 207 | try { |
| 208 | varHandle.setVolatile(instance, true); |
| 209 | } catch (RuntimeException e) { |
| 210 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 211 | System.out.println(varHandle.get(instance)); |
| 212 | } |
| 213 | try { |
| 214 | varHandle.setVolatile(instance, "3"); |
| 215 | } catch (RuntimeException e) { |
| 216 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 217 | System.out.println(varHandle.get(instance)); |
| 218 | } |
| 219 | try { |
| 220 | varHandle.setVolatile(instance, 3.0f); |
| 221 | } catch (RuntimeException e) { |
| 222 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 223 | System.out.println(varHandle.get(instance)); |
| 224 | } |
| 225 | try { |
| 226 | varHandle.setVolatile(instance, 3.0); |
| 227 | } catch (RuntimeException e) { |
| 228 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 229 | System.out.println(varHandle.get(instance)); |
| 230 | } |
| 231 | } |
| 232 | |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 233 | public static void testCompareAndSet(VarHandle varHandle) { |
| 234 | System.out.println("testCompareAndSet"); |
| 235 | |
| 236 | InstanceLongField instance = new InstanceLongField(); |
| 237 | |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 238 | // Long value. |
| 239 | varHandle.compareAndSet(instance, 1L, 2L); |
| 240 | System.out.println((long) varHandle.get(instance)); |
| 241 | varHandle.compareAndSet(instance, 0L, 1L); |
| 242 | System.out.println((long) varHandle.get(instance)); |
| 243 | varHandle.compareAndSet(instance, Long.valueOf(1), 2); |
| 244 | System.out.println((long) varHandle.get(instance)); |
| 245 | varHandle.compareAndSet(instance, 2, Long.valueOf(3)); |
| 246 | System.out.println((long) varHandle.get(instance)); |
| 247 | varHandle.compareAndSet(instance, Long.valueOf(3), Long.valueOf(4)); |
| 248 | System.out.println((long) varHandle.get(instance)); |
| 249 | |
| 250 | // Long compatible values. |
| 251 | varHandle.compareAndSet(instance, (byte) 4, 5); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 252 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 253 | varHandle.compareAndSet(instance, 5, (byte) 6); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 254 | System.out.println(varHandle.get(instance)); |
Søren Gjesse | 47cb975 | 2022-12-06 08:39:57 +0100 | [diff] [blame] | 255 | varHandle.compareAndSet(instance, (byte) 6, (byte) 7); |
| 256 | System.out.println(varHandle.get(instance)); |
| 257 | varHandle.compareAndSet(instance, Byte.valueOf((byte) 7), (byte) 8); |
| 258 | System.out.println(varHandle.get(instance)); |
| 259 | varHandle.compareAndSet(instance, (byte) 8, Byte.valueOf((byte) 9)); |
| 260 | System.out.println(varHandle.get(instance)); |
| 261 | varHandle.compareAndSet(instance, Byte.valueOf((byte) 9), Byte.valueOf((byte) 10)); |
| 262 | System.out.println(varHandle.get(instance)); |
| 263 | varHandle.compareAndSet(instance, 10, '0'); |
| 264 | System.out.println(varHandle.get(instance)); |
| 265 | varHandle.compareAndSet(instance, '0', 49); |
| 266 | System.out.println(varHandle.get(instance)); |
| 267 | varHandle.compareAndSet(instance, '1', '2'); |
| 268 | System.out.println(varHandle.get(instance)); |
| 269 | varHandle.compareAndSet(instance, (byte) 50, '3'); |
| 270 | System.out.println(varHandle.get(instance)); |
| 271 | varHandle.compareAndSet(instance, '3', (byte) 52); |
| 272 | System.out.println(varHandle.get(instance)); |
| 273 | varHandle.compareAndSet(instance, '4', '5'); |
| 274 | System.out.println(varHandle.get(instance)); |
| 275 | varHandle.compareAndSet(instance, '5', (int) 11); |
| 276 | System.out.println(varHandle.get(instance)); |
| 277 | varHandle.compareAndSet(instance, (int) 11, (int) 12); |
| 278 | System.out.println(varHandle.get(instance)); |
| 279 | varHandle.compareAndSet(instance, (int) 12, Integer.valueOf(13)); |
| 280 | System.out.println(varHandle.get(instance)); |
| 281 | varHandle.compareAndSet(instance, Integer.valueOf(13), Integer.valueOf(14)); |
| 282 | System.out.println(varHandle.get(instance)); |
| 283 | |
| 284 | // Long non-compatible values. |
| 285 | try { |
| 286 | varHandle.compareAndSet(instance, 6, 7.0f); |
| 287 | } catch (RuntimeException e) { |
| 288 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 289 | System.out.println(varHandle.get(instance)); |
| 290 | } |
| 291 | try { |
| 292 | varHandle.compareAndSet(instance, 6.0f, 7); |
| 293 | } catch (RuntimeException e) { |
| 294 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 295 | System.out.println(varHandle.get(instance)); |
| 296 | } |
| 297 | try { |
| 298 | varHandle.compareAndSet(instance, 6.0f, 7.0f); |
| 299 | } catch (RuntimeException e) { |
| 300 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 301 | System.out.println(varHandle.get(instance)); |
| 302 | } |
| 303 | try { |
| 304 | varHandle.compareAndSet(instance, 6, 7.0); |
| 305 | } catch (RuntimeException e) { |
| 306 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 307 | System.out.println(varHandle.get(instance)); |
| 308 | } |
| 309 | try { |
| 310 | varHandle.compareAndSet(instance, 6.0, 7); |
| 311 | } catch (RuntimeException e) { |
| 312 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 313 | System.out.println(varHandle.get(instance)); |
| 314 | } |
| 315 | try { |
| 316 | varHandle.compareAndSet(instance, 6.0, 7.0); |
| 317 | } catch (RuntimeException e) { |
| 318 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 319 | System.out.println(varHandle.get(instance)); |
| 320 | } |
| 321 | |
| 322 | try { |
| 323 | varHandle.compareAndSet(instance, 6, "7"); |
| 324 | } catch (RuntimeException e) { |
| 325 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 326 | System.out.println(varHandle.get(instance)); |
| 327 | } |
| 328 | try { |
| 329 | varHandle.compareAndSet(instance, "6", 7); |
| 330 | } catch (RuntimeException e) { |
| 331 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 332 | System.out.println(varHandle.get(instance)); |
| 333 | } |
| 334 | try { |
| 335 | varHandle.compareAndSet(instance, "6", "7"); |
| 336 | } catch (RuntimeException e) { |
| 337 | checkJavaLangInvokeWrongMethodTypeException(e); |
| 338 | System.out.println(varHandle.get(instance)); |
| 339 | } |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
| 343 | VarHandle varHandle = |
| 344 | MethodHandles.lookup().findVarHandle(InstanceLongField.class, "field", long.class); |
Søren Gjesse | ca343fd | 2022-12-20 16:22:00 +0100 | [diff] [blame] | 345 | testGet(varHandle); |
Søren Gjesse | deab69d | 2023-01-02 15:25:36 +0100 | [diff] [blame] | 346 | testGetVolatile(varHandle); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 347 | testSet(varHandle); |
Søren Gjesse | 0a319f6 | 2023-01-02 15:25:47 +0100 | [diff] [blame^] | 348 | testSetVolatile(varHandle); |
Søren Gjesse | 6f80c1a | 2022-11-30 17:13:05 +0100 | [diff] [blame] | 349 | testCompareAndSet(varHandle); |
| 350 | } |
| 351 | } |