1 /*
  2  * Copyright (c) 2019, 2021, 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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package runtime.valhalla.inlinetypes;
 24 
 25 import jdk.test.lib.Asserts;
 26 
 27 /*
 28  * @test
 29  * @summary Test circularity in static fields
 30  * @library /test/lib
 31  * @compile StaticFieldsTest.java
 32  * @run main runtime.valhalla.inlinetypes.StaticFieldsTest
 33  */
 34 
 35 public class StaticFieldsTest {
 36 
 37 
 38     // ClassA and ClassB have a simple cycle in their static fields, but they should
 39     // be able to load and initialize themselves successfully. Access to these
 40     // static fields after their initialization should return the default value.
 41     static primitive class ClassA {
 42         static ClassB b;
 43         public int i;
 44 
 45         public ClassA() {
 46             i = 3;
 47         }
 48     }
 49 
 50     static primitive class ClassB {
 51         static ClassA a;
 52         public int i;
 53 
 54         public ClassB() {
 55             i = 700;
 56         }
 57     }
 58 
 59     // ClassC has a reference to itself in its static field, but it should be able
 60     // to initialize itelf successfully. Access to this static field after initialization
 61     // should return the default value.
 62     static primitive class ClassC {
 63         static ClassC c;
 64         int i;
 65 
 66         public ClassC() {
 67             i = 42;
 68         }
 69     }
 70 
 71 
 72     // ClassD and ClassE have circular references in their static fields, and they
 73     // read these static fields during their initialization, the value read from
 74     // these fields should be the default value. Both classes should initialize
 75     // successfully.
 76     static primitive class ClassD {
 77         static ClassE e;
 78         int i;
 79 
 80         static {
 81             Asserts.assertEquals(e.i, 0, "Static field e.i incorrect");
 82         }
 83 
 84         public ClassD() {
 85             i = 42;
 86         }
 87     }
 88 
 89     static primitive class ClassE {
 90         static ClassD d;
 91         int i;
 92 
 93         static {
 94             Asserts.assertEquals(d.i, 0, "Static field d.i incorrect");
 95         }
 96 
 97         public ClassE() {
 98             i = 42;
 99         }
100     }
101 
102     // ClassF and ClassG have circular references in their static fields, and they
103     // create new instances of each other type to initialize these static fields
104     // during their initialization. Both classes should initialize successfully.
105     static primitive class ClassF {
106         static ClassG g;
107         int i;
108 
109         static {
110             g = new ClassG();
111             Asserts.assertEquals(g.i, 64, "Static field ClassF.g.i incorrect");
112         }
113 
114         ClassF() {
115             i = 314;
116         }
117     }
118 
119     static primitive class ClassG {
120         static ClassF f;
121         int i;
122 
123         static {
124             f = new ClassF();
125             Asserts.assertEquals(f.i, 314, "Static field ClassG.f.i incorrect");
126         }
127 
128         ClassG() {
129             i = 64;
130         }
131     }
132 
133     public static void main(String[] args) {
134         Asserts.assertEquals(ClassA.b.i, 0, "Static field ClassA.b.i incorrect");
135         Asserts.assertEquals(ClassB.a.i, 0, "Static field Classb.a.i incorrect");
136         Asserts.assertEquals(ClassC.c.i, 0, "Static field ClassC.c.i incorrect");
137         new ClassD();
138         new ClassF();
139     }
140 }