Debian Codegraph @

test/122-npe/src/Main.java

raw
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import java.lang.ref.WeakReference;
18
19 /**
20 * Test that null pointer exceptions are thrown by the VM.
21 */
22 public class Main {
23 private int f;
24 public static void main(String[] args) {
25 methodOne();
26 }
27
28 static void methodOne() {
29 methodTwo();
30 }
31
32 private int callSpecial() {
33 return f;
34 }
35
36 final int callFinal() {
37 return f;
38 }
39
40 static void methodTwo() {
41 NullPointerException npe = null;
42
43 int thisLine = 43;
44
45 new Object().getClass(); // Ensure compiled.
46 try {
47 ((Object) null).getClass();
48 } catch (NullPointerException e) {
49 npe = e;
50 }
51 check(npe, thisLine += 4);
52
53 new Main().callSpecial(); // Ensure compiled.
54 try {
55 ((Main) null).callSpecial(); // Test invokespecial.
56 } catch (NullPointerException e) {
57 npe = e;
58 }
59 check(npe, thisLine += 8);
60
61 new Main().callFinal(); // Ensure compiled.
62 try {
63 ((Main) null).callFinal(); // Test invokevirtual on final.
64 } catch (NullPointerException e) {
65 npe = e;
66 }
67 check(npe, thisLine += 8);
68
69 try {
70 ((Value) null).objectField.toString();
71 } catch (NullPointerException e) {
72 npe = e;
73 }
74 check(npe, thisLine += 7);
75
76 try {
77 useInt(((Value) null).intField);
78 } catch (NullPointerException e) {
79 npe = e;
80 }
81 check(npe, thisLine += 7);
82
83 try {
84 useFloat(((Value) null).floatField);
85 } catch (NullPointerException e) {
86 npe = e;
87 }
88 check(npe, thisLine += 7);
89
90 try {
91 useLong(((Value) null).longField);
92 } catch (NullPointerException e) {
93 npe = e;
94 }
95 check(npe, thisLine += 7);
96
97 try {
98 useDouble(((Value) null).doubleField);
99 } catch (NullPointerException e) {
100 npe = e;
101 }
102 check(npe, thisLine += 7);
103
104 try {
105 ((Value) null).objectField = "Fisk";
106 } catch (NullPointerException e) {
107 npe = e;
108 }
109 check(npe, thisLine += 7);
110
111 try {
112 ((Value) null).intField = 42;
113 } catch (NullPointerException e) {
114 npe = e;
115 }
116 check(npe, thisLine += 7);
117
118 try {
119 ((Value) null).floatField = 42.0F;
120 } catch (NullPointerException e) {
121 npe = e;
122 }
123 check(npe, thisLine += 7);
124
125 try {
126 ((Value) null).longField = 42L;
127 } catch (NullPointerException e) {
128 npe = e;
129 }
130 check(npe, thisLine += 7);
131
132 try {
133 ((Value) null).doubleField = 42.0d;
134 } catch (NullPointerException e) {
135 npe = e;
136 }
137 check(npe, thisLine += 7);
138
139 try {
140 useInt(((Value) null).byteField);
141 } catch (NullPointerException e) {
142 npe = e;
143 }
144 check(npe, thisLine += 7);
145
146 try {
147 if (((Value) null).booleanField) { }
148 } catch (NullPointerException e) {
149 npe = e;
150 }
151 check(npe, thisLine += 7);
152
153 try {
154 useInt(((Value) null).charField);
155 } catch (NullPointerException e) {
156 npe = e;
157 }
158 check(npe, thisLine += 7);
159
160 try {
161 useInt(((Value) null).shortField);
162 } catch (NullPointerException e) {
163 npe = e;
164 }
165 check(npe, thisLine += 7);
166
167 try {
168 ((Value) null).byteField = 42;
169 } catch (NullPointerException e) {
170 npe = e;
171 }
172 check(npe, thisLine += 7);
173
174 try {
175 ((Value) null).booleanField = true;
176 } catch (NullPointerException e) {
177 npe = e;
178 }
179 check(npe, thisLine += 7);
180
181 try {
182 ((Value) null).charField = '\u0042';
183 } catch (NullPointerException e) {
184 npe = e;
185 }
186 check(npe, thisLine += 7);
187
188 try {
189 ((Value) null).shortField = 42;
190 } catch (NullPointerException e) {
191 npe = e;
192 }
193 check(npe, thisLine += 7);
194
195 try {
196 ((Value) null).volatileObjectField.toString();
197 } catch (NullPointerException e) {
198 npe = e;
199 }
200 check(npe, thisLine += 7);
201
202 try {
203 ((Value) null).volatileObjectField = "Fisk";
204 } catch (NullPointerException e) {
205 npe = e;
206 }
207 check(npe, thisLine += 7);
208
209 try {
210 useInt(((Value) null).volatileIntField);
211 } catch (NullPointerException e) {
212 npe = e;
213 }
214 check(npe, thisLine += 7);
215
216 try {
217 ((Value) null).volatileIntField = 42;
218 } catch (NullPointerException e) {
219 npe = e;
220 }
221 check(npe, thisLine += 7);
222
223 try {
224 useFloat(((Value) null).volatileFloatField);
225 } catch (NullPointerException e) {
226 npe = e;
227 }
228 check(npe, thisLine += 7);
229
230 try {
231 ((Value) null).volatileFloatField = 42.0F;
232 } catch (NullPointerException e) {
233 npe = e;
234 }
235 check(npe, thisLine += 7);
236
237 try {
238 useLong(((Value) null).volatileLongField);
239 } catch (NullPointerException e) {
240 npe = e;
241 }
242 check(npe, thisLine += 7);
243
244 try {
245 ((Value) null).volatileLongField = 42L;
246 } catch (NullPointerException e) {
247 npe = e;
248 }
249 check(npe, thisLine += 7);
250
251 try {
252 useDouble(((Value) null).volatileDoubleField);
253 } catch (NullPointerException e) {
254 npe = e;
255 }
256 check(npe, thisLine += 7);
257
258 try {
259 ((Value) null).volatileDoubleField = 42.0d;
260 } catch (NullPointerException e) {
261 npe = e;
262 }
263 check(npe, thisLine += 7);
264
265 try {
266 useInt(((Value) null).volatileByteField);
267 } catch (NullPointerException e) {
268 npe = e;
269 }
270 check(npe, thisLine += 7);
271
272 try {
273 ((Value) null).volatileByteField = 42;
274 } catch (NullPointerException e) {
275 npe = e;
276 }
277 check(npe, thisLine += 7);
278
279 try {
280 if (((Value) null).volatileBooleanField) { }
281 } catch (NullPointerException e) {
282 npe = e;
283 }
284 check(npe, thisLine += 7);
285
286 try {
287 ((Value) null).volatileBooleanField = true;
288 } catch (NullPointerException e) {
289 npe = e;
290 }
291 check(npe, thisLine += 7);
292
293 try {
294 useInt(((Value) null).volatileCharField);
295 } catch (NullPointerException e) {
296 npe = e;
297 }
298 check(npe, thisLine += 7);
299
300 try {
301 ((Value) null).volatileCharField = '\u0042';
302 } catch (NullPointerException e) {
303 npe = e;
304 }
305 check(npe, thisLine += 7);
306
307 try {
308 useInt(((Value) null).volatileShortField);
309 } catch (NullPointerException e) {
310 npe = e;
311 }
312 check(npe, thisLine += 7);
313
314 try {
315 ((Value) null).volatileShortField = 42;
316 } catch (NullPointerException e) {
317 npe = e;
318 }
319 check(npe, thisLine += 7);
320
321 try {
322 ((Object[]) null)[0].toString();
323 } catch (NullPointerException e) {
324 npe = e;
325 }
326 check(npe, thisLine += 7);
327
328 try {
329 useInt(((int[]) null)[0]);
330 } catch (NullPointerException e) {
331 npe = e;
332 }
333 check(npe, thisLine += 7);
334
335 try {
336 useFloat(((float[]) null)[0]);
337 } catch (NullPointerException e) {
338 npe = e;
339 }
340 check(npe, thisLine += 7);
341
342 try {
343 useLong(((long[]) null)[0]);
344 } catch (NullPointerException e) {
345 npe = e;
346 }
347 check(npe, thisLine += 7);
348
349 try {
350 useDouble(((double[]) null)[0]);
351 } catch (NullPointerException e) {
352 npe = e;
353 }
354 check(npe, thisLine += 7);
355
356 try {
357 ((Object[]) null)[0] = "Fisk";
358 } catch (NullPointerException e) {
359 npe = e;
360 }
361 check(npe, thisLine += 7);
362
363 try {
364 ((int[]) null)[0] = 42;
365 } catch (NullPointerException e) {
366 npe = e;
367 }
368 check(npe, thisLine += 7);
369
370 try {
371 ((float[]) null)[0] = 42.0F;
372 } catch (NullPointerException e) {
373 npe = e;
374 }
375 check(npe, thisLine += 7);
376
377 try {
378 ((long[]) null)[0] = 42L;
379 } catch (NullPointerException e) {
380 npe = e;
381 }
382 check(npe, thisLine += 7);
383
384 try {
385 ((double[]) null)[0] = 42.0d;
386 } catch (NullPointerException e) {
387 npe = e;
388 }
389 check(npe, thisLine += 7);
390
391 try {
392 useInt(((byte[]) null)[0]);
393 } catch (NullPointerException e) {
394 npe = e;
395 }
396 check(npe, thisLine += 7);
397
398 try {
399 if (((boolean[]) null)[0]) { }
400 } catch (NullPointerException e) {
401 npe = e;
402 }
403 check(npe, thisLine += 7);
404
405 try {
406 useInt(((char[]) null)[0]);
407 } catch (NullPointerException e) {
408 npe = e;
409 }
410 check(npe, thisLine += 7);
411
412 try {
413 useInt(((short[]) null)[0]);
414 } catch (NullPointerException e) {
415 npe = e;
416 }
417 check(npe, thisLine += 7);
418
419 try {
420 ((byte[]) null)[0] = 42;
421 } catch (NullPointerException e) {
422 npe = e;
423 }
424 check(npe, thisLine += 7);
425
426 try {
427 ((boolean[]) null)[0] = true;
428 } catch (NullPointerException e) {
429 npe = e;
430 }
431 check(npe, thisLine += 7);
432
433 try {
434 ((char[]) null)[0] = '\u0042';
435 } catch (NullPointerException e) {
436 npe = e;
437 }
438 check(npe, thisLine += 7);
439
440 try {
441 ((short[]) null)[0] = 42;
442 } catch (NullPointerException e) {
443 npe = e;
444 }
445 check(npe, thisLine += 7);
446
447 try {
448 useInt(((Object[]) null).length);
449 } catch (NullPointerException e) {
450 npe = e;
451 }
452 check(npe, thisLine += 7);
453
454 try {
455 useInt(((int[]) null).length);
456 } catch (NullPointerException e) {
457 npe = e;
458 }
459 check(npe, thisLine += 7);
460
461 try {
462 useInt(((float[]) null).length);
463 } catch (NullPointerException e) {
464 npe = e;
465 }
466 check(npe, thisLine += 7);
467
468 try {
469 useInt(((long[]) null).length);
470 } catch (NullPointerException e) {
471 npe = e;
472 }
473 check(npe, thisLine += 7);
474
475 try {
476 useInt(((double[]) null).length);
477 } catch (NullPointerException e) {
478 npe = e;
479 }
480 check(npe, thisLine += 7);
481
482 try {
483 useInt(((byte[]) null).length);
484 } catch (NullPointerException e) {
485 npe = e;
486 }
487 check(npe, thisLine += 7);
488
489 try {
490 useInt(((boolean[]) null).length);
491 } catch (NullPointerException e) {
492 npe = e;
493 }
494 check(npe, thisLine += 7);
495
496 try {
497 useInt(((char[]) null).length);
498 } catch (NullPointerException e) {
499 npe = e;
500 }
501 check(npe, thisLine += 7);
502
503 try {
504 useInt(((short[]) null).length);
505 } catch (NullPointerException e) {
506 npe = e;
507 }
508 check(npe, thisLine += 7);
509
510 try {
511 Interface i = null;
512 i.methodInterface(); // Test null on invokeinterface.
513 } catch (NullPointerException e) {
514 npe = e;
515 }
516 check(npe, thisLine += 8);
517
518 try {
519 Object o = null;
520 o.toString(); // Test null on invokevirtual.
521 } catch (NullPointerException e) {
522 npe = e;
523 }
524 check(npe, thisLine += 8);
525
526 npe = null;
527 try {
528 String s = null;
529 try {
530 throw new AssertionError();
531 } finally {
532 // Cause an implicit NPE.
533 s.getClass();
534 }
535 } catch (NullPointerException e) {
536 npe = e;
537 }
538 check(npe, thisLine += 13);
539
540 npe = null;
541 try {
542 String s = null;
543 try {
544 throw new AssertionError();
545 } catch (AssertionError ex) {
546 }
547 s.getClass();
548 } catch (NullPointerException e) {
549 npe = e;
550 }
551 check(npe, thisLine += 14);
552
553 npe = null;
554 try {
555 useInt(((WeakReference<Object>) null).refersTo(null) ? 1 : 0);
556 } catch (NullPointerException e) {
557 npe = e;
558 }
559 check(npe, thisLine += 8);
560 }
561
562 static void check(NullPointerException npe, int firstLine) {
563 final boolean debug = false;
564 if (debug) {
565 System.out.print("Got to line ");
566 System.out.print(firstLine);
567 System.out.println();
568 }
569 StackTraceElement[] trace = npe.getStackTrace();
570 checkElement(trace[0], "Main", "methodTwo", "Main.java", firstLine);
571 checkElement(trace[1], "Main", "methodOne", "Main.java", 29);
572 checkElement(trace[2], "Main", "main", "Main.java", 25);
573 }
574
575 static void checkElement(StackTraceElement element,
576 String declaringClass, String methodName,
577 String fileName, int lineNumber) {
578 assertEquals(declaringClass, element.getClassName());
579 assertEquals(methodName, element.getMethodName());
580 assertEquals(fileName, element.getFileName());
581 assertEquals(lineNumber, element.getLineNumber());
582 }
583
584 static void assertEquals(Object expected, Object actual) {
585 if (!expected.equals(actual)) {
586 String msg = "Expected \"" + expected + "\" but got \"" + actual + "\"";
587 throw new AssertionError(msg);
588 }
589 }
590
591 static void assertEquals(int expected, int actual) {
592 if (expected != actual) {
593 throw new AssertionError("Expected " + expected + " got " + actual);
594 }
595 }
596
597 interface Interface {
598 void methodInterface();
599 }
600
601 static void useInt(int i) {
602 }
603
604 static void useFloat(float f) {
605 }
606
607 static void useDouble(double d) {
608 }
609
610 static void useLong(long l) {
611 }
612
613 static class Value {
614 Object objectField;
615 int intField;
616 float floatField;
617 long longField;
618 double doubleField;
619 byte byteField;
620 boolean booleanField;
621 char charField;
622 short shortField;
623
624 volatile Object volatileObjectField;
625 volatile int volatileIntField;
626 volatile float volatileFloatField;
627 volatile long volatileLongField;
628 volatile double volatileDoubleField;
629 volatile byte volatileByteField;
630 volatile boolean volatileBooleanField;
631 volatile char volatileCharField;
632 volatile short volatileShortField;
633 }
634 }