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 TestStableChar 29 * @summary tests on stable fields and arrays 30 * @library /testlibrary 31 * @compile -XDignore.symbol.file TestStableChar.java 32 * @run main ClassFileInstaller 33 * java/lang/invoke/TestStableChar 34 * java/lang/invoke/TestStableChar$CharStable 35 * java/lang/invoke/TestStableChar$StaticCharStable 36 * java/lang/invoke/TestStableChar$VolatileCharStable 37 * java/lang/invoke/TestStableChar$CharArrayDim1 38 * java/lang/invoke/TestStableChar$CharArrayDim2 39 * java/lang/invoke/TestStableChar$CharArrayDim3 40 * java/lang/invoke/TestStableChar$CharArrayDim4 41 * java/lang/invoke/TestStableChar$ObjectArrayLowerDim0 42 * java/lang/invoke/TestStableChar$ObjectArrayLowerDim1 43 * java/lang/invoke/TestStableChar$NestedStableField 44 * java/lang/invoke/TestStableChar$NestedStableField$A 45 * java/lang/invoke/TestStableChar$NestedStableField1 46 * java/lang/invoke/TestStableChar$NestedStableField1$A 47 * java/lang/invoke/TestStableChar$NestedStableField2 48 * java/lang/invoke/TestStableChar$NestedStableField2$A 49 * java/lang/invoke/TestStableChar$NestedStableField3 50 * java/lang/invoke/TestStableChar$NestedStableField3$A 51 * java/lang/invoke/TestStableChar$DefaultValue 52 * java/lang/invoke/TestStableChar$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.TestStableChar 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.TestStableChar 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.TestStableChar 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.TestStableChar 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 TestStableChar { 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(CharStable.class); 90 run(StaticCharStable.class); 91 run(VolatileCharStable.class); 92 93 // @Stable arrays: Dim 1-4 94 run(CharArrayDim1.class); 95 run(CharArrayDim2.class); 96 run(CharArrayDim3.class); 97 run(CharArrayDim4.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 char v; 119 120 public static final DefaultValue c = new DefaultValue(); 121 public static char get() { return c.v; } 122 public static void test() throws Exception { 123 char val1 = get(); 124 c.v = 'a'; char val2 = get(); 125 assertEquals(val1, 0); 126 assertEquals(val2, 'a'); 127 } 128 } 129 130 /* ==================================================== */ 131 132 static class CharStable { 133 public @Stable char v; 134 135 public static final CharStable c = new CharStable(); 136 public static char get() { return c.v; } 137 public static void test() throws Exception { 138 c.v = 'a'; char val1 = get(); 139 c.v = 'b'; char val2 = get(); 140 assertEquals(val1, 'a'); 141 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 142 } 143 } 144 145 /* ==================================================== */ 146 147 static class StaticCharStable { 148 public @Stable char v; 149 150 public static final StaticCharStable c = new StaticCharStable(); 151 public static char get() { return c.v; } 152 public static void test() throws Exception { 153 c.v = 'a'; char val1 = get(); 154 c.v = 'b'; char val2 = get(); 155 assertEquals(val1, 'a'); 156 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 157 } 158 } 159 160 /* ==================================================== */ 161 162 static class VolatileCharStable { 163 public @Stable volatile char v; 164 165 public static final VolatileCharStable c = new VolatileCharStable(); 166 public static char get() { return c.v; } 167 public static void test() throws Exception { 168 c.v = 'a'; char val1 = get(); 169 c.v = 'b'; char val2 = get(); 170 assertEquals(val1, 'a'); 171 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 172 } 173 } 174 175 /* ==================================================== */ 176 // @Stable array == field && all components are stable 177 178 static class CharArrayDim1 { 179 public @Stable char[] v; 180 181 public static final CharArrayDim1 c = new CharArrayDim1(); 182 public static char get() { return c.v[0]; } 183 public static char get1() { return c.v[10]; } 184 public static char[] get2() { return c.v; } 185 public static void test() throws Exception { 186 { 187 c.v = new char[1]; c.v[0] = 'a'; char val1 = get(); 188 c.v[0] = 'b'; char val2 = get(); 189 assertEquals(val1, 'a'); 190 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 191 192 c.v = new char[1]; c.v[0] = 'c'; char val3 = get(); 193 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 194 } 195 196 { 197 c.v = new char[20]; c.v[10] = 'a'; char val1 = get1(); 198 c.v[10] = 'b'; char val2 = get1(); 199 assertEquals(val1, 'a'); 200 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 201 202 c.v = new char[20]; c.v[10] = 'c'; char val3 = get1(); 203 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 204 } 205 206 { 207 c.v = new char[1]; char[] val1 = get2(); 208 c.v = new char[1]; char[] val2 = get2(); 209 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 210 } 211 } 212 } 213 214 /* ==================================================== */ 215 216 static class CharArrayDim2 { 217 public @Stable char[][] v; 218 219 public static final CharArrayDim2 c = new CharArrayDim2(); 220 public static char get() { return c.v[0][0]; } 221 public static char[] get1() { return c.v[0]; } 222 public static char[][] get2() { return c.v; } 223 public static void test() throws Exception { 224 { 225 c.v = new char[1][1]; c.v[0][0] = 'a'; char val1 = get(); 226 c.v[0][0] = 'b'; char val2 = get(); 227 assertEquals(val1, 'a'); 228 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 229 230 c.v = new char[1][1]; c.v[0][0] = 'c'; char val3 = get(); 231 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 232 233 c.v[0] = new char[1]; c.v[0][0] = 'd'; char val4 = get(); 234 assertEquals(val4, (isStableEnabled ? 'a' : 'd')); 235 } 236 237 { 238 c.v = new char[1][1]; char[] val1 = get1(); 239 c.v[0] = new char[1]; char[] val2 = get1(); 240 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 241 } 242 243 { 244 c.v = new char[1][1]; char[][] val1 = get2(); 245 c.v = new char[1][1]; char[][] val2 = get2(); 246 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 247 } 248 } 249 } 250 251 /* ==================================================== */ 252 253 static class CharArrayDim3 { 254 public @Stable char[][][] v; 255 256 public static final CharArrayDim3 c = new CharArrayDim3(); 257 public static char get() { return c.v[0][0][0]; } 258 public static char[] get1() { return c.v[0][0]; } 259 public static char[][] get2() { return c.v[0]; } 260 public static char[][][] get3() { return c.v; } 261 public static void test() throws Exception { 262 { 263 c.v = new char[1][1][1]; c.v[0][0][0] = 'a'; char val1 = get(); 264 c.v[0][0][0] = 'b'; char val2 = get(); 265 assertEquals(val1, 'a'); 266 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 267 268 c.v = new char[1][1][1]; c.v[0][0][0] = 'c'; char val3 = get(); 269 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 270 271 c.v[0] = new char[1][1]; c.v[0][0][0] = 'd'; char val4 = get(); 272 assertEquals(val4, (isStableEnabled ? 'a' : 'd')); 273 274 c.v[0][0] = new char[1]; c.v[0][0][0] = 'e'; char val5 = get(); 275 assertEquals(val5, (isStableEnabled ? 'a' : 'e')); 276 } 277 278 { 279 c.v = new char[1][1][1]; char[] val1 = get1(); 280 c.v[0][0] = new char[1]; char[] val2 = get1(); 281 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 282 } 283 284 { 285 c.v = new char[1][1][1]; char[][] val1 = get2(); 286 c.v[0] = new char[1][1]; char[][] val2 = get2(); 287 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 288 } 289 290 { 291 c.v = new char[1][1][1]; char[][][] val1 = get3(); 292 c.v = new char[1][1][1]; char[][][] val2 = get3(); 293 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 294 } 295 } 296 } 297 298 /* ==================================================== */ 299 300 static class CharArrayDim4 { 301 public @Stable char[][][][] v; 302 303 public static final CharArrayDim4 c = new CharArrayDim4(); 304 public static char get() { return c.v[0][0][0][0]; } 305 public static char[] get1() { return c.v[0][0][0]; } 306 public static char[][] get2() { return c.v[0][0]; } 307 public static char[][][] get3() { return c.v[0]; } 308 public static char[][][][] get4() { return c.v; } 309 public static void test() throws Exception { 310 { 311 c.v = new char[1][1][1][1]; c.v[0][0][0][0] = 'a'; char val1 = get(); 312 c.v[0][0][0][0] = 'b'; char val2 = get(); 313 assertEquals(val1, 'a'); 314 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 315 316 c.v = new char[1][1][1][1]; c.v[0][0][0][0] = 'c'; char val3 = get(); 317 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 318 319 c.v[0] = new char[1][1][1]; c.v[0][0][0][0] = 'd'; char val4 = get(); 320 assertEquals(val4, (isStableEnabled ? 'a' : 'd')); 321 322 c.v[0][0] = new char[1][1]; c.v[0][0][0][0] = 'e'; char val5 = get(); 323 assertEquals(val5, (isStableEnabled ? 'a' : 'e')); 324 325 c.v[0][0][0] = new char[1]; c.v[0][0][0][0] = 'f'; char val6 = get(); 326 assertEquals(val6, (isStableEnabled ? 'a' : 'f')); 327 } 328 329 { 330 c.v = new char[1][1][1][1]; char[] val1 = get1(); 331 c.v[0][0][0] = new char[1]; char[] val2 = get1(); 332 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 333 } 334 335 { 336 c.v = new char[1][1][1][1]; char[][] val1 = get2(); 337 c.v[0][0] = new char[1][1]; char[][] val2 = get2(); 338 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 339 } 340 341 { 342 c.v = new char[1][1][1][1]; char[][][] val1 = get3(); 343 c.v[0] = new char[1][1][1]; char[][][] val2 = get3(); 344 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 345 } 346 347 { 348 c.v = new char[1][1][1][1]; char[][][][] val1 = get4(); 349 c.v = new char[1][1][1][1]; char[][][][] val2 = get4(); 350 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 351 } 352 353 } 354 } 355 356 /* ==================================================== */ 357 // Dynamic Dim is higher than static 358 static class ObjectArrayLowerDim0 { 359 public @Stable Object v; 360 361 public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0(); 362 public static char get() { return ((char[])c.v)[0]; } 363 public static char[] get1() { return (char[])c.v; } 364 365 public static void test() throws Exception { 366 { 367 c.v = new char[1]; ((char[])c.v)[0] = 'a'; char val1 = get(); 368 ((char[])c.v)[0] = 'b'; char val2 = get(); 369 370 assertEquals(val1, 'a'); 371 assertEquals(val2, 'b'); 372 } 373 374 { 375 c.v = new char[1]; char[] val1 = get1(); 376 c.v = new char[1]; char[] val2 = get1(); 377 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 378 } 379 } 380 } 381 382 /* ==================================================== */ 383 384 static class ObjectArrayLowerDim1 { 385 public @Stable Object[] v; 386 387 public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1(); 388 public static char get() { return ((char[][])c.v)[0][0]; } 389 public static char[] get1() { return (char[])(c.v[0]); } 390 public static Object[] get2() { return c.v; } 391 392 public static void test() throws Exception { 393 { 394 c.v = new char[1][1]; ((char[][])c.v)[0][0] = 'a'; char val1 = get(); 395 ((char[][])c.v)[0][0] = 'b'; char val2 = get(); 396 397 assertEquals(val1, 'a'); 398 assertEquals(val2, 'b'); 399 } 400 401 { 402 c.v = new char[1][1]; c.v[0] = new char[0]; char[] val1 = get1(); 403 c.v[0] = new char[0]; char[] val2 = get1(); 404 405 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 406 } 407 408 { 409 c.v = new char[0][0]; Object[] val1 = get2(); 410 c.v = new char[0][0]; Object[] val2 = get2(); 411 412 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 413 } 414 } 415 } 416 417 /* ==================================================== */ 418 419 static class ObjectArrayLowerDim2 { 420 public @Stable Object[][] v; 421 422 public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2(); 423 public static char get() { return ((char[][][])c.v)[0][0][0]; } 424 public static char[] get1() { return (char[])(c.v[0][0]); } 425 public static char[][] get2() { return (char[][])(c.v[0]); } 426 public static Object[][] get3() { return c.v; } 427 428 public static void test() throws Exception { 429 { 430 c.v = new char[1][1][1]; ((char[][][])c.v)[0][0][0] = 'a'; char val1 = get(); 431 ((char[][][])c.v)[0][0][0] = 'b'; char val2 = get(); 432 433 assertEquals(val1, 'a'); 434 assertEquals(val2, 'b'); 435 } 436 437 { 438 c.v = new char[1][1][1]; c.v[0][0] = new char[0]; char[] val1 = get1(); 439 c.v[0][0] = new char[0]; char[] val2 = get1(); 440 441 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 442 } 443 444 { 445 c.v = new char[1][1][1]; c.v[0] = new char[0][0]; char[][] val1 = get2(); 446 c.v[0] = new char[0][0]; char[][] val2 = get2(); 447 448 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 449 } 450 451 { 452 c.v = new char[0][0][0]; Object[][] val1 = get3(); 453 c.v = new char[0][0][0]; Object[][] val2 = get3(); 454 455 assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); 456 } 457 } 458 } 459 460 /* ==================================================== */ 461 462 static class NestedStableField { 463 static class A { 464 public @Stable char a; 465 466 } 467 public @Stable A v; 468 469 public static final NestedStableField c = new NestedStableField(); 470 public static A get() { return c.v; } 471 public static char get1() { return get().a; } 472 473 public static void test() throws Exception { 474 { 475 c.v = new A(); c.v.a = 'a'; A val1 = get(); 476 c.v.a = 'b'; A val2 = get(); 477 478 assertEquals(val1.a, 'b'); 479 assertEquals(val2.a, 'b'); 480 } 481 482 { 483 c.v = new A(); c.v.a = 'a'; char val1 = get1(); 484 c.v.a = 'b'; char val2 = get1(); 485 c.v = new A(); c.v.a = 'c'; char val3 = get1(); 486 487 assertEquals(val1, 'a'); 488 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 489 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 490 } 491 } 492 } 493 494 /* ==================================================== */ 495 496 static class NestedStableField1 { 497 static class A { 498 public @Stable char a; 499 public @Stable A next; 500 } 501 public @Stable A v; 502 503 public static final NestedStableField1 c = new NestedStableField1(); 504 public static A get() { return c.v.next.next.next.next.next.next.next; } 505 public static char get1() { return get().a; } 506 507 public static void test() throws Exception { 508 { 509 c.v = new A(); c.v.next = new A(); c.v.next.next = c.v; 510 c.v.a = 'a'; c.v.next.a = 'a'; A val1 = get(); 511 c.v.a = 'b'; c.v.next.a = 'b'; A val2 = get(); 512 513 assertEquals(val1.a, 'b'); 514 assertEquals(val2.a, 'b'); 515 } 516 517 { 518 c.v = new A(); c.v.next = c.v; 519 c.v.a = 'a'; char val1 = get1(); 520 c.v.a = 'b'; char val2 = get1(); 521 c.v = new A(); c.v.next = c.v; 522 c.v.a = 'c'; char val3 = get1(); 523 524 assertEquals(val1, 'a'); 525 assertEquals(val2, (isStableEnabled ? 'a' : 'b')); 526 assertEquals(val3, (isStableEnabled ? 'a' : 'c')); 527 } 528 } 529 } 530 /* ==================================================== */ 531 532 static class NestedStableField2 { 533 static class A { 534 public @Stable char a; 535 public @Stable A left; 536 public A right; 537 } 538 539 public @Stable A v; 540 541 public static final NestedStableField2 c = new NestedStableField2(); 542 public static char get() { return c.v.left.left.left.a; } 543 public static char get1() { return c.v.left.left.right.left.a; } 544 545 public static void test() throws Exception { 546 { 547 c.v = new A(); c.v.left = c.v.right = c.v; 548 c.v.a = 'a'; char val1 = get(); char val2 = get1(); 549 c.v.a = 'b'; char val3 = get(); char val4 = get1(); 550 551 assertEquals(val1, 'a'); 552 assertEquals(val3, (isStableEnabled ? 'a' : 'b')); 553 554 assertEquals(val2, 'a'); 555 assertEquals(val4, 'b'); 556 } 557 } 558 } 559 560 /* ==================================================== */ 561 562 static class NestedStableField3 { 563 static class A { 564 public @Stable char a; 565 public @Stable A[] left; 566 public A[] right; 567 } 568 569 public @Stable A[] v; 570 571 public static final NestedStableField3 c = new NestedStableField3(); 572 public static char get() { return c.v[0].left[1].left[0].left[1].a; } 573 public static char get1() { return c.v[1].left[0].left[1].right[0].left[1].a; } 574 575 public static void test() throws Exception { 576 { 577 A elem = new A(); 578 c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v; 579 elem.a = 'a'; char val1 = get(); char val2 = get1(); 580 elem.a = 'b'; char val3 = get(); char val4 = get1(); 581 582 assertEquals(val1, 'a'); 583 assertEquals(val3, (isStableEnabled ? 'a' : 'b')); 584 585 assertEquals(val2, 'a'); 586 assertEquals(val4, 'b'); 587 } 588 } 589 } 590 591 /* ==================================================== */ 592 // Auxiliary methods 593 static void assertEquals(int i, int j) { if (i != j) throw new AssertionError(i + " != " + j); } 594 static void assertTrue(boolean b) { if (!b) throw new AssertionError(); } 595 596 static boolean failed = false; 597 598 public static void run(Class<?> test) { 599 Throwable ex = null; 600 System.out.print(test.getName()+": "); 601 try { 602 test.getMethod("test").invoke(null); 603 } catch (InvocationTargetException e) { 604 ex = e.getCause(); 605 } catch (Throwable e) { 606 ex = e; 607 } finally { 608 if (ex == null) { 609 System.out.println("PASSED"); 610 } else { 611 failed = true; 612 System.out.println("FAILED"); 613 ex.printStackTrace(System.out); 614 } 615 } 616 } 617 618 static final boolean isStableEnabled; 619 static { 620 HotSpotDiagnosticMXBean diagnostic 621 = ManagementFactoryHelper.getDiagnosticMXBean(); 622 VMOption tmp; 623 try { 624 tmp = diagnostic.getVMOption("FoldStableValues"); 625 } catch (IllegalArgumentException e) { 626 tmp = null; 627 } 628 isStableEnabled = (tmp == null ? false : Boolean.parseBoolean(tmp.getValue())); 629 } 630 }