TestStableObject.java 26.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test TestStableObject
 * @summary tests on stable fields and arrays
29 30 31
 * @library /testlibrary /testlibrary/whitebox
 * @build TestStableObject StableConfiguration sun.hotspot.WhiteBox
 * @run main ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
32
 * @run main ClassFileInstaller
33
 *           java/lang/invoke/StableConfiguration
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 *           java/lang/invoke/TestStableObject
 *           java/lang/invoke/TestStableObject$ObjectStable
 *           java/lang/invoke/TestStableObject$StaticObjectStable
 *           java/lang/invoke/TestStableObject$VolatileObjectStable
 *           java/lang/invoke/TestStableObject$ObjectArrayDim1
 *           java/lang/invoke/TestStableObject$ObjectArrayDim2
 *           java/lang/invoke/TestStableObject$ObjectArrayDim3
 *           java/lang/invoke/TestStableObject$ObjectArrayDim4
 *           java/lang/invoke/TestStableObject$ObjectArrayLowerDim0
 *           java/lang/invoke/TestStableObject$ObjectArrayLowerDim1
 *           java/lang/invoke/TestStableObject$NestedStableField
 *           java/lang/invoke/TestStableObject$NestedStableField$A
 *           java/lang/invoke/TestStableObject$NestedStableField1
 *           java/lang/invoke/TestStableObject$NestedStableField1$A
 *           java/lang/invoke/TestStableObject$NestedStableField2
 *           java/lang/invoke/TestStableObject$NestedStableField2$A
 *           java/lang/invoke/TestStableObject$NestedStableField3
 *           java/lang/invoke/TestStableObject$NestedStableField3$A
 *           java/lang/invoke/TestStableObject$Values
 *           java/lang/invoke/TestStableObject$DefaultValue
54
 *           java/lang/invoke/TestStableObject$DefaultStaticValue
55 56
 *           java/lang/invoke/TestStableObject$ObjectArrayLowerDim2
 *
57
 * @run main/othervm -Xbootclasspath/a:.
58
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
59
 *                   -XX:-TieredCompilation
60
 *                   -XX:+FoldStableValues
61 62
 *                   -XX:CompileOnly=::get,::get1,::get2,::get3,::get4
 *                   java.lang.invoke.TestStableObject
63
 * @run main/othervm -Xbootclasspath/a:.
64
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
65
 *                   -XX:-TieredCompilation
66
 *                   -XX:-FoldStableValues
67 68 69
 *                   -XX:CompileOnly=::get,::get1,::get2,::get3,::get4
 *                   java.lang.invoke.TestStableObject
 *
70
 * @run main/othervm -Xbootclasspath/a:.
71
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
72
 *                   -XX:+TieredCompilation -XX:TieredStopAtLevel=1
73 74 75
 *                   -XX:+FoldStableValues
 *                   -XX:CompileOnly=::get,::get1,::get2,::get3,::get4
 *                   java.lang.invoke.TestStableObject
76
 * @run main/othervm -Xbootclasspath/a:.
77
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp
78
 *                   -XX:+TieredCompilation -XX:TieredStopAtLevel=1
79
 *                   -XX:-FoldStableValues
80 81 82 83 84 85 86 87 88
 *                   -XX:CompileOnly=::get,::get1,::get2,::get3,::get4
 *                   java.lang.invoke.TestStableObject
 *
 */
package java.lang.invoke;

import java.lang.reflect.InvocationTargetException;

public class TestStableObject {
89 90
    static final boolean isStableEnabled    = StableConfiguration.isStableEnabled;
    static final boolean isServerWithStable = StableConfiguration.isServerWithStable;
91

92
    public static void main(String[] args) throws Exception {
93 94
        run(DefaultValue.class);
        run(ObjectStable.class);
95
        run(DefaultStaticValue.class);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        run(StaticObjectStable.class);
        run(VolatileObjectStable.class);

        // @Stable arrays: Dim 1-4
        run(ObjectArrayDim1.class);
        run(ObjectArrayDim2.class);
        run(ObjectArrayDim3.class);
        run(ObjectArrayDim4.class);

        // @Stable Object field: dynamic arrays
        run(ObjectArrayLowerDim0.class);
        run(ObjectArrayLowerDim1.class);
        run(ObjectArrayLowerDim2.class);

        // Nested @Stable fields
        run(NestedStableField.class);
        run(NestedStableField1.class);
        run(NestedStableField2.class);
        run(NestedStableField3.class);

        if (failed) {
            throw new Error("TEST FAILED");
        }
    }

