Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import java.lang.invoke.MethodHandle; |
| 17 | import java.lang.invoke.MethodHandles; |
| 18 | import java.lang.invoke.WrongMethodTypeException; |
| 19 | |
| 20 | public class Main { |
| 21 | |
| 22 | public static class ValueHolder { |
| 23 | public boolean m_z = false; |
| 24 | public byte m_b = 0; |
| 25 | public char m_c = 'a'; |
| 26 | public short m_s = 0; |
| 27 | public int m_i = 0; |
| 28 | public float m_f = 0.0f; |
| 29 | public double m_d = 0.0; |
| 30 | public long m_j = 0; |
| 31 | public String m_l = "a"; |
| 32 | |
| 33 | public static boolean s_z; |
| 34 | public static byte s_b; |
| 35 | public static char s_c; |
| 36 | public static short s_s; |
| 37 | public static int s_i; |
| 38 | public static float s_f; |
| 39 | public static double s_d; |
| 40 | public static long s_j; |
| 41 | public static String s_l; |
| 42 | |
| 43 | public final int m_fi = 0xa5a5a5a5; |
| 44 | public static final int s_fi = 0x5a5a5a5a; |
| 45 | } |
| 46 | |
| 47 | public static class InvokeExactTester { |
| 48 | private enum PrimitiveType { |
| 49 | Boolean, |
| 50 | Byte, |
| 51 | Char, |
| 52 | Short, |
| 53 | Int, |
| 54 | Long, |
| 55 | Float, |
| 56 | Double, |
| 57 | String, |
| 58 | } |
| 59 | |
| 60 | private enum AccessorType { |
| 61 | IPUT, |
| 62 | SPUT, |
| 63 | IGET, |
| 64 | SGET, |
| 65 | } |
| 66 | |
| 67 | private static void assertActualAndExpectedMatch(boolean actual, boolean expected) |
| 68 | throws AssertionError { |
| 69 | if (actual != expected) { |
| 70 | throw new AssertionError("Actual != Expected (" + actual + " != " + expected + ")"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private static void assertTrue(boolean value) throws AssertionError { |
| 75 | if (!value) { |
| 76 | throw new AssertionError("Value is not true"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void setByte(MethodHandle m, ValueHolder v, byte value, boolean expectFailure) |
| 81 | throws Throwable { |
| 82 | boolean exceptionThrown = false; |
| 83 | try { |
| 84 | if (v == null) { |
| 85 | m.invokeExact(value); |
| 86 | } |
| 87 | else { |
| 88 | m.invokeExact(v, value); |
| 89 | } |
| 90 | } |
| 91 | catch (WrongMethodTypeException e) { |
| 92 | exceptionThrown = true; |
| 93 | } |
| 94 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 95 | } |
| 96 | |
| 97 | static void setByte(MethodHandle m, byte value, boolean expectFailure) throws Throwable { |
| 98 | setByte(m, null, value, expectFailure); |
| 99 | } |
| 100 | |
| 101 | static void getByte(MethodHandle m, ValueHolder v, byte value, boolean expectFailure) |
| 102 | throws Throwable { |
| 103 | boolean exceptionThrown = false; |
| 104 | try { |
| 105 | final byte got; |
| 106 | if (v == null) { |
| 107 | got = (byte)m.invokeExact(); |
| 108 | } else { |
| 109 | got = (byte)m.invokeExact(v); |
| 110 | } |
| 111 | assertTrue(got == value); |
| 112 | } |
| 113 | catch (WrongMethodTypeException e) { |
| 114 | exceptionThrown = true; |
| 115 | } |
| 116 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 117 | } |
| 118 | |
| 119 | static void getByte(MethodHandle m, byte value, boolean expectFailure) throws Throwable { |
| 120 | getByte(m, null, value, expectFailure); |
| 121 | } |
| 122 | |
| 123 | static void setChar(MethodHandle m, ValueHolder v, char value, boolean expectFailure) |
| 124 | throws Throwable { |
| 125 | boolean exceptionThrown = false; |
| 126 | try { |
| 127 | if (v == null) { |
| 128 | m.invokeExact(value); |
| 129 | } |
| 130 | else { |
| 131 | m.invokeExact(v, value); |
| 132 | } |
| 133 | } |
| 134 | catch (WrongMethodTypeException e) { |
| 135 | exceptionThrown = true; |
| 136 | } |
| 137 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 138 | } |
| 139 | |
| 140 | static void setChar(MethodHandle m, char value, boolean expectFailure) throws Throwable { |
| 141 | setChar(m, null, value, expectFailure); |
| 142 | } |
| 143 | |
| 144 | static void getChar(MethodHandle m, ValueHolder v, char value, boolean expectFailure) |
| 145 | throws Throwable { |
| 146 | boolean exceptionThrown = false; |
| 147 | try { |
| 148 | final char got; |
| 149 | if (v == null) { |
| 150 | got = (char)m.invokeExact(); |
| 151 | } else { |
| 152 | got = (char)m.invokeExact(v); |
| 153 | } |
| 154 | assertTrue(got == value); |
| 155 | } |
| 156 | catch (WrongMethodTypeException e) { |
| 157 | exceptionThrown = true; |
| 158 | } |
| 159 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 160 | } |
| 161 | |
| 162 | static void getChar(MethodHandle m, char value, boolean expectFailure) throws Throwable { |
| 163 | getChar(m, null, value, expectFailure); |
| 164 | } |
| 165 | |
| 166 | static void setShort(MethodHandle m, ValueHolder v, short value, boolean expectFailure) |
| 167 | throws Throwable { |
| 168 | boolean exceptionThrown = false; |
| 169 | try { |
| 170 | if (v == null) { |
| 171 | m.invokeExact(value); |
| 172 | } |
| 173 | else { |
| 174 | m.invokeExact(v, value); |
| 175 | } |
| 176 | } |
| 177 | catch (WrongMethodTypeException e) { |
| 178 | exceptionThrown = true; |
| 179 | } |
| 180 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 181 | } |
| 182 | |
| 183 | static void setShort(MethodHandle m, short value, boolean expectFailure) throws Throwable { |
| 184 | setShort(m, null, value, expectFailure); |
| 185 | } |
| 186 | |
| 187 | static void getShort(MethodHandle m, ValueHolder v, short value, boolean expectFailure) |
| 188 | throws Throwable { |
| 189 | boolean exceptionThrown = false; |
| 190 | try { |
| 191 | final short got = (v == null) ? (short)m.invokeExact() : (short)m.invokeExact(v); |
| 192 | assertTrue(got == value); |
| 193 | } |
| 194 | catch (WrongMethodTypeException e) { |
| 195 | exceptionThrown = true; |
| 196 | } |
| 197 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 198 | } |
| 199 | |
| 200 | static void getShort(MethodHandle m, short value, boolean expectFailure) throws Throwable { |
| 201 | getShort(m, null, value, expectFailure); |
| 202 | } |
| 203 | |
| 204 | static void setInt(MethodHandle m, ValueHolder v, int value, boolean expectFailure) |
| 205 | throws Throwable { |
| 206 | boolean exceptionThrown = false; |
| 207 | try { |
| 208 | if (v == null) { |
| 209 | m.invokeExact(value); |
| 210 | } |
| 211 | else { |
| 212 | m.invokeExact(v, value); |
| 213 | } |
| 214 | } |
| 215 | catch (WrongMethodTypeException e) { |
| 216 | exceptionThrown = true; |
| 217 | } |
| 218 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 219 | } |
| 220 | |
| 221 | static void setInt(MethodHandle m, int value, boolean expectFailure) throws Throwable { |
| 222 | setInt(m, null, value, expectFailure); |
| 223 | } |
| 224 | |
| 225 | static void getInt(MethodHandle m, ValueHolder v, int value, boolean expectFailure) |
| 226 | throws Throwable { |
| 227 | boolean exceptionThrown = false; |
| 228 | try { |
| 229 | final int got = (v == null) ? (int)m.invokeExact() : (int)m.invokeExact(v); |
| 230 | assertTrue(got == value); |
| 231 | } |
| 232 | catch (WrongMethodTypeException e) { |
| 233 | exceptionThrown = true; |
| 234 | } |
| 235 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 236 | } |
| 237 | |
| 238 | static void getInt(MethodHandle m, int value, boolean expectFailure) throws Throwable { |
| 239 | getInt(m, null, value, expectFailure); |
| 240 | } |
| 241 | |
| 242 | static void setLong(MethodHandle m, ValueHolder v, long value, boolean expectFailure) |
| 243 | throws Throwable { |
| 244 | boolean exceptionThrown = false; |
| 245 | try { |
| 246 | if (v == null) { |
| 247 | m.invokeExact(value); |
| 248 | } |
| 249 | else { |
| 250 | m.invokeExact(v, value); |
| 251 | } |
| 252 | } |
| 253 | catch (WrongMethodTypeException e) { |
| 254 | exceptionThrown = true; |
| 255 | } |
| 256 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 257 | } |
| 258 | |
| 259 | static void setLong(MethodHandle m, long value, boolean expectFailure) throws Throwable { |
| 260 | setLong(m, null, value, expectFailure); |
| 261 | } |
| 262 | |
| 263 | static void getLong(MethodHandle m, ValueHolder v, long value, boolean expectFailure) |
| 264 | throws Throwable { |
| 265 | boolean exceptionThrown = false; |
| 266 | try { |
| 267 | final long got = (v == null) ? (long)m.invokeExact() : (long)m.invokeExact(v); |
| 268 | assertTrue(got == value); |
| 269 | } |
| 270 | catch (WrongMethodTypeException e) { |
| 271 | exceptionThrown = true; |
| 272 | } |
| 273 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 274 | } |
| 275 | |
| 276 | static void getLong(MethodHandle m, long value, boolean expectFailure) throws Throwable { |
| 277 | getLong(m, null, value, expectFailure); |
| 278 | } |
| 279 | |
| 280 | static void setFloat(MethodHandle m, ValueHolder v, float value, boolean expectFailure) |
| 281 | throws Throwable { |
| 282 | boolean exceptionThrown = false; |
| 283 | try { |
| 284 | if (v == null) { |
| 285 | m.invokeExact(value); |
| 286 | } |
| 287 | else { |
| 288 | m.invokeExact(v, value); |
| 289 | } |
| 290 | } |
| 291 | catch (WrongMethodTypeException e) { |
| 292 | exceptionThrown = true; |
| 293 | } |
| 294 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 295 | } |
| 296 | |
| 297 | static void setFloat(MethodHandle m, float value, boolean expectFailure) throws Throwable { |
| 298 | setFloat(m, null, value, expectFailure); |
| 299 | } |
| 300 | |
| 301 | static void getFloat(MethodHandle m, ValueHolder v, float value, boolean expectFailure) |
| 302 | throws Throwable { |
| 303 | boolean exceptionThrown = false; |
| 304 | try { |
| 305 | final float got = (v == null) ? (float)m.invokeExact() : (float)m.invokeExact(v); |
| 306 | assertTrue(got == value); |
| 307 | } |
| 308 | catch (WrongMethodTypeException e) { |
| 309 | exceptionThrown = true; |
| 310 | } |
| 311 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 312 | } |
| 313 | |
| 314 | static void getFloat(MethodHandle m, float value, boolean expectFailure) throws Throwable { |
| 315 | getFloat(m, null, value, expectFailure); |
| 316 | } |
| 317 | |
| 318 | static void setDouble(MethodHandle m, ValueHolder v, double value, boolean expectFailure) |
| 319 | throws Throwable { |
| 320 | boolean exceptionThrown = false; |
| 321 | try { |
| 322 | if (v == null) { |
| 323 | m.invokeExact(value); |
| 324 | } |
| 325 | else { |
| 326 | m.invokeExact(v, value); |
| 327 | } |
| 328 | } |
| 329 | catch (WrongMethodTypeException e) { |
| 330 | exceptionThrown = true; |
| 331 | } |
| 332 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 333 | } |
| 334 | |
| 335 | static void setDouble(MethodHandle m, double value, boolean expectFailure) |
| 336 | throws Throwable { |
| 337 | setDouble(m, null, value, expectFailure); |
| 338 | } |
| 339 | |
| 340 | static void getDouble(MethodHandle m, ValueHolder v, double value, boolean expectFailure) |
| 341 | throws Throwable { |
| 342 | boolean exceptionThrown = false; |
| 343 | try { |
| 344 | final double got = (v == null) ? (double)m.invokeExact() : (double)m.invokeExact(v); |
| 345 | assertTrue(got == value); |
| 346 | } |
| 347 | catch (WrongMethodTypeException e) { |
| 348 | exceptionThrown = true; |
| 349 | } |
| 350 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 351 | } |
| 352 | |
| 353 | static void getDouble(MethodHandle m, double value, boolean expectFailure) |
| 354 | throws Throwable { |
| 355 | getDouble(m, null, value, expectFailure); |
| 356 | } |
| 357 | |
| 358 | static void setString(MethodHandle m, ValueHolder v, String value, boolean expectFailure) |
| 359 | throws Throwable { |
| 360 | boolean exceptionThrown = false; |
| 361 | try { |
| 362 | if (v == null) { |
| 363 | m.invokeExact(value); |
| 364 | } |
| 365 | else { |
| 366 | m.invokeExact(v, value); |
| 367 | } |
| 368 | } |
| 369 | catch (WrongMethodTypeException e) { |
| 370 | exceptionThrown = true; |
| 371 | } |
| 372 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 373 | } |
| 374 | |
| 375 | static void setString(MethodHandle m, String value, boolean expectFailure) |
| 376 | throws Throwable { |
| 377 | setString(m, null, value, expectFailure); |
| 378 | } |
| 379 | |
| 380 | static void getString(MethodHandle m, ValueHolder v, String value, boolean expectFailure) |
| 381 | throws Throwable { |
| 382 | boolean exceptionThrown = false; |
| 383 | try { |
| 384 | final String got = (v == null) ? (String)m.invokeExact() : (String)m.invokeExact(v); |
| 385 | assertTrue(got.equals(value)); |
| 386 | } |
| 387 | catch (WrongMethodTypeException e) { |
| 388 | exceptionThrown = true; |
| 389 | } |
| 390 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 391 | } |
| 392 | |
| 393 | static void getString(MethodHandle m, String value, boolean expectFailure) |
| 394 | throws Throwable { |
| 395 | getString(m, null, value, expectFailure); |
| 396 | } |
| 397 | |
| 398 | static void setBoolean(MethodHandle m, ValueHolder v, boolean value, boolean expectFailure) |
| 399 | throws Throwable { |
| 400 | boolean exceptionThrown = false; |
| 401 | try { |
| 402 | if (v == null) { |
| 403 | m.invokeExact(value); |
| 404 | } |
| 405 | else { |
| 406 | m.invokeExact(v, value); |
| 407 | } |
| 408 | } |
| 409 | catch (WrongMethodTypeException e) { |
| 410 | exceptionThrown = true; |
| 411 | } |
| 412 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 413 | } |
| 414 | |
| 415 | static void setBoolean(MethodHandle m, boolean value, boolean expectFailure) |
| 416 | throws Throwable { |
| 417 | setBoolean(m, null, value, expectFailure); |
| 418 | } |
| 419 | |
| 420 | static void getBoolean(MethodHandle m, ValueHolder v, boolean value, boolean expectFailure) |
| 421 | throws Throwable { |
| 422 | boolean exceptionThrown = false; |
| 423 | try { |
| 424 | final boolean got = |
| 425 | (v == null) ? (boolean)m.invokeExact() : (boolean)m.invokeExact(v); |
| 426 | assertTrue(got == value); |
| 427 | } |
| 428 | catch (WrongMethodTypeException e) { |
| 429 | exceptionThrown = true; |
| 430 | } |
| 431 | assertActualAndExpectedMatch(exceptionThrown, expectFailure); |
| 432 | } |
| 433 | |
| 434 | static void getBoolean(MethodHandle m, boolean value, boolean expectFailure) |
| 435 | throws Throwable { |
| 436 | getBoolean(m, null, value, expectFailure); |
| 437 | } |
| 438 | |
| 439 | static boolean resultFor(PrimitiveType actualType, PrimitiveType expectedType, |
| 440 | AccessorType actualAccessor, |
| 441 | AccessorType expectedAccessor) { |
| 442 | return (actualType != expectedType) || (actualAccessor != expectedAccessor); |
| 443 | } |
| 444 | |
| 445 | static void tryAccessor(MethodHandle methodHandle, |
| 446 | ValueHolder valueHolder, |
| 447 | PrimitiveType primitive, |
| 448 | Object value, |
| 449 | AccessorType accessor) throws Throwable { |
| 450 | boolean booleanValue = |
| 451 | value instanceof Boolean ? ((Boolean)value).booleanValue() : false; |
| 452 | setBoolean(methodHandle, valueHolder, booleanValue, |
| 453 | resultFor(primitive, PrimitiveType.Boolean, accessor, AccessorType.IPUT)); |
| 454 | setBoolean(methodHandle, booleanValue, |
| 455 | resultFor(primitive, PrimitiveType.Boolean, accessor, AccessorType.SPUT)); |
| 456 | getBoolean(methodHandle, valueHolder, booleanValue, |
| 457 | resultFor(primitive, PrimitiveType.Boolean, accessor, AccessorType.IGET)); |
| 458 | getBoolean(methodHandle, booleanValue, |
| 459 | resultFor(primitive, PrimitiveType.Boolean, accessor, AccessorType.SGET)); |
| 460 | |
| 461 | byte byteValue = value instanceof Byte ? ((Byte)value).byteValue() : (byte)0; |
| 462 | setByte(methodHandle, valueHolder, byteValue, |
| 463 | resultFor(primitive, PrimitiveType.Byte, accessor, AccessorType.IPUT)); |
| 464 | setByte(methodHandle, byteValue, |
| 465 | resultFor(primitive, PrimitiveType.Byte, accessor, AccessorType.SPUT)); |
| 466 | getByte(methodHandle, valueHolder, byteValue, |
| 467 | resultFor(primitive, PrimitiveType.Byte, accessor, AccessorType.IGET)); |
| 468 | getByte(methodHandle, byteValue, |
| 469 | resultFor(primitive, PrimitiveType.Byte, accessor, AccessorType.SGET)); |
| 470 | |
| 471 | char charValue = value instanceof Character ? ((Character)value).charValue() : 'z'; |
| 472 | setChar(methodHandle, valueHolder, charValue, |
| 473 | resultFor(primitive, PrimitiveType.Char, accessor, AccessorType.IPUT)); |
| 474 | setChar(methodHandle, charValue, |
| 475 | resultFor(primitive, PrimitiveType.Char, accessor, AccessorType.SPUT)); |
| 476 | getChar(methodHandle, valueHolder, charValue, |
| 477 | resultFor(primitive, PrimitiveType.Char, accessor, AccessorType.IGET)); |
| 478 | getChar(methodHandle, charValue, |
| 479 | resultFor(primitive, PrimitiveType.Char, accessor, AccessorType.SGET)); |
| 480 | |
| 481 | short shortValue = value instanceof Short ? ((Short)value).shortValue() : (short)0; |
| 482 | setShort(methodHandle, valueHolder, shortValue, |
| 483 | resultFor(primitive, PrimitiveType.Short, accessor, AccessorType.IPUT)); |
| 484 | setShort(methodHandle, shortValue, |
| 485 | resultFor(primitive, PrimitiveType.Short, accessor, AccessorType.SPUT)); |
| 486 | getShort(methodHandle, valueHolder, shortValue, |
| 487 | resultFor(primitive, PrimitiveType.Short, accessor, AccessorType.IGET)); |
| 488 | getShort(methodHandle, shortValue, |
| 489 | resultFor(primitive, PrimitiveType.Short, accessor, AccessorType.SGET)); |
| 490 | |
| 491 | int intValue = value instanceof Integer ? ((Integer)value).intValue() : -1; |
| 492 | setInt(methodHandle, valueHolder, intValue, |
| 493 | resultFor(primitive, PrimitiveType.Int, accessor, AccessorType.IPUT)); |
| 494 | setInt(methodHandle, intValue, |
| 495 | resultFor(primitive, PrimitiveType.Int, accessor, AccessorType.SPUT)); |
| 496 | getInt(methodHandle, valueHolder, intValue, |
| 497 | resultFor(primitive, PrimitiveType.Int, accessor, AccessorType.IGET)); |
| 498 | getInt(methodHandle, intValue, |
| 499 | resultFor(primitive, PrimitiveType.Int, accessor, AccessorType.SGET)); |
| 500 | |
| 501 | long longValue = value instanceof Long ? ((Long)value).longValue() : (long)-1; |
| 502 | setLong(methodHandle, valueHolder, longValue, |
| 503 | resultFor(primitive, PrimitiveType.Long, accessor, AccessorType.IPUT)); |
| 504 | setLong(methodHandle, longValue, |
| 505 | resultFor(primitive, PrimitiveType.Long, accessor, AccessorType.SPUT)); |
| 506 | getLong(methodHandle, valueHolder, longValue, |
| 507 | resultFor(primitive, PrimitiveType.Long, accessor, AccessorType.IGET)); |
| 508 | getLong(methodHandle, longValue, |
| 509 | resultFor(primitive, PrimitiveType.Long, accessor, AccessorType.SGET)); |
| 510 | |
| 511 | float floatValue = value instanceof Float ? ((Float)value).floatValue() : -1.0f; |
| 512 | setFloat(methodHandle, valueHolder, floatValue, |
| 513 | resultFor(primitive, PrimitiveType.Float, accessor, AccessorType.IPUT)); |
| 514 | setFloat(methodHandle, floatValue, |
| 515 | resultFor(primitive, PrimitiveType.Float, accessor, AccessorType.SPUT)); |
| 516 | getFloat(methodHandle, valueHolder, floatValue, |
| 517 | resultFor(primitive, PrimitiveType.Float, accessor, AccessorType.IGET)); |
| 518 | getFloat(methodHandle, floatValue, |
| 519 | resultFor(primitive, PrimitiveType.Float, accessor, AccessorType.SGET)); |
| 520 | |
| 521 | double doubleValue = value instanceof Double ? ((Double)value).doubleValue() : -1.0; |
| 522 | setDouble(methodHandle, valueHolder, doubleValue, |
| 523 | resultFor(primitive, PrimitiveType.Double, accessor, AccessorType.IPUT)); |
| 524 | setDouble(methodHandle, doubleValue, |
| 525 | resultFor(primitive, PrimitiveType.Double, accessor, AccessorType.SPUT)); |
| 526 | getDouble(methodHandle, valueHolder, doubleValue, |
| 527 | resultFor(primitive, PrimitiveType.Double, accessor, AccessorType.IGET)); |
| 528 | getDouble(methodHandle, doubleValue, |
| 529 | resultFor(primitive, PrimitiveType.Double, accessor, AccessorType.SGET)); |
| 530 | |
| 531 | String stringValue = value instanceof String ? ((String) value) : "No Spock, no"; |
| 532 | setString(methodHandle, valueHolder, stringValue, |
| 533 | resultFor(primitive, PrimitiveType.String, accessor, AccessorType.IPUT)); |
| 534 | setString(methodHandle, stringValue, |
| 535 | resultFor(primitive, PrimitiveType.String, accessor, AccessorType.SPUT)); |
| 536 | getString(methodHandle, valueHolder, stringValue, |
| 537 | resultFor(primitive, PrimitiveType.String, accessor, AccessorType.IGET)); |
| 538 | getString(methodHandle, stringValue, |
| 539 | resultFor(primitive, PrimitiveType.String, accessor, AccessorType.SGET)); |
| 540 | } |
| 541 | |
| 542 | public static void main() throws Throwable { |
| 543 | ValueHolder valueHolder = new ValueHolder(); |
| 544 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 545 | |
| 546 | boolean [] booleans = { false, true, false }; |
| 547 | for (boolean b : booleans) { |
| 548 | Boolean boxed = new Boolean(b); |
| 549 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_z", boolean.class), |
| 550 | valueHolder, PrimitiveType.Boolean, boxed, AccessorType.IPUT); |
| 551 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_z", boolean.class), |
| 552 | valueHolder, PrimitiveType.Boolean, boxed, AccessorType.IGET); |
| 553 | assertTrue(valueHolder.m_z == b); |
| 554 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_z", boolean.class), |
| 555 | valueHolder, PrimitiveType.Boolean, boxed, AccessorType.SPUT); |
| 556 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_z", boolean.class), |
| 557 | valueHolder, PrimitiveType.Boolean, boxed, AccessorType.SGET); |
| 558 | assertTrue(ValueHolder.s_z == b); |
| 559 | } |
| 560 | |
| 561 | byte [] bytes = { (byte)0x73, (byte)0xfe }; |
| 562 | for (byte b : bytes) { |
| 563 | Byte boxed = new Byte(b); |
| 564 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_b", byte.class), |
| 565 | valueHolder, PrimitiveType.Byte, boxed, AccessorType.IPUT); |
| 566 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_b", byte.class), |
| 567 | valueHolder, PrimitiveType.Byte, boxed, AccessorType.IGET); |
| 568 | assertTrue(valueHolder.m_b == b); |
| 569 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_b", byte.class), |
| 570 | valueHolder, PrimitiveType.Byte, boxed, AccessorType.SPUT); |
| 571 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_b", byte.class), |
| 572 | valueHolder, PrimitiveType.Byte, boxed, AccessorType.SGET); |
| 573 | assertTrue(ValueHolder.s_b == b); |
| 574 | } |
| 575 | |
| 576 | char [] chars = { 'a', 'b', 'c' }; |
| 577 | for (char c : chars) { |
| 578 | Character boxed = new Character(c); |
| 579 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_c", char.class), |
| 580 | valueHolder, PrimitiveType.Char, boxed, AccessorType.IPUT); |
| 581 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_c", char.class), |
| 582 | valueHolder, PrimitiveType.Char, boxed, AccessorType.IGET); |
| 583 | assertTrue(valueHolder.m_c == c); |
| 584 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_c", char.class), |
| 585 | valueHolder, PrimitiveType.Char, boxed, AccessorType.SPUT); |
| 586 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_c", char.class), |
| 587 | valueHolder, PrimitiveType.Char, boxed, AccessorType.SGET); |
| 588 | assertTrue(ValueHolder.s_c == c); |
| 589 | } |
| 590 | |
| 591 | short [] shorts = { (short)0x1234, (short)0x4321 }; |
| 592 | for (short s : shorts) { |
| 593 | Short boxed = new Short(s); |
| 594 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_s", short.class), |
| 595 | valueHolder, PrimitiveType.Short, boxed, AccessorType.IPUT); |
| 596 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_s", short.class), |
| 597 | valueHolder, PrimitiveType.Short, boxed, AccessorType.IGET); |
| 598 | assertTrue(valueHolder.m_s == s); |
| 599 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_s", short.class), |
| 600 | valueHolder, PrimitiveType.Short, boxed, AccessorType.SPUT); |
| 601 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_s", short.class), |
| 602 | valueHolder, PrimitiveType.Short, boxed, AccessorType.SGET); |
| 603 | assertTrue(ValueHolder.s_s == s); |
| 604 | } |
| 605 | |
| 606 | int [] ints = { -100000000, 10000000 }; |
| 607 | for (int i : ints) { |
| 608 | Integer boxed = new Integer(i); |
| 609 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_i", int.class), |
| 610 | valueHolder, PrimitiveType.Int, boxed, AccessorType.IPUT); |
| 611 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_i", int.class), |
| 612 | valueHolder, PrimitiveType.Int, boxed, AccessorType.IGET); |
| 613 | assertTrue(valueHolder.m_i == i); |
| 614 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_i", int.class), |
| 615 | valueHolder, PrimitiveType.Int, boxed, AccessorType.SPUT); |
| 616 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_i", int.class), |
| 617 | valueHolder, PrimitiveType.Int, boxed, AccessorType.SGET); |
| 618 | assertTrue(ValueHolder.s_i == i); |
| 619 | } |
| 620 | |
| 621 | float [] floats = { 0.99f, -1.23e-17f }; |
| 622 | for (float f : floats) { |
| 623 | Float boxed = new Float(f); |
| 624 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_f", float.class), |
| 625 | valueHolder, PrimitiveType.Float, boxed, AccessorType.IPUT); |
| 626 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_f", float.class), |
| 627 | valueHolder, PrimitiveType.Float, boxed, AccessorType.IGET); |
| 628 | assertTrue(valueHolder.m_f == f); |
| 629 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_f", float.class), |
| 630 | valueHolder, PrimitiveType.Float, boxed, AccessorType.SPUT); |
| 631 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_f", float.class), |
| 632 | valueHolder, PrimitiveType.Float, boxed, AccessorType.SGET); |
| 633 | assertTrue(ValueHolder.s_f == f); |
| 634 | } |
| 635 | |
| 636 | double [] doubles = { 0.44444444444e37, -0.555555555e-37 }; |
| 637 | for (double d : doubles) { |
| 638 | Double boxed = new Double(d); |
| 639 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_d", double.class), |
| 640 | valueHolder, PrimitiveType.Double, boxed, AccessorType.IPUT); |
| 641 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_d", double.class), |
| 642 | valueHolder, PrimitiveType.Double, boxed, AccessorType.IGET); |
| 643 | assertTrue(valueHolder.m_d == d); |
| 644 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_d", double.class), |
| 645 | valueHolder, PrimitiveType.Double, boxed, AccessorType.SPUT); |
| 646 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_d", double.class), |
| 647 | valueHolder, PrimitiveType.Double, boxed, AccessorType.SGET); |
| 648 | assertTrue(ValueHolder.s_d == d); |
| 649 | } |
| 650 | |
| 651 | long [] longs = { 0x0123456789abcdefl, 0xfedcba9876543210l }; |
| 652 | for (long j : longs) { |
| 653 | Long boxed = new Long(j); |
| 654 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_j", long.class), |
| 655 | valueHolder, PrimitiveType.Long, boxed, AccessorType.IPUT); |
| 656 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_j", long.class), |
| 657 | valueHolder, PrimitiveType.Long, boxed, AccessorType.IGET); |
| 658 | assertTrue(valueHolder.m_j == j); |
| 659 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_j", long.class), |
| 660 | valueHolder, PrimitiveType.Long, boxed, AccessorType.SPUT); |
| 661 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_j", long.class), |
| 662 | valueHolder, PrimitiveType.Long, boxed, AccessorType.SGET); |
| 663 | assertTrue(ValueHolder.s_j == j); |
| 664 | } |
| 665 | |
| 666 | String [] strings = { "octopus", "crab" }; |
| 667 | for (String s : strings) { |
| 668 | tryAccessor(lookup.findSetter(ValueHolder.class, "m_l", String.class), |
| 669 | valueHolder, PrimitiveType.String, s, AccessorType.IPUT); |
| 670 | tryAccessor(lookup.findGetter(ValueHolder.class, "m_l", String.class), |
| 671 | valueHolder, PrimitiveType.String, s, AccessorType.IGET); |
| 672 | assertTrue(s.equals(valueHolder.m_l)); |
| 673 | tryAccessor(lookup.findStaticSetter(ValueHolder.class, "s_l", String.class), |
| 674 | valueHolder, PrimitiveType.String, s, AccessorType.SPUT); |
| 675 | tryAccessor(lookup.findStaticGetter(ValueHolder.class, "s_l", String.class), |
| 676 | valueHolder, PrimitiveType.String, s, AccessorType.SGET); |
| 677 | assertTrue(s.equals(ValueHolder.s_l)); |
| 678 | } |
| 679 | |
| 680 | System.out.println("Passed InvokeExact tests for accessors."); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | public static class FindAccessorTester { |
| 685 | public static void main() throws Throwable { |
| 686 | ValueHolder valueHolder = new ValueHolder(); |
| 687 | MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 688 | |
| 689 | lookup.findStaticGetter(ValueHolder.class, "s_fi", int.class); |
| 690 | try { |
| 691 | lookup.findStaticGetter(ValueHolder.class, "s_fi", byte.class); |
| 692 | unreachable(); |
| 693 | } catch (NoSuchFieldException e) {} |
| 694 | try { |
| 695 | lookup.findGetter(ValueHolder.class, "s_fi", byte.class); |
| 696 | unreachable(); |
| 697 | } catch (NoSuchFieldException e) {} |
| 698 | try { |
| 699 | lookup.findStaticSetter(ValueHolder.class, "s_fi", int.class); |
| 700 | unreachable(); |
| 701 | } catch (IllegalAccessException e) {} |
| 702 | |
| 703 | lookup.findGetter(ValueHolder.class, "m_fi", int.class); |
| 704 | try { |
| 705 | lookup.findGetter(ValueHolder.class, "m_fi", byte.class); |
| 706 | unreachable(); |
| 707 | } catch (NoSuchFieldException e) {} |
| 708 | try { |
| 709 | lookup.findStaticGetter(ValueHolder.class, "m_fi", byte.class); |
| 710 | unreachable(); |
| 711 | } catch (NoSuchFieldException e) {} |
| 712 | try { |
| 713 | lookup.findSetter(ValueHolder.class, "m_fi", int.class); |
| 714 | unreachable(); |
| 715 | } catch (IllegalAccessException e) {} |
| 716 | } |
| 717 | |
| 718 | public static void unreachable() throws Throwable{ |
| 719 | throw new Error("unreachable"); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | public static void main(String[] args) throws Throwable { |
| 724 | FindAccessorTester.main(); |
| 725 | InvokeExactTester.main(); |
| 726 | } |
| 727 | } |