Debian Codegraph @

checker/tests/nullness/init/RawTypesInit.java

raw
1 // Note that this file is a near duplicate in /nullness and /nullness-uninit
2
3 import org.checkerframework.checker.initialization.qual.UnknownInitialization;
4 import org.checkerframework.checker.nullness.qual.*;
5 import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
6 import org.checkerframework.checker.nullness.qual.RequiresNonNull;
7
8 class RawTypesInit {
9
10 class Bad {
11 @NonNull String field;
12
13 public Bad() {
14 // :: error: (method.invocation.invalid)
15 this.init(); // error
16 // :: error: (method.invocation.invalid)
17 init(); // error
18
19 this.field = "field"; // valid
20 // :: error: (assignment.type.incompatible)
21 this.field = null; // error
22 field = "field"; // valid
23 // :: error: (assignment.type.incompatible)
24 field = null; // error
25 }
26
27 void init() {
28 output(this.field.length()); // valid
29 }
30 }
31
32 class A {
33 @NonNull String field;
34
35 public A() {
36 this.field = "field"; // valid
37 field = "field"; // valid
38 this.init(); // valid
39 init(); // valid
40 }
41
42 public void init(@UnknownInitialization A this) {
43 // :: error: (dereference.of.nullable)
44 output(this.field.length());
45 }
46
47 public void initExpl2(@UnknownInitialization A this) {
48 // :: error: (argument.type.incompatible)
49 output(this.field);
50 }
51
52 public void initImpl1(@UnknownInitialization A this) {
53 // :: error: (dereference.of.nullable)
54 output(field.length());
55 }
56
57 public void initImpl2(@UnknownInitialization A this) {
58 // :: error: (argument.type.incompatible)
59 output(field);
60 }
61 }
62
63 class B extends A {
64 @NonNull String otherField;
65
66 public B() {
67 super();
68 // :: error: (assignment.type.incompatible)
69 this.otherField = null; // error
70 this.otherField = "otherField"; // valid
71 }
72
73 @Override
74 public void init(@UnknownInitialization B this) {
75 // :: error: (dereference.of.nullable)
76 output(this.field.length()); // error (TODO: substitution)
77 super.init(); // valid
78 }
79
80 public void initImpl1(@UnknownInitialization B this) {
81 // :: error: (dereference.of.nullable)
82 output(field.length()); // error (TODO: substitution)
83 }
84
85 public void initExpl2(@UnknownInitialization B this) {
86 // :: error: (dereference.of.nullable)
87 output(this.otherField.length()); // error
88 }
89
90 public void initImpl2(@UnknownInitialization B this) {
91 // :: error: (dereference.of.nullable)
92 output(otherField.length()); // error
93 }
94
95 void other() {
96 init(); // valid
97 this.init(); // valid
98 }
99
100 void otherRaw(@UnknownInitialization B this) {
101 init(); // valid
102 this.init(); // valid
103 }
104 }
105
106 // :: error: (initialization.fields.uninitialized)
107 class C extends B {
108
109 @NonNull String[] strings;
110
111 @Override
112 public void init(@UnknownInitialization C this) {
113 // :: error: (dereference.of.nullable)
114 output(this.strings.length); // error
115 System.out.println(); // valid
116 }
117 }
118
119 // To test whether the argument is @NonNull and @Initialized
120 static void output(@NonNull Object o) {}
121
122 class D extends C {
123 @Override
124 public void init(@UnknownInitialization D this) {
125 this.field = "s";
126 output(this.field.length());
127 }
128 }
129
130 class MyTest {
131 Integer i;
132
133 MyTest(int i) {
134 this.i = i;
135 }
136
137 void myTest(@UnknownInitialization MyTest this) {
138 // :: error: (unboxing.of.nullable)
139 i = i + 1;
140 }
141 }
142
143 class AllFieldsInitialized {
144 Integer elapsedMillis = 0;
145 Integer startTime = 0;
146
147 public AllFieldsInitialized() {
148 // :: error: (method.invocation.invalid)
149 nonRawMethod();
150 }
151
152 public void nonRawMethod() {}
153 }
154
155 // :: error: (initialization.fields.uninitialized)
156 class AFSIICell {
157 AllFieldsSetInInitializer afsii;
158 }
159
160 class AllFieldsSetInInitializer {
161 Integer elapsedMillis;
162 Integer startTime;
163
164 public AllFieldsSetInInitializer() {
165 elapsedMillis = 0;
166 // :: error: (method.invocation.invalid)
167 nonRawMethod(); // error
168 startTime = 0;
169 // :: error: (method.invocation.invalid)
170 nonRawMethod(); // error
171 // :: error: (initialization.invalid.field.write.initialized)
172 new AFSIICell().afsii = this;
173 }
174
175 // :: error: (initialization.fields.uninitialized)
176 public AllFieldsSetInInitializer(boolean b) {
177 // :: error: (method.invocation.invalid)
178 nonRawMethod(); // error
179 }
180
181 public void nonRawMethod() {}
182 }
183
184 class ConstructorInvocations {
185 Integer v;
186
187 public ConstructorInvocations(int v) {
188 this.v = v;
189 }
190
191 public ConstructorInvocations() {
192 this(0);
193 // :: error: (method.invocation.invalid)
194 nonRawMethod(); // invalid
195 }
196
197 public void nonRawMethod() {}
198 }
199
200 class MethodAccess {
201 public MethodAccess() {
202 @NonNull String s = string();
203 }
204
205 public @NonNull String string(@UnknownInitialization MethodAccess this) {
206 return "nonnull";
207 }
208 }
209
210 void cast(@UnknownInitialization Object... args) {
211
212 @SuppressWarnings("rawtypes")
213 // :: error: (assignment.type.incompatible)
214 Object[] argsNonRaw1 = args;
215
216 @SuppressWarnings("cast")
217 Object[] argsNonRaw2 = (Object[]) args;
218 }
219
220 class RawAfterConstructorBad {
221 Object o;
222 // :: error: (initialization.fields.uninitialized)
223 RawAfterConstructorBad() {}
224 }
225
226 class RawAfterConstructorOK1 {
227 @Nullable Object o;
228
229 RawAfterConstructorOK1() {}
230 }
231
232 class RawAfterConstructorOK2 {
233 Integer a;
234 // :: error: (initialization.fields.uninitialized)
235 RawAfterConstructorOK2() {}
236 }
237
238 // TODO: reinstate. This shows desired features, for initialization in
239 // a helper method rather than in the constructor.
240 class InitInHelperMethod {
241 Integer a;
242 Integer b;
243
244 InitInHelperMethod(short constructor_inits_ab) {
245 a = 1;
246 b = 1;
247 // :: error: (method.invocation.invalid)
248 nonRawMethod();
249 }
250
251 InitInHelperMethod(boolean constructor_inits_a) {
252 a = 1;
253 init_b();
254 // :: error: (method.invocation.invalid)
255 nonRawMethod();
256 }
257
258 @RequiresNonNull("a")
259 @EnsuresNonNull("b")
260 void init_b(@UnknownInitialization InitInHelperMethod this) {
261 b = 2;
262 // :: error: (method.invocation.invalid)
263 nonRawMethod();
264 }
265
266 InitInHelperMethod(int constructor_inits_none) {
267 init_ab();
268 // :: error: (method.invocation.invalid)
269 nonRawMethod();
270 }
271
272 @EnsuresNonNull({"a", "b"})
273 void init_ab(@UnknownInitialization InitInHelperMethod this) {
274 a = 1;
275 b = 2;
276 // :: error: (method.invocation.invalid)
277 nonRawMethod();
278 }
279
280 void nonRawMethod() {}
281 }
282 }