1 /* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.lang.invoke; 26 27 /* 28 * @test TestStableLong 29 * @summary tests on stable fields and arrays 30 * @library /testlibrary 31 * @compile -XDignore.symbol.file TestStableLong.java 32 * @run main ClassFileInstaller 33 * java/lang/invoke/TestStableLong 34 * java/lang/invoke/TestStableLong$LongStable 35 * java/lang/invoke/TestStableLong$StaticLongStable 36 * java/lang/invoke/TestStableLong$VolatileLongStable 37 * java/lang/invoke/TestStableLong$LongArrayDim1 38 * java/lang/invoke/TestStableLong$LongArrayDim2 39 * java/lang/invoke/TestStableLong$LongArrayDim3 40 * java/lang/invoke/TestStableLong$LongArrayDim4 41 * java/lang/invoke/TestStableLong$ObjectArrayLowerDim0 42 * java/lang/invoke/TestStableLong$ObjectArrayLowerDim1 43 * java/lang/invoke/TestStableLong$NestedStableField 44 * java/lang/invoke/TestStableLong$NestedStableField$A 45 * java/lang/invoke/TestStableLong$NestedStableField1 46 * java/lang/invoke/TestStableLong$NestedStableField1$A 47 * java/lang/invoke/TestStableLong$NestedStableField2 48 * java/lang/invoke/TestStableLong$NestedStableField2$A 49 * java/lang/invoke/TestStableLong$NestedStableField3 50 * java/lang/invoke/TestStableLong$NestedStableField3$A 51 * java/lang/invoke/TestStableLong$DefaultValue 52 * java/lang/invoke/TestStableLong$ObjectArrayLowerDim2 53 * 54 * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions 55 * -XX:+FoldStableValues -XX:+UseCompressedOop 56 * -server -XX:-TieredCompilation -Xcomp 57 * -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 58 * java.lang.invoke.TestStableLong 59 * 60 * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions 61 * -XX:+FoldStableValues -XX:-UseCompressedOop 62 * -server -XX:-TieredCompilation -Xcomp 63 * -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 64 * java.lang.invoke.TestStableLong 65 * 66 * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions 67 * -XX:-FoldStableValues -XX:+UseCompressedOop 68 * -server -XX:-TieredCompilation -Xcomp 69 * -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 70 * java.lang.invoke.TestStableLong 71 * 72 * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions 73 * -XX:-FoldStableValues -XX:-UseCompressedOop 74 * -server -XX:-TieredCompilation -Xcomp 75 * -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 76 * java.lang.invoke.TestStableLong 77 */ 78 import com.sun.management.HotSpotDiagnosticMXBean; 79 import com.sun.management.VMOption; 80 import sun.management.ManagementFactoryHelper; 81 import java.lang.reflect.InvocationTargetException; 82 83 public class TestStableLong { 84 public static void main(String[] args) throws Exception { 85 System.out.println("@Stable enabled: "+isStableEnabled); 86 System.out.println(); 87 88 run(DefaultValue.class); 89 run(LongStable.class); 90 run(StaticLongStable.class); 91 run(VolatileLongStable.class); 92 93 // @Stable arrays: Dim 1-4 94 run(LongArrayDim1.class); 95 run(LongArrayDim2.class); 96 run(LongArrayDim3.class); 97 run(LongArrayDim4.class); 98 99 // @Stable Object field: dynamic arrays 100 run(ObjectArrayLowerDim0.class); 101 run(ObjectArrayLowerDim1.class); 102 run(ObjectArrayLowerDim2.class); 103 104 // Nested @Stable fields 105 run(NestedStableField.class); 106 run(NestedStableField1.class); 107 run(NestedStableField2.class); 108 run(NestedStableField3.class); 109 110 if (failed) { 111 throw new Error("TEST FAILED"); 112 } 113 } 114 115 /* ==================================================== */ 116 117 static class DefaultValue { 118 public @Stable long v; 119 120 public static final DefaultValue c = new DefaultValue(); 121 public static long get() { return c.v; } 122 public static void test() throws Exception { 123 long val1 = get(); 124 c.v = 1L; long val2 = get(); 125 assertEquals(val1, 0); 126 assertEquals(val2, 1L); 127 } 128 } 129 130 /* ==================================================== */ 131 132 static class LongStable { 133 public @Stable long v; 134 135 public static final LongStable c = new LongStable(); 136 public static long get() { return c.v; } 137 public static void test() throws Exception { 138 c.v = 5; long val1 = get(); 139 c.v = Long.MAX_VALUE; long val2 = get(); 140 assertEquals(val1, 5); 141 assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); 142 } 143 } 144 145 /* ==================================================== */ 146 147 static class StaticLongStable { 148 public static @Stable long v; 149 150 public static final StaticLongStable c = new StaticLongStable(); 151 public static long get() { return c.v; } 152 public static void test() throws Exception { 153 c.v = 5; long val1 = get(); 154 c.v = Long.MAX_VALUE; long val2 = get(); 155 assertEquals(val1, 5); 156 assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); 157 } 158 } 159 160 /* ==================================================== */ 161 162 static class VolatileLongStable { 163 public @Stable volatile long v; 164 165 public static final VolatileLongStable c = new VolatileLongStable(); 166 public static long get() { return c.v; } 167 public static void test() throws Exception { 168 c.v = 5; long val1 = get(); 169 c.v = Long.MAX_VALUE; long val2 = get(); 170 assertEquals(val1, 5); 171 assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); 172 } 173 } 174 175 /* ==================================================== */ 176 // @Stable array == field && all components are stable 177 178 static class LongArrayDim1 { 179 public @Stable long[] v; 180 181 public static final LongArrayDim1 c = new LongArrayDim1(); 182 public static long get() { return c.v[0]; } 183 public static long get1() { return c.v[10]; } 184 public static long[] get2() { return c.v; } 185 public static void test() throws Exception { 186 { 187 c.v = new long[1]; c.v[0] = 1; long val1 = get(); 188 c.v[0] = 2; long val2 = get(); 189 assertEquals(val1, 1); 190 assertEquals(val2, (isStableEnabled ? 1 : 2)); 191 192 c.v = new long[1]; c.v[0] = 3; long val3 = get(); 193 assertEquals(val3, (isStableEnabled ? 1 : 3)); 194 } 195 196 { 197 c.v = new long[20]; c.v[10] = 1; long val1 = get1(); 198 c.v[10] = 2; long val2 = get1(); 199 assertEquals(val1, 1); 200 assertEquals(val2, (isStableEnabled ? 1 : 2)); 201 202 c.v = new long[20]; c.v[10] = 3; long val3 = get1(); 203 assertEquals(val3, (isStableEnabled ? 1 : 3)); 204 } 205 206 { 207 c.v = new long[1]; long[] val1 = get2(); 208 c.v = new long[1]; long[] val2 = get2(); 209 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 210 } 211 } 212 } 213 214 /* ==================================================== */ 215 216 static class LongArrayDim2 { 217 public @Stable long[][] v; 218 219 public static final LongArrayDim2 c = new LongArrayDim2(); 220 public static long get() { return c.v[0][0]; } 221 public static long[] get1() { return c.v[0]; } 222 public static long[][] get2() { return c.v; } 223 public static void test() throws Exception { 224 { 225 c.v = new long[1][1]; c.v[0][0] = 1; long val1 = get(); 226 c.v[0][0] = 2; long val2 = get(); 227 assertEquals(val1, 1); 228 assertEquals(val2, (isStableEnabled ? 1 : 2)); 229 230 c.v = new long[1][1]; c.v[0][0] = 3; long val3 = get(); 231 assertEquals(val3, (isStableEnabled ? 1 : 3)); 232 233 c.v[0] = new long[1]; c.v[0][0] = 4; long val4 = get(); 234 assertEquals(val4, (isStableEnabled ? 1 : 4)); 235 } 236 237 { 238 c.v = new long[1][1]; long[] val1 = get1(); 239 c.v[0] = new long[1]; long[] val2 = get1(); 240 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 241 } 242 243 { 244 c.v = new long[1][1]; long[][] val1 = get2(); 245 c.v = new long[1][1]; long[][] val2 = get2(); 246 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 247 } 248 } 249 } 250 251 /* ==================================================== */ 252 253 static class LongArrayDim3 { 254 public @Stable long[][][] v; 255 256 public static final LongArrayDim3 c = new LongArrayDim3(); 257 public static long get() { return c.v[0][0][0]; } 258 public static long[] get1() { return c.v[0][0]; } 259 public static long[][] get2() { return c.v[0]; } 260 public static long[][][] get3() { return c.v; } 261 public static void test() throws Exception { 262 { 263 c.v = new long[1][1][1]; c.v[0][0][0] = 1; long val1 = get(); 264 c.v[0][0][0] = 2; long val2 = get(); 265 assertEquals(val1, 1); 266 assertEquals(val2, (isStableEnabled ? 1 : 2)); 267 268 c.v = new long[1][1][1]; c.v[0][0][0] = 3; long val3 = get(); 269 assertEquals(val3, (isStableEnabled ? 1 : 3)); 270 271 c.v[0] = new long[1][1]; c.v[0][0][0] = 4; long val4 = get(); 272 assertEquals(val4, (isStableEnabled ? 1 : 4)); 273 274 c.v[0][0] = new long[1]; c.v[0][0][0] = 5; long val5 = get(); 275 assertEquals(val5, (isStableEnabled ? 1 : 5)); 276 } 277 278 { 279 c.v = new long[1][1][1]; long[] val1 = get1(); 280 c.v[0][0] = new long[1]; long[] val2 = get1(); 281 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 282 } 283 284 { 285 c.v = new long[1][1][1]; long[][] val1 = get2(); 286 c.v[0] = new long[1][1]; long[][] val2 = get2(); 287 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 288 } 289 290 { 291 c.v = new long[1][1][1]; long[][][] val1 = get3(); 292 c.v = new long[1][1][1]; long[][][] val2 = get3(); 293 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 294 } 295 } 296 } 297 298 /* ==================================================== */ 299 300 static class LongArrayDim4 { 301 public @Stable long[][][][] v; 302 303 public static final LongArrayDim4 c = new LongArrayDim4(); 304 public static long get() { return c.v[0][0][0][0]; } 305 public static long[] get1() { return c.v[0][0][0]; } 306 public static long[][] get2() { return c.v[0][0]; } 307 public static long[][][] get3() { return c.v[0]; } 308 public static long[][][][] get4() { return c.v; } 309 public static void test() throws Exception { 310 { 311 c.v = new long[1][1][1][1]; c.v[0][0][0][0] = 1; long val1 = get(); 312 c.v[0][0][0][0] = 2; long val2 = get(); 313 assertEquals(val1, 1); 314 assertEquals(val2, (isStableEnabled ? 1 : 2)); 315 316 c.v = new long[1][1][1][1]; c.v[0][0][0][0] = 3; long val3 = get(); 317 assertEquals(val3, (isStableEnabled ? 1 : 3)); 318 319 c.v[0] = new long[1][1][1]; c.v[0][0][0][0] = 4; long val4 = get(); 320 assertEquals(val4, (isStableEnabled ? 1 : 4)); 321 322 c.v[0][0] = new long[1][1]; c.v[0][0][0][0] = 5; long val5 = get(); 323 assertEquals(val5, (isStableEnabled ? 1 : 5)); 324 325 c.v[0][0][0] = new long[1]; c.v[0][0][0][0] = 6; long val6 = get(); 326 assertEquals(val6, (isStableEnabled ? 1 : 6)); 327 } 328 329 { 330 c.v = new long[1][1][1][1]; long[] val1 = get1(); 331 c.v[0][0][0] = new long[1]; long[] val2 = get1(); 332 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 333 } 334 335 { 336 c.v = new long[1][1][1][1]; long[][] val1 = get2(); 337 c.v[0][0] = new long[1][1]; long[][] val2 = get2(); 338 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 339 } 340 341 { 342 c.v = new long[1][1][1][1]; long[][][] val1 = get3(); 343 c.v[0] = new long[1][1][1]; long[][][] val2 = get3(); 344 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 345 } 346 347 { 348 c.v = new long[1][1][1][1]; long[][][][] val1 = get4(); 349 c.v = new long[1][1][1][1]; long[][][][] val2 = get4(); 350 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 351 } 352 353 } 354 } 355 356 /* ==================================================== */ 357 // Dynamic Dim is higher than static 358 359 static class ObjectArrayLowerDim0 { 360 public @Stable Object v; 361 362 public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0(); 363 public static long get() { return ((long[])c.v)[0]; } 364 public static long[] get1() { return (long[])c.v; } 365 366 public static void test() throws Exception { 367 { 368 c.v = new long[1]; ((long[])c.v)[0] = 1; long val1 = get(); 369 ((long[])c.v)[0] = 2; long val2 = get(); 370 371 assertEquals(val1, 1); 372 assertEquals(val2, 2); 373 } 374 375 { 376 c.v = new long[1]; long[] val1 = get1(); 377 c.v = new long[1]; long[] val2 = get1(); 378 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 379 } 380 } 381 } 382 383 /* ==================================================== */ 384 385 static class ObjectArrayLowerDim1 { 386 public @Stable Object[] v; 387 388 public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1(); 389 public static long get() { return ((long[][])c.v)[0][0]; } 390 public static long[] get1() { return (long[])(c.v[0]); } 391 public static Object[] get2() { return c.v; } 392 393 public static void test() throws Exception { 394 { 395 c.v = new long[1][1]; ((long[][])c.v)[0][0] = 1; long val1 = get(); 396 ((long[][])c.v)[0][0] = 2; long val2 = get(); 397 398 assertEquals(val1, 1); 399 assertEquals(val2, 2); 400 } 401 402 { 403 c.v = new long[1][1]; c.v[0] = new long[0]; long[] val1 = get1(); 404 c.v[0] = new long[0]; long[] val2 = get1(); 405 406 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 407 } 408 409 { 410 c.v = new long[0][0]; Object[] val1 = get2(); 411 c.v = new long[0][0]; Object[] val2 = get2(); 412 413 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 414 } 415 } 416 } 417 418 /* ==================================================== */ 419 420 static class ObjectArrayLowerDim2 { 421 public @Stable Object[][] v; 422 423 public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2(); 424 public static long get() { return ((long[][][])c.v)[0][0][0]; } 425 public static long[] get1() { return (long[])(c.v[0][0]); } 426 public static long[][] get2() { return (long[][])(c.v[0]); } 427 public static Object[][] get3() { return c.v; } 428 429 public static void test() throws Exception { 430 { 431 c.v = new long[1][1][1]; ((long[][][])c.v)[0][0][0] = 1L; long val1 = get(); 432 ((long[][][])c.v)[0][0][0] = 2L; long val2 = get(); 433 434 assertEquals(val1, 1L); 435 assertEquals(val2, 2L); 436 } 437 438 { 439 c.v = new long[1][1][1]; c.v[0][0] = new long[0]; long[] val1 = get1(); 440 c.v[0][0] = new long[0]; long[] val2 = get1(); 441 442 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 443 } 444 445 { 446 c.v = new long[1][1][1]; c.v[0] = new long[0][0]; long[][] val1 = get2(); 447 c.v[0] = new long[0][0]; long[][] val2 = get2(); 448 449 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 450 } 451 452 { 453 c.v = new long[0][0][0]; Object[][] val1 = get3(); 454 c.v = new long[0][0][0]; Object[][] val2 = get3(); 455 456 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 457 } 458 } 459 } 460 461 /* ==================================================== */ 462 463 static class NestedStableField { 464 static class A { 465 public @Stable long a; 466 467 } 468 public @Stable A v; 469 470 public static final NestedStableField c = new NestedStableField(); 471 public static A get() { return c.v; } 472 public static long get1() { return get().a; } 473 474 public static void test() throws Exception { 475 { 476 c.v = new A(); c.v.a = 1; A val1 = get(); 477 c.v.a = 2; A val2 = get(); 478 479 assertEquals(val1.a, 2); 480 assertEquals(val2.a, 2); 481 } 482 483 { 484 c.v = new A(); c.v.a = 1; long val1 = get1(); 485 c.v.a = 2; long val2 = get1(); 486 c.v = new A(); c.v.a = 3; long val3 = get1(); 487 488 assertEquals(val1, 1); 489 assertEquals(val2, (isStableEnabled ? 1 : 2)); 490 assertEquals(val3, (isStableEnabled ? 1 : 3)); 491 } 492 } 493 } 494 495 /* ==================================================== */ 496 497 static class NestedStableField1 { 498 static class A { 499 public @Stable long a; 500 public @Stable A next; 501 } 502 public @Stable A v; 503 504 public static final NestedStableField1 c = new NestedStableField1(); 505 public static A get() { return c.v.next.next.next.next.next.next.next; } 506 public static long get1() { return get().a; } 507 508 public static void test() throws Exception { 509 { 510 c.v = new A(); c.v.next = new A(); c.v.next.next = c.v; 511 c.v.a = 1; c.v.next.a = 1; A val1 = get(); 512 c.v.a = 2; c.v.next.a = 2; A val2 = get(); 513 514 assertEquals(val1.a, 2); 515 assertEquals(val2.a, 2); 516 } 517 518 { 519 c.v = new A(); c.v.next = c.v; 520 c.v.a = 1; long val1 = get1(); 521 c.v.a = 2; long val2 = get1(); 522 c.v = new A(); c.v.next = c.v; 523 c.v.a = 3; long val3 = get1(); 524 525 assertEquals(val1, 1); 526 assertEquals(val2, (isStableEnabled ? 1 : 2)); 527 assertEquals(val3, (isStableEnabled ? 1 : 3)); 528 } 529 } 530 } 531 /* ==================================================== */ 532 533 static class NestedStableField2 { 534 static class A { 535 public @Stable long a; 536 public @Stable A left; 537 public A right; 538 } 539 540 public @Stable A v; 541 542 public static final NestedStableField2 c = new NestedStableField2(); 543 public static long get() { return c.v.left.left.left.a; } 544 public static long get1() { return c.v.left.left.right.left.a; } 545 546 public static void test() throws Exception { 547 { 548 c.v = new A(); c.v.left = c.v.right = c.v; 549 c.v.a = 1; long val1 = get(); long val2 = get1(); 550 c.v.a = 2; long val3 = get(); long val4 = get1(); 551 552 assertEquals(val1, 1); 553 assertEquals(val3, (isStableEnabled ? 1 : 2)); 554 555 assertEquals(val2, 1); 556 assertEquals(val4, 2); 557 } 558 } 559 } 560 561 /* ==================================================== */ 562 563 static class NestedStableField3 { 564 static class A { 565 public @Stable long a; 566 public @Stable A[] left; 567 public A[] right; 568 } 569 570 public @Stable A[] v; 571 572 public static final NestedStableField3 c = new NestedStableField3(); 573 public static long get() { return c.v[0].left[1].left[0].left[1].a; } 574 public static long get1() { return c.v[1].left[0].left[1].right[0].left[1].a; } 575 576 public static void test() throws Exception { 577 { 578 A elem = new A(); 579 c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v; 580 elem.a = 1; long val1 = get(); long val2 = get1(); 581 elem.a = 2; long val3 = get(); long val4 = get1(); 582 583 assertEquals(val1, 1); 584 assertEquals(val3, (isStableEnabled ? 1 : 2)); 585 586 assertEquals(val2, 1); 587 assertEquals(val4, 2); 588 } 589 } 590 } 591 592 /* ==================================================== */ 593 // Auxiliary methods 594 static void assertEquals(long i, long j) { if (i != j) throw new AssertionError(i + " != " + j); } 595 static void assertTrue(boolean b) { if (!b) throw new AssertionError(); } 596 597 static boolean failed = false; 598 599 public static void run(Class<?> test) { 600 Throwable ex = null; 601 System.out.print(test.getName()+": "); 602 try { 603 test.getMethod("test").invoke(null); 604 } catch (InvocationTargetException e) { 605 ex = e.getCause(); 606 } catch (Throwable e) { 607 ex = e; 608 } finally { 609 if (ex == null) { 610 System.out.println("PASSED"); 611 } else { 612 failed = true; 613 System.out.println("FAILED"); 614 ex.printStackTrace(System.out); 615 } 616 } 617 } 618 619 static final boolean isStableEnabled; 620 static { 621 HotSpotDiagnosticMXBean diagnostic 622 = ManagementFactoryHelper.getDiagnosticMXBean(); 623 VMOption tmp; 624 try { 625 tmp = diagnostic.getVMOption("FoldStableValues"); 626 } catch (IllegalArgumentException e) { 627 tmp = null; 628 } 629 isStableEnabled = (tmp == null ? false : Boolean.parseBoolean(tmp.getValue())); 630 } 631 }