/* * Copyright (c) 2013, 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 StableValueTest * @summary tests on stable fields and arrays * @library /testlibrary * @compile -XDignore.symbol.file StableValueTest.java * @run main ClassFileInstaller StableValueTest StableValueTest$BoolStable StableValueTest$ByteStable StableValueTest$CharStable * StableValueTest$DoubleStable StableValueTest$FloatStable StableValueTest$IntArrayArity1 * StableValueTest$IntArrayArity2 StableValueTest$IntArrayArity3 StableValueTest$IntArrayArity4 * StableValueTest$IntStable StableValueTest$LongStable StableValueTest$ObjectArrayArity0 * StableValueTest$ObjectArrayArity1 StableValueTest$ObjectStable$Values StableValueTest$ObjectStable * StableValueTest$ShortStable StableValueTest$StaticBoolStable StableValueTest$StaticByteStable * StableValueTest$StaticCharStable StableValueTest$StaticDoubleStable StableValueTest$StaticFloatStable * StableValueTest$StaticIntStable StableValueTest$StaticLongStable StableValueTest$StaticShortStable * StableValueTest$VolatileBoolStable StableValueTest$VolatileByteStable StableValueTest$VolatileCharStable * StableValueTest$VolatileDoubleStable StableValueTest$VolatileFloatStable StableValueTest$VolatileIntStable * StableValueTest$VolatileLongStable StableValueTest$VolatileObjectStable$Values * StableValueTest$VolatileObjectStable StableValueTest$VolatileShortStable * StableValueTest$NestedStableField StableValueTest$NestedStableField$A * StableValueTest$NestedStableField1 StableValueTest$NestedStableField1$A * StableValueTest$NestedStableField2 StableValueTest$NestedStableField2$A * StableValueTest$NestedStableField3 StableValueTest$NestedStableField3$A * * @run main/othervm -Xbootclasspath/a:. -server -XX:-TieredCompilation -Xcomp -XX:+UnlockExperimentalVMOptions * -XX:+FoldStableValues -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 StableValueTest * @run main/othervm -Xbootclasspath/a:. -server -XX:-TieredCompilation -Xcomp -XX:+UnlockExperimentalVMOptions * -XX:-FoldStableValues -XX:CompileOnly=::get,::get1,::get2,::get3,::get4 StableValueTest */ import com.sun.management.HotSpotDiagnosticMXBean; import com.sun.management.VMOption; import sun.invoke.Stable; import sun.management.ManagementFactoryHelper; import java.lang.reflect.InvocationTargetException; public class StableValueTest { public static void main(String[] args) throws Exception { System.out.println("@Stable enabled: "+isStableEnabled); System.out.println(); // @Stable primitive & raw Object fields run(ObjectStable.class); run(BoolStable.class); run(ByteStable.class); run(ShortStable.class); run(CharStable.class); run(IntStable.class); run(LongStable.class); run(FloatStable.class); run(DoubleStable.class); // @Stable primitive & raw Object static fields run(StaticBoolStable.class); run(StaticByteStable.class); run(StaticShortStable.class); run(StaticCharStable.class); run(StaticIntStable.class); run(StaticLongStable.class); run(StaticFloatStable.class); run(StaticDoubleStable.class); // @Stable primitive & raw Object volatile instance fields run(VolatileObjectStable.class); run(VolatileBoolStable.class); run(VolatileByteStable.class); run(VolatileShortStable.class); run(VolatileCharStable.class); run(VolatileIntStable.class); run(VolatileLongStable.class); run(VolatileFloatStable.class); run(VolatileDoubleStable.class); // @Stable arrays: arity 1-4 run(IntArrayArity1.class); run(IntArrayArity2.class); run(IntArrayArity3.class); run(IntArrayArity4.class); // @Stable Object field: dynamic arrays run(ObjectArrayArity0.class); run(ObjectArrayArity1.class); // Nested @Stable fields run(NestedStableField.class); run(NestedStableField1.class); run(NestedStableField2.class); run(NestedStableField3.class); if (failed) { throw new Error("TEST FAILED"); } } /* ==================================================== */ static class ObjectStable { enum Values { A, B} 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)); } } /* ==================================================== */ static class BoolStable { public @Stable boolean v; public static final BoolStable c = new BoolStable(); public static boolean get() { return c.v; } public static void test() throws Exception { c.v = true; boolean val1 = get(); c.v = false; boolean val2 = get(); assertEquals(val1, true); assertEquals(val2, (isStableEnabled ? true : false)); } } /* ==================================================== */ static class ByteStable { public @Stable byte v; public static final ByteStable c = new ByteStable(); public static byte get() { return c.v; } public static void test() throws Exception { c.v = 5; byte val1 = get(); c.v = 127; byte val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : 127)); } } /* ==================================================== */ static class ShortStable { public @Stable short v; public static final ShortStable c = new ShortStable(); public static short get() { return c.v; } public static void test() throws Exception { c.v = 1; short val1 = get(); c.v = 32767; short val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 32767)); } } /* ==================================================== */ static class CharStable { public @Stable char v; public static final CharStable c = new CharStable(); public static char get() { return c.v; } public static void test() throws Exception { c.v = 'a'; char val1 = get(); c.v = 'b'; char val2 = get(); assertEquals(val1, 'a'); assertEquals(val2, (isStableEnabled ? 'a' : 'b')); } } /* ==================================================== */ static class IntStable { public @Stable int v; public static final IntStable c = new IntStable(); public static int get() { return c.v; } public static void test() throws Exception { c.v = 1; int val1 = get(); c.v = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); } } /* ==================================================== */ static class FloatStable { public @Stable float v; public static final FloatStable c = new FloatStable(); public static float get() { return c.v; } public static void test() throws Exception { c.v = 1.0F; float val1 = get(); c.v = 2.0F; float val2 = get(); assertEquals(val1, 1.0F); assertEquals(val2, (isStableEnabled ? 1.0F : 2.0F)); } } /* ==================================================== */ static class LongStable { public @Stable long v; public static final LongStable c = new LongStable(); public static long get() { return c.v; } public static void test() throws Exception { c.v = 5; long val1 = get(); c.v = Long.MAX_VALUE; long val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); } } /* ==================================================== */ static class DoubleStable { public @Stable double v; public static final DoubleStable c = new DoubleStable(); public static double get() { return c.v; } public static void test() throws Exception { c.v = 1.0; double val1 = get(); c.v = Double.MAX_VALUE; double val2 = get(); assertEquals(val1, 1.0); assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE)); } } /* ==================================================== */ static class StaticBoolStable { public static @Stable boolean v; public static final StaticBoolStable c = new StaticBoolStable(); public static boolean get() { return c.v; } public static void test() throws Exception { c.v = true; boolean val1 = get(); c.v = false; boolean val2 = get(); assertEquals(val1, true); assertEquals(val2, (isStableEnabled ? true : false)); } } /* ==================================================== */ static class StaticByteStable { public static @Stable byte v; public static final StaticByteStable c = new StaticByteStable(); public static byte get() { return c.v; } public static void test() throws Exception { c.v = 5; byte val1 = get(); c.v = 127; byte val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : 127)); } } /* ==================================================== */ static class StaticShortStable { public static @Stable short v; public static final StaticShortStable c = new StaticShortStable(); public static short get() { return c.v; } public static void test() throws Exception { c.v = 1; short val1 = get(); c.v = 32767; short val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 32767)); } } /* ==================================================== */ static class StaticCharStable { public static @Stable char v; public static final StaticCharStable c = new StaticCharStable(); public static char get() { return c.v; } public static void test() throws Exception { c.v = 'a'; char val1 = get(); c.v = 'b'; char val2 = get(); assertEquals(val1, 'a'); assertEquals(val2, (isStableEnabled ? 'a' : 'b')); } } /* ==================================================== */ static class StaticIntStable { public static @Stable int v; public static final StaticIntStable c = new StaticIntStable(); public static int get() { return c.v; } public static void test() throws Exception { c.v = 1; int val1 = get(); c.v = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); } } /* ==================================================== */ static class StaticFloatStable { public static @Stable float v; public static final StaticFloatStable c = new StaticFloatStable(); public static float get() { return c.v; } public static void test() throws Exception { c.v = 1.0F; float val1 = get(); c.v = 2.0F; float val2 = get(); assertEquals(val1, 1.0F); assertEquals(val2, (isStableEnabled ? 1.0F : 2.0F)); } } /* ==================================================== */ static class StaticLongStable { public static @Stable long v; public static final StaticLongStable c = new StaticLongStable(); public static long get() { return c.v; } public static void test() throws Exception { c.v = 5; long val1 = get(); c.v = Long.MAX_VALUE; long val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); } } /* ==================================================== */ static class StaticDoubleStable { public static @Stable double v; public static final StaticDoubleStable c = new StaticDoubleStable(); public static double get() { return c.v; } public static void test() throws Exception { c.v = 1.0; double val1 = get(); c.v = Double.MAX_VALUE; double val2 = get(); assertEquals(val1, 1.0); assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE)); } } /* ==================================================== */ static class VolatileObjectStable { enum Values { A, B} 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)); } } /* ==================================================== */ static class VolatileBoolStable { public @Stable volatile boolean v; public static final VolatileBoolStable c = new VolatileBoolStable(); public static boolean get() { return c.v; } public static void test() throws Exception { c.v = true; boolean val1 = get(); c.v = false; boolean val2 = get(); assertEquals(val1, true); assertEquals(val2, (isStableEnabled ? true : false)); } } /* ==================================================== */ static class VolatileByteStable { public @Stable volatile byte v; public static final VolatileByteStable c = new VolatileByteStable(); public static byte get() { return c.v; } public static void test() throws Exception { c.v = 5; byte val1 = get(); c.v = 127; byte val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : 127)); } } /* ==================================================== */ static class VolatileShortStable { public @Stable volatile short v; public static final VolatileShortStable c = new VolatileShortStable(); public static short get() { return c.v; } public static void test() throws Exception { c.v = 1; short val1 = get(); c.v = 32767; short val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 32767)); } } /* ==================================================== */ static class VolatileCharStable { public @Stable volatile char v; public static final VolatileCharStable c = new VolatileCharStable(); public static char get() { return c.v; } public static void test() throws Exception { c.v = 'a'; char val1 = get(); c.v = 'b'; char val2 = get(); assertEquals(val1, 'a'); assertEquals(val2, (isStableEnabled ? 'a' : 'b')); } } /* ==================================================== */ static class VolatileIntStable { public @Stable volatile int v; public static final VolatileIntStable c = new VolatileIntStable(); public static int get() { return c.v; } public static void test() throws Exception { c.v = 1; int val1 = get(); c.v = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); } } /* ==================================================== */ static class VolatileFloatStable { public @Stable volatile float v; public static final VolatileFloatStable c = new VolatileFloatStable(); public static float get() { return c.v; } public static void test() throws Exception { c.v = 1.0F; float val1 = get(); c.v = 2.0F; float val2 = get(); assertEquals(val1, 1.0F); assertEquals(val2, (isStableEnabled ? 1.0F : 2.0F)); } } /* ==================================================== */ static class VolatileLongStable { public @Stable volatile long v; public static final VolatileLongStable c = new VolatileLongStable(); public static long get() { return c.v; } public static void test() throws Exception { c.v = 5; long val1 = get(); c.v = Long.MAX_VALUE; long val2 = get(); assertEquals(val1, 5); assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE)); } } /* ==================================================== */ static class VolatileDoubleStable { public @Stable double v; public static final VolatileDoubleStable c = new VolatileDoubleStable(); public static double get() { return c.v; } public static void test() throws Exception { c.v = 1.0; double val1 = get(); c.v = Double.MAX_VALUE; double val2 = get(); assertEquals(val1, 1.0); assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE)); } } /* ==================================================== */ // @Stable array == field && all components are stable static class IntArrayArity1 { public static @Stable int[] v; public static final IntArrayArity1 c = new IntArrayArity1(); public static int get() { return c.v[0]; } public static int[] get1() { return c.v; } public static void test() throws Exception { { c.v = new int[1]; c.v[0] = 1; int val1 = get(); c.v[0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); c.v = new int[1]; c.v[0] = 3; int val3 = get(); assertEquals(val3, (isStableEnabled ? 1 : 3)); } { c.v = new int[1]; int[] val1 = get1(); c.v = new int[1]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ static class IntArrayArity2 { public static @Stable int[][] v; public static final IntArrayArity2 c = new IntArrayArity2(); public static int get() { return c.v[0][0]; } public static int[] get1() { return c.v[0]; } public static int[][] get2() { return c.v; } public static void test() throws Exception { { c.v = new int[1][1]; c.v[0][0] = 1; int val1 = get(); c.v[0][0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); c.v = new int[1][1]; c.v[0][0] = 3; int val3 = get(); assertEquals(val3, (isStableEnabled ? 1 : 3)); c.v[0] = new int[1]; c.v[0][0] = 4; int val4 = get(); assertEquals(val4, (isStableEnabled ? 1 : 4)); } { c.v = new int[1][1]; int[] val1 = get1(); c.v[0] = new int[1]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1]; int[][] val1 = get2(); c.v = new int[1][1]; int[][] val2 = get2(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ static class IntArrayArity3 { public static @Stable int[][][] v; public static final IntArrayArity3 c = new IntArrayArity3(); public static int get() { return c.v[0][0][0]; } public static int[] get1() { return c.v[0][0]; } public static int[][] get2() { return c.v[0]; } public static int[][][] get3() { return c.v; } public static void test() throws Exception { { c.v = new int[1][1][1]; c.v[0][0][0] = 1; int val1 = get(); c.v[0][0][0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); c.v = new int[1][1][1]; c.v[0][0][0] = 3; int val3 = get(); assertEquals(val3, (isStableEnabled ? 1 : 3)); c.v[0] = new int[1][1]; c.v[0][0][0] = 4; int val4 = get(); assertEquals(val4, (isStableEnabled ? 1 : 4)); c.v[0][0] = new int[1]; c.v[0][0][0] = 5; int val5 = get(); assertEquals(val5, (isStableEnabled ? 1 : 5)); } { c.v = new int[1][1][1]; int[] val1 = get1(); c.v[0][0] = new int[1]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1][1]; int[][] val1 = get2(); c.v[0] = new int[1][1]; int[][] val2 = get2(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1][1]; int[][][] val1 = get3(); c.v = new int[1][1][1]; int[][][] val2 = get3(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ static class IntArrayArity4 { public static @Stable int[][][][] v; public static final IntArrayArity4 c = new IntArrayArity4(); public static int get() { return c.v[0][0][0][0]; } public static int[] get1() { return c.v[0][0][0]; } public static int[][] get2() { return c.v[0][0]; } public static int[][][] get3() { return c.v[0]; } public static int[][][][] get4() { return c.v; } public static void test() throws Exception { { c.v = new int[1][1][1][1]; c.v[0][0][0][0] = 1; int val1 = get(); c.v[0][0][0][0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); c.v = new int[1][1][1][1]; c.v[0][0][0][0] = 3; int val3 = get(); assertEquals(val3, (isStableEnabled ? 1 : 3)); c.v[0] = new int[1][1][1]; c.v[0][0][0][0] = 4; int val4 = get(); assertEquals(val4, (isStableEnabled ? 1 : 4)); c.v[0][0] = new int[1][1]; c.v[0][0][0][0] = 5; int val5 = get(); assertEquals(val5, (isStableEnabled ? 1 : 5)); c.v[0][0][0] = new int[1]; c.v[0][0][0][0] = 6; int val6 = get(); assertEquals(val6, (isStableEnabled ? 1 : 6)); } { c.v = new int[1][1][1][1]; int[] val1 = get1(); c.v[0][0][0] = new int[1]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1][1][1]; int[][] val1 = get2(); c.v[0][0] = new int[1][1]; int[][] val2 = get2(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1][1][1]; int[][][] val1 = get3(); c.v[0] = new int[1][1][1]; int[][][] val2 = get3(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[1][1][1][1]; int[][][][] val1 = get4(); c.v = new int[1][1][1][1]; int[][][][] val2 = get4(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ // Dynamic arity is higher than static static class ObjectArrayArity0 { public static @Stable Object v; public static final ObjectArrayArity0 c = new ObjectArrayArity0(); public static int get() { return ((int[])c.v)[0]; } public static int[] get1() { return (int[])c.v; } public static void test() throws Exception { { c.v = new int[1]; ((int[])c.v)[0] = 1; int val1 = get(); ((int[])c.v)[0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, 2); } { c.v = new int[1]; int[] val1 = get1(); c.v = new int[1]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ static class ObjectArrayArity1 { public static @Stable Object[] v; public static final ObjectArrayArity1 c = new ObjectArrayArity1(); public static int get() { return ((int[][])c.v)[0][0]; } public static int[] get1() { return (int[])(c.v[0]); } public static Object[] get2() { return c.v; } public static void test() throws Exception { { c.v = new int[1][1]; ((int[][])c.v)[0][0] = 1; int val1 = get(); ((int[][])c.v)[0][0] = 2; int val2 = get(); assertEquals(val1, 1); assertEquals(val2, 2); } { c.v = new int[1][1]; c.v[0] = new int[0]; int[] val1 = get1(); c.v[0] = new int[0]; int[] val2 = get1(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } { c.v = new int[0][0]; Object[] val1 = get2(); c.v = new int[0][0]; Object[] val2 = get2(); assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2))); } } } /* ==================================================== */ static class NestedStableField { static class A { public @Stable int a; } public static @Stable A v; public static final NestedStableField c = new NestedStableField(); public static A get() { return c.v; } public static int get1() { return get().a; } public static void test() throws Exception { { c.v = new A(); c.v.a = 1; A val1 = get(); c.v.a = 2; A val2 = get(); assertEquals(val1.a, 2); assertEquals(val2.a, 2); } { c.v = new A(); c.v.a = 1; int val1 = get1(); c.v.a = 2; int val2 = get1(); c.v = new A(); c.v.a = 3; int val3 = get1(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); assertEquals(val3, (isStableEnabled ? 1 : 3)); } } } /* ==================================================== */ static class NestedStableField1 { static class A { public @Stable int a; public @Stable A next; } public static @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 int 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 = 1; c.v.next.a = 1; A val1 = get(); c.v.a = 2; c.v.next.a = 2; A val2 = get(); assertEquals(val1.a, 2); assertEquals(val2.a, 2); } { c.v = new A(); c.v.next = c.v; c.v.a = 1; int val1 = get1(); c.v.a = 2; int val2 = get1(); c.v = new A(); c.v.next = c.v; c.v.a = 3; int val3 = get1(); assertEquals(val1, 1); assertEquals(val2, (isStableEnabled ? 1 : 2)); assertEquals(val3, (isStableEnabled ? 1 : 3)); } } } /* ==================================================== */ static class NestedStableField2 { static class A { public @Stable int a; public @Stable A left; public A right; } public static @Stable A v; public static final NestedStableField2 c = new NestedStableField2(); public static int get() { return c.v.left.left.left.a; } public static int 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 = 1; int val1 = get(); int val2 = get1(); c.v.a = 2; int val3 = get(); int val4 = get1(); assertEquals(val1, 1); assertEquals(val3, (isStableEnabled ? 1 : 2)); assertEquals(val2, 1); assertEquals(val4, 2); } } } /* ==================================================== */ static class NestedStableField3 { static class A { public @Stable int a; public @Stable A[] left; public A[] right; } public static @Stable A[] v; public static final NestedStableField3 c = new NestedStableField3(); public static int get() { return c.v[0].left[1].left[0].left[1].a; } public static int 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 = 1; int val1 = get(); int val2 = get1(); elem.a = 2; int val3 = get(); int val4 = get1(); assertEquals(val1, 1); assertEquals(val3, (isStableEnabled ? 1 : 2)); assertEquals(val2, 1); assertEquals(val4, 2); } } } /* ==================================================== */ // Auxiliary methods static void assertEquals(int i, int j) { if (i != j) throw new AssertionError(i + " != " + j); } static void assertEquals(long i, long j) { if (i != j) throw new AssertionError(i + " != " + j); } static void assertEquals(boolean i, boolean j) { if (i != j) throw new AssertionError(i + " != " + j); } static void assertEquals(float i, float j) { if (i != j) throw new AssertionError(i + " != " + j); } static void assertEquals(double i, double j) { if (i != j) throw new AssertionError(i + " != " + j); } static void assertEquals(Object i, Object j) { if (!i.equals(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); } } } static final boolean isStableEnabled; static { HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean(); VMOption tmp; try { tmp = diagnostic.getVMOption("FoldStableValues"); } catch (IllegalArgumentException e) { tmp = null; } isStableEnabled = (tmp == null ? false : Boolean.parseBoolean(tmp.getValue())); } }