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