    /* ==================================================== */

    enum Values {A, B, C, D, E, F}

    static class DefaultValue {
        public @Stable Object v;

        public static final DefaultValue c = new DefaultValue();
        public static Object get() { return c.v; }
        public static void test() throws Exception {
                            Object val1 = get();
            c.v = Values.A; Object val2 = get();
            assertEquals(val1, null);
            assertEquals(val2, Values.A);
        }
    }

    /* ==================================================== */

    static class ObjectStable {
        public @Stable Values v;

        public static final ObjectStable c = new ObjectStable ();
        public static Values get() { return c.v; }
        public static void test() throws Exception {
            c.v = Values.A; Values val1 = get();
            c.v = Values.B; Values val2 = get();
            assertEquals(val1, Values.A);
            assertEquals(val2, (isStableEnabled ? Values.A : Values.B));
        }
    }

    /* ==================================================== */

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    static class DefaultStaticValue {
        public static @Stable Object v;

        public static final DefaultStaticValue c = new DefaultStaticValue();
        public static Object get() { return c.v; }
        public static void test() throws Exception {
                            Object val1 = get();
            c.v = Values.A; Object val2 = get();
            assertEquals(val1, null);
            assertEquals(val2, Values.A);
        }
    }

    /* ==================================================== */

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    static class StaticObjectStable {
        public static @Stable Values v;

        public static final ObjectStable c = new ObjectStable ();
        public static Values get() { return c.v; }
        public static void test() throws Exception {
            c.v = Values.A; Values val1 = get();
            c.v = Values.B; Values val2 = get();
            assertEquals(val1, Values.A);
            assertEquals(val2, (isStableEnabled ? Values.A : Values.B));
        }
    }

    /* ==================================================== */

    static class VolatileObjectStable {
        public @Stable volatile Values v;

        public static final VolatileObjectStable c = new VolatileObjectStable ();
        public static Values get() { return c.v; }
        public static void test() throws Exception {
            c.v = Values.A; Values val1 = get();
            c.v = Values.B; Values val2 = get();
            assertEquals(val1, Values.A);
            assertEquals(val2, (isStableEnabled ? Values.A : Values.B));
        }
    }

    /* ==================================================== */
    // @Stable array == field && all components are stable

    static class ObjectArrayDim1 {
        public @Stable Object[] v;

        public static final ObjectArrayDim1 c = new ObjectArrayDim1();
        public static Object get() { return c.v[0]; }
        public static Object get1() { return c.v[10]; }
        public static Object[] get2() { return c.v; }
        public static void test() throws Exception {
            {
                c.v = new Object[1]; c.v[0] = Values.A; Object val1 = get();
                                     c.v[0] = Values.B; Object val2 = get();
                assertEquals(val1, Values.A);
213
                assertEquals(val2, (isServerWithStable ? Values.A : Values.B));
214 215

                c.v = new Object[1]; c.v[0] = Values.C; Object val3 = get();
216 217
                assertEquals(val3, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.C));
218 219 220 221 222 223
            }

            {
                c.v = new Object[20]; c.v[10] = Values.A; Object val1 = get1();
                                      c.v[10] = Values.B; Object val2 = get1();
                assertEquals(val1, Values.A);
224
                assertEquals(val2, (isServerWithStable ? Values.A : Values.B));
225 226

                c.v = new Object[20]; c.v[10] = Values.C; Object val3 = get1();
227 228
                assertEquals(val3, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.C));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
            }

