Debian Codegraph @

checker/tests/initialization/fbc/Issue556b.java

raw
1 // @skip-test
2
3 // To reproduce the problem, run:
4 // javac -processor nullness Issue556b.java
5 // java Issue556b
6 // and observe that the javac execution issues no warnings but the java
7 // execution suffers a null pointer exception.
8
9 // Before the constructor is invoked, static initializers and static blocks
10 // are executed. This suggests that the Initialization Checker can assume
11 // that static fields are initialized in the constructor.
12 //
13 // However, if any user-defined code -- including callbacks such as
14 // executions of equals() and hashCode() -- appears in a static initializer
15 // or static block, then static fields cannot be assumed to be
16 // initialized within the constructor.
17
18 public class Issue556b {
19 static class Parent {
20 private final Object o;
21
22 public Parent(final Object o) {
23 this.o = o;
24 }
25
26 @Override
27 public String toString() {
28 return o.toString();
29 }
30 }
31
32 static class Child extends Parent {
33 public static final Child CHILD = new Child();
34 private static final Object OBJ = new Object();
35
36 private Child() {
37 // This call should not be legal, because at the time that the
38 // call occurs, the static initializers of Child have not yet
39 // finished executing and therefore CHILD and OBJ are not
40 // necessarily initialized and are not necessarily non-null.
41 // :: error: (method.invocation.invalid)
42 super(OBJ);
43 }
44 }
45
46 static class Child2 extends Parent {
47 public static final Child2 CHILD;
48 private static final Object OBJ;
49
50 static {
51 CHILD = new Child2();
52 OBJ = new Object();
53 }
54
55 private Child2() {
56 // This call should not be legal, because at the time that the
57 // call occurs, the static initializers of Child have not yet
58 // finished executing and therefore CHILD and OBJ are not
59 // necessarily initialized and are not necessarily non-null.
60 // :: error: (method.invocation.invalid)
61 super(OBJ);
62 }
63 }
64
65 // Changing the order of the OBJ and CHILD fields prevents a null pointer
66 // exception.
67 static class ChildOk1 extends Parent {
68 private static final Object OBJ = new Object();
69 public static final Child CHILD = new Child();
70
71 private ChildOk1() {
72 // This call is legal, because OBJ is non-null at the time of the
73 // call. That's because OBJ is initialized before CHILD and
74 // therefore before the call to "new Child()".
75 super(OBJ);
76 }
77 }
78
79 // Changing the order of the OBJ and CHILD field assignments prevents a
80 // null pointer exception.
81 static class ChildOk2 extends Parent {
82 public static final ChildOk2 CHILD;
83 private static final Object OBJ;
84
85 static {
86 OBJ = new Object();
87 CHILD = new ChildOk2();
88 }
89
90 private ChildOk2() {
91 // This call is legal, because OBJ is non-null at the time of the
92 // call. That's because OBJ is initialized before CHILD and
93 // therefore before the call to "new Child()".
94 super(OBJ);
95 }
96 }
97
98 public static void main(final String[] args) {
99 System.out.println(Child.CHILD);
100 }
101 }