            {
                c.v = new Object[1]; Object[] val1 = get2();
                c.v = new Object[1]; Object[] val2 = get2();
                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class ObjectArrayDim2 {
        public @Stable Object[][] v;

        public static final ObjectArrayDim2 c = new ObjectArrayDim2();
        public static Object get() { return c.v[0][0]; }
        public static Object[] get1() { return c.v[0]; }
        public static Object[][] get2() { return c.v; }
        public static void test() throws Exception {
            {
                c.v = new Object[1][1]; c.v[0][0] = Values.A; Object val1 = get();
                                        c.v[0][0] = Values.B; Object val2 = get();
                assertEquals(val1, Values.A);
253
                assertEquals(val2, (isServerWithStable ? Values.A : Values.B));
254 255

                c.v = new Object[1][1]; c.v[0][0] = Values.C; Object val3 = get();
256 257
                assertEquals(val3, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.C));
258 259

                c.v[0] = new Object[1]; c.v[0][0] = Values.D; Object val4 = get();
260 261
                assertEquals(val4, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.D));
262 263 264 265 266
            }

            {
                c.v = new Object[1][1]; Object[] val1 = get1();
                c.v[0] = new Object[1]; Object[] val2 = get1();
267
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
            }

            {
                c.v = new Object[1][1]; Object[][] val1 = get2();
                c.v = new Object[1][1]; Object[][] val2 = get2();
                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class ObjectArrayDim3 {
        public @Stable Object[][][] v;

        public static final ObjectArrayDim3 c = new ObjectArrayDim3();
        public static Object get() { return c.v[0][0][0]; }
        public static Object[] get1() { return c.v[0][0]; }
        public static Object[][] get2() { return c.v[0]; }
        public static Object[][][] get3() { return c.v; }
        public static void test() throws Exception {
            {
                c.v = new Object[1][1][1]; c.v[0][0][0] = Values.A; Object val1 = get();
                                           c.v[0][0][0] = Values.B; Object val2 = get();
                assertEquals(val1, Values.A);
293
                assertEquals(val2, (isServerWithStable ? Values.A : Values.B));
294 295

                c.v = new Object[1][1][1]; c.v[0][0][0] = Values.C; Object val3 = get();
296 297
                assertEquals(val3, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.C));
298 299

                c.v[0] = new Object[1][1]; c.v[0][0][0] = Values.D; Object val4 = get();
300 301
                assertEquals(val4, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.D));
302 303

                c.v[0][0] = new Object[1]; c.v[0][0][0] = Values.E; Object val5 = get();
304 305
                assertEquals(val5, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.E));
306 307 308 309 310
            }

            {
                c.v = new Object[1][1][1]; Object[] val1 = get1();
                c.v[0][0] = new Object[1]; Object[] val2 = get1();
311
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
312 313 314 315 316
            }

            {
                c.v = new Object[1][1][1]; Object[][] val1 = get2();
                c.v[0] = new Object[1][1]; Object[][] val2 = get2();
317
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
            }

            {
                c.v = new Object[1][1][1]; Object[][][] val1 = get3();
                c.v = new Object[1][1][1]; Object[][][] val2 = get3();
                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class ObjectArrayDim4 {
        public @Stable Object[][][][] v;

        public static final ObjectArrayDim4 c = new ObjectArrayDim4();
        public static Object get() { return c.v[0][0][0][0]; }
        public static Object[] get1() { return c.v[0][0][0]; }
        public static Object[][] get2() { return c.v[0][0]; }
        public static Object[][][] get3() { return c.v[0]; }
        public static Object[][][][] get4() { return c.v; }
        public static void test() throws Exception {
            {
                c.v = new Object[1][1][1][1]; c.v[0][0][0][0] = Values.A; Object val1 = get();
                                              c.v[0][0][0][0] = Values.B; Object val2 = get();
                assertEquals(val1, Values.A);
344
                assertEquals(val2, (isServerWithStable ? Values.A : Values.B));
345 346

                c.v = new Object[1][1][1][1]; c.v[0][0][0][0] = Values.C; Object val3 = get();
347 348
                assertEquals(val3, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.C));
349 350

                c.v[0] = new Object[1][1][1]; c.v[0][0][0][0] = Values.D; Object val4 = get();
351 352
                assertEquals(val4, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.D));
353 354

                c.v[0][0] = new Object[1][1]; c.v[0][0][0][0] = Values.E; Object val5 = get();
355 356
                assertEquals(val5, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.E));
357 358

                c.v[0][0][0] = new Object[1]; c.v[0][0][0][0] = Values.F; Object val6 = get();
359 360
                assertEquals(val6, (isStableEnabled ? (isServerWithStable ? Values.A : Values.B)
                                                    : Values.F));
361 362 363 364 365
            }

            {
                c.v = new Object[1][1][1][1]; Object[] val1 = get1();
                c.v[0][0][0] = new Object[1]; Object[] val2 = get1();
366
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
367 368 369 370 371
            }

            {
                c.v = new Object[1][1][1][1]; Object[][] val1 = get2();
                c.v[0][0] = new Object[1][1]; Object[][] val2 = get2();
372
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
373 374 375 376 377
            }

            {
                c.v = new Object[1][1][1][1]; Object[][][] val1 = get3();
                c.v[0] = new Object[1][1][1]; Object[][][] val2 = get3();
378
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
            }

            {
                c.v = new Object[1][1][1][1]; Object[][][][] val1 = get4();
                c.v = new Object[1][1][1][1]; Object[][][][] val2 = get4();
                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */
    // Dynamic Dim is higher than static
    static class ObjectArrayLowerDim0 {
        public @Stable Object v;

        public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0();
        public static Object get() { return ((Object[])c.v)[0]; }
        public static Object[] get1() { return (Object[])c.v; }

        public static void test() throws Exception {
            {
                c.v = new Object[1]; ((Object[])c.v)[0] = Values.A; Object val1 = get();
                                     ((Object[])c.v)[0] = Values.B; Object val2 = get();

                assertEquals(val1, Values.A);
                assertEquals(val2, Values.B);
            }

            {
                c.v = new Object[1]; Object[] val1 = get1();
                c.v = new Object[1]; Object[] val2 = get1();
                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class ObjectArrayLowerDim1 {
        public @Stable Object[] v;

        public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1();
        public static Object get() { return ((Object[][])c.v)[0][0]; }
        public static Object[] get1() { return (Object[])(c.v[0]); }
        public static Object[] get2() { return c.v; }

        public static void test() throws Exception {
            {
                c.v = new Object[1][1]; ((Object[][])c.v)[0][0] = Values.A; Object val1 = get();
                                        ((Object[][])c.v)[0][0] = Values.B; Object val2 = get();

                assertEquals(val1, Values.A);
                assertEquals(val2, Values.B);
            }

            {
                c.v = new Object[1][1]; c.v[0] = new Object[0]; Object[] val1 = get1();
                                     c.v[0] = new Object[0]; Object[] val2 = get1();

438
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
            }

            {
                c.v = new Object[0][0]; Object[] val1 = get2();
                c.v = new Object[0][0]; Object[] val2 = get2();

                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class ObjectArrayLowerDim2 {
        public @Stable Object[][] v;

        public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2();
        public static Object get() { return ((Object[][][])c.v)[0][0][0]; }
        public static Object[] get1() { return (Object[])(c.v[0][0]); }
        public static Object[][] get2() { return (Object[][])(c.v[0]); }
        public static Object[][] get3() { return c.v; }

        public static void test() throws Exception {
            {
                c.v = new Object[1][1][1]; ((Object[][][])c.v)[0][0][0] = Values.A; Object val1 = get();
                                           ((Object[][][])c.v)[0][0][0] = Values.B; Object val2 = get();

                assertEquals(val1, Values.A);
                assertEquals(val2, Values.B);
            }

            {
                c.v = new Object[1][1][1]; c.v[0][0] = new Object[0]; Object[] val1 = get1();
                                           c.v[0][0] = new Object[0]; Object[] val2 = get1();

474
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
475 476 477 478 479 480
            }

            {
                c.v = new Object[1][1][1]; c.v[0] = new Object[0][0]; Object[][] val1 = get2();
                                           c.v[0] = new Object[0][0]; Object[][] val2 = get2();

481
                assertTrue((isServerWithStable ? (val1 == val2) : (val1 != val2)));
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
            }

            {
                c.v = new Object[0][0][0]; Object[][] val1 = get3();
                c.v = new Object[0][0][0]; Object[][] val2 = get3();

                assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));
            }
        }
    }

    /* ==================================================== */

    static class NestedStableField {
        static class A {
            public @Stable Object a;

        }
        public @Stable A v;

        public static final NestedStableField c = new NestedStableField();
        public static A get() { return c.v; }
        public static Object get1() { return get().a; }

        public static void test() throws Exception {
            {
                c.v = new A(); c.v.a = Values.A; A val1 = get();
                               c.v.a = Values.B; A val2 = get();

                assertEquals(val1.a, Values.B);
                assertEquals(val2.a, Values.B);
            }

            {
                c.v = new A(); c.v.a = Values.A; Object val1 = get1();
                               c.v.a = Values.B; Object val2 = get1();
                c.v = new A(); c.v.a = Values.C; Object val3 = get1();

                assertEquals(val1, Values.A);
                assertEquals(val2, (isStableEnabled ? Values.A : Values.B));
                assertEquals(val3, (isStableEnabled ? Values.A : Values.C));
            }
        }
    }

    /* ==================================================== */

    static class NestedStableField1 {
        static class A {
            public @Stable Object a;
            public @Stable A next;
        }
        public @Stable A v;

        public static final NestedStableField1 c = new NestedStableField1();
        public static A get() { return c.v.next.next.next.next.next.next.next; }
        public static Object get1() { return get().a; }

        public static void test() throws Exception {
            {
                c.v = new A(); c.v.next = new A();   c.v.next.next  = c.v;
                               c.v.a = Values.A; c.v.next.a = Values.A; A val1 = get();
                               c.v.a = Values.B; c.v.next.a = Values.B; A val2 = get();

                assertEquals(val1.a, Values.B);
                assertEquals(val2.a, Values.B);
            }

            {
                c.v = new A(); c.v.next = c.v;
                               c.v.a = Values.A; Object val1 = get1();
                               c.v.a = Values.B; Object val2 = get1();
                c.v = new A(); c.v.next = c.v;
                               c.v.a = Values.C; Object val3 = get1();

                assertEquals(val1, Values.A);
                assertEquals(val2, (isStableEnabled ? Values.A : Values.B));
                assertEquals(val3, (isStableEnabled ? Values.A : Values.C));
            }
        }
    }
   /* ==================================================== */

    static class NestedStableField2 {
        static class A {
            public @Stable Object a;
            public @Stable A left;
            public         A right;
        }

        public @Stable A v;

        public static final NestedStableField2 c = new NestedStableField2();
        public static Object get() { return c.v.left.left.left.a; }
        public static Object get1() { return c.v.left.left.right.left.a; }

        public static void test() throws Exception {
            {
                c.v = new A(); c.v.left = c.v.right = c.v;
                               c.v.a = Values.A; Object val1 = get(); Object val2 = get1();
                               c.v.a = Values.B; Object val3 = get(); Object val4 = get1();

                assertEquals(val1, Values.A);
                assertEquals(val3, (isStableEnabled ? Values.A : Values.B));

                assertEquals(val2, Values.A);
                assertEquals(val4, Values.B);
            }
        }
    }

    /* ==================================================== */

    static class NestedStableField3 {
        static class A {
            public @Stable Object a;
            public @Stable A[] left;
            public         A[] right;
        }

        public @Stable A[] v;

        public static final NestedStableField3 c = new NestedStableField3();
        public static Object get() { return c.v[0].left[1].left[0].left[1].a; }
        public static Object get1() { return c.v[1].left[0].left[1].right[0].left[1].a; }

        public static void test() throws Exception {
            {
                A elem = new A();
                c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v;
                               elem.a = Values.A; Object val1 = get(); Object val2 = get1();
                               elem.a = Values.B; Object val3 = get(); Object val4 = get1();

                assertEquals(val1, Values.A);
616
                assertEquals(val3, (isServerWithStable ? Values.A : Values.B));
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

                assertEquals(val2, Values.A);
                assertEquals(val4, Values.B);
            }
        }
    }

    /* ==================================================== */
    // Auxiliary methods
    static void assertEquals(Object i, Object j) { if (i != j)  throw new AssertionError(i + " != " + j); }
    static void assertTrue(boolean b) { if (!b)  throw new AssertionError(); }

    static boolean failed = false;

    public static void run(Class<?> test) {
        Throwable ex = null;
        System.out.print(test.getName()+": ");
        try {
            test.getMethod("test").invoke(null);
        } catch (InvocationTargetException e) {
            ex = e.getCause();
        } catch (Throwable e) {
            ex = e;
        } finally {
            if (ex == null) {
                System.out.println("PASSED");
            } else {
                failed = true;
                System.out.println("FAILED");
                ex.printStackTrace(System.out);
            }
        }
    }
}