Nicolas Geoffray | 5f16c88 | 2014-09-11 18:46:01 +0100 | [diff] [blame] | 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 | /** |
| 18 | * Test that null pointer exceptions are thrown by the VM. |
| 19 | */ |
| 20 | public class Main { |
| 21 | private int f; |
| 22 | public static void main(String[] args) { |
| 23 | methodOne(); |
| 24 | } |
| 25 | |
| 26 | static void methodOne() { |
| 27 | methodTwo(); |
| 28 | } |
| 29 | |
| 30 | private int callSpecial() { |
| 31 | return f; |
| 32 | } |
| 33 | |
| 34 | final int callFinal() { |
| 35 | return f; |
| 36 | } |
| 37 | |
| 38 | static void methodTwo() { |
| 39 | NullPointerException npe = null; |
| 40 | |
| 41 | int thisLine = 41; |
| 42 | |
| 43 | new Object().getClass(); // Ensure compiled. |
| 44 | try { |
| 45 | ((Object) null).getClass(); |
| 46 | } catch (NullPointerException e) { |
| 47 | npe = e; |
| 48 | } |
| 49 | check(npe, thisLine += 4); |
| 50 | |
| 51 | new Main().callSpecial(); // Ensure compiled. |
| 52 | try { |
| 53 | ((Main) null).callSpecial(); // Test invokespecial. |
| 54 | } catch (NullPointerException e) { |
| 55 | npe = e; |
| 56 | } |
| 57 | check(npe, thisLine += 8); |
| 58 | |
| 59 | new Main().callFinal(); // Ensure compiled. |
| 60 | try { |
| 61 | ((Main) null).callFinal(); // Test invokevirtual on final. |
| 62 | } catch (NullPointerException e) { |
| 63 | npe = e; |
| 64 | } |
| 65 | check(npe, thisLine += 8); |
| 66 | |
| 67 | try { |
| 68 | ((Value) null).objectField.toString(); |
| 69 | } catch (NullPointerException e) { |
| 70 | npe = e; |
| 71 | } |
| 72 | check(npe, thisLine += 7); |
| 73 | |
| 74 | try { |
| 75 | useInt(((Value) null).intField); |
| 76 | } catch (NullPointerException e) { |
| 77 | npe = e; |
| 78 | } |
| 79 | check(npe, thisLine += 7); |
| 80 | |
| 81 | try { |
| 82 | useFloat(((Value) null).floatField); |
| 83 | } catch (NullPointerException e) { |
| 84 | npe = e; |
| 85 | } |
| 86 | check(npe, thisLine += 7); |
| 87 | |
| 88 | try { |
| 89 | useLong(((Value) null).longField); |
| 90 | } catch (NullPointerException e) { |
| 91 | npe = e; |
| 92 | } |
| 93 | check(npe, thisLine += 7); |
| 94 | |
| 95 | try { |
| 96 | useDouble(((Value) null).doubleField); |
| 97 | } catch (NullPointerException e) { |
| 98 | npe = e; |
| 99 | } |
| 100 | check(npe, thisLine += 7); |
| 101 | |
| 102 | try { |
| 103 | ((Value) null).objectField = "Fisk"; |
| 104 | } catch (NullPointerException e) { |
| 105 | npe = e; |
| 106 | } |
| 107 | check(npe, thisLine += 7); |
| 108 | |
| 109 | try { |
| 110 | ((Value) null).intField = 42; |
| 111 | } catch (NullPointerException e) { |
| 112 | npe = e; |
| 113 | } |
| 114 | check(npe, thisLine += 7); |
| 115 | |
| 116 | try { |
| 117 | ((Value) null).floatField = 42.0F; |
| 118 | } catch (NullPointerException e) { |
| 119 | npe = e; |
| 120 | } |
| 121 | check(npe, thisLine += 7); |
| 122 | |
| 123 | try { |
| 124 | ((Value) null).longField = 42L; |
| 125 | } catch (NullPointerException e) { |
| 126 | npe = e; |
| 127 | } |
| 128 | check(npe, thisLine += 7); |
| 129 | |
| 130 | try { |
| 131 | ((Value) null).doubleField = 42.0d; |
| 132 | } catch (NullPointerException e) { |
| 133 | npe = e; |
| 134 | } |
| 135 | check(npe, thisLine += 7); |
| 136 | |
| 137 | try { |
| 138 | useInt(((Value) null).byteField); |
| 139 | } catch (NullPointerException e) { |
| 140 | npe = e; |
| 141 | } |
| 142 | check(npe, thisLine += 7); |
| 143 | |
| 144 | try { |
| 145 | if (((Value) null).booleanField) { } |
| 146 | } catch (NullPointerException e) { |
| 147 | npe = e; |
| 148 | } |
| 149 | check(npe, thisLine += 7); |
| 150 | |
| 151 | try { |
| 152 | useInt(((Value) null).charField); |
| 153 | } catch (NullPointerException e) { |
| 154 | npe = e; |
| 155 | } |
| 156 | check(npe, thisLine += 7); |
| 157 | |
| 158 | try { |
| 159 | useInt(((Value) null).shortField); |
| 160 | } catch (NullPointerException e) { |
| 161 | npe = e; |
| 162 | } |
| 163 | check(npe, thisLine += 7); |
| 164 | |
| 165 | try { |
| 166 | ((Value) null).byteField = 42; |
| 167 | } catch (NullPointerException e) { |
| 168 | npe = e; |
| 169 | } |
| 170 | check(npe, thisLine += 7); |
| 171 | |
| 172 | try { |
| 173 | ((Value) null).booleanField = true; |
| 174 | } catch (NullPointerException e) { |
| 175 | npe = e; |
| 176 | } |
| 177 | check(npe, thisLine += 7); |
| 178 | |
| 179 | try { |
| 180 | ((Value) null).charField = '\u0042'; |
| 181 | } catch (NullPointerException e) { |
| 182 | npe = e; |
| 183 | } |
| 184 | check(npe, thisLine += 7); |
| 185 | |
| 186 | try { |
| 187 | ((Value) null).shortField = 42; |
| 188 | } catch (NullPointerException e) { |
| 189 | npe = e; |
| 190 | } |
| 191 | check(npe, thisLine += 7); |
| 192 | |
| 193 | try { |
| 194 | ((Object[]) null)[0].toString(); |
| 195 | } catch (NullPointerException e) { |
| 196 | npe = e; |
| 197 | } |
| 198 | check(npe, thisLine += 7); |
| 199 | |
| 200 | try { |
| 201 | useInt(((int[]) null)[0]); |
| 202 | } catch (NullPointerException e) { |
| 203 | npe = e; |
| 204 | } |
| 205 | check(npe, thisLine += 7); |
| 206 | |
| 207 | try { |
| 208 | useFloat(((float[]) null)[0]); |
| 209 | } catch (NullPointerException e) { |
| 210 | npe = e; |
| 211 | } |
| 212 | check(npe, thisLine += 7); |
| 213 | |
| 214 | try { |
| 215 | useLong(((long[]) null)[0]); |
| 216 | } catch (NullPointerException e) { |
| 217 | npe = e; |
| 218 | } |
| 219 | check(npe, thisLine += 7); |
| 220 | |
| 221 | try { |
| 222 | useDouble(((double[]) null)[0]); |
| 223 | } catch (NullPointerException e) { |
| 224 | npe = e; |
| 225 | } |
| 226 | check(npe, thisLine += 7); |
| 227 | |
| 228 | try { |
| 229 | ((Object[]) null)[0] = "Fisk"; |
| 230 | } catch (NullPointerException e) { |
| 231 | npe = e; |
| 232 | } |
| 233 | check(npe, thisLine += 7); |
| 234 | |
| 235 | try { |
| 236 | ((int[]) null)[0] = 42; |
| 237 | } catch (NullPointerException e) { |
| 238 | npe = e; |
| 239 | } |
| 240 | check(npe, thisLine += 7); |
| 241 | |
| 242 | try { |
| 243 | ((float[]) null)[0] = 42.0F; |
| 244 | } catch (NullPointerException e) { |
| 245 | npe = e; |
| 246 | } |
| 247 | check(npe, thisLine += 7); |
| 248 | |
| 249 | try { |
| 250 | ((long[]) null)[0] = 42L; |
| 251 | } catch (NullPointerException e) { |
| 252 | npe = e; |
| 253 | } |
| 254 | check(npe, thisLine += 7); |
| 255 | |
| 256 | try { |
| 257 | ((double[]) null)[0] = 42.0d; |
| 258 | } catch (NullPointerException e) { |
| 259 | npe = e; |
| 260 | } |
| 261 | check(npe, thisLine += 7); |
| 262 | |
| 263 | try { |
| 264 | useInt(((byte[]) null)[0]); |
| 265 | } catch (NullPointerException e) { |
| 266 | npe = e; |
| 267 | } |
| 268 | check(npe, thisLine += 7); |
| 269 | |
| 270 | try { |
| 271 | if (((boolean[]) null)[0]) { } |
| 272 | } catch (NullPointerException e) { |
| 273 | npe = e; |
| 274 | } |
| 275 | check(npe, thisLine += 7); |
| 276 | |
| 277 | try { |
| 278 | useInt(((char[]) null)[0]); |
| 279 | } catch (NullPointerException e) { |
| 280 | npe = e; |
| 281 | } |
| 282 | check(npe, thisLine += 7); |
| 283 | |
| 284 | try { |
| 285 | useInt(((short[]) null)[0]); |
| 286 | } catch (NullPointerException e) { |
| 287 | npe = e; |
| 288 | } |
| 289 | check(npe, thisLine += 7); |
| 290 | |
| 291 | try { |
| 292 | ((byte[]) null)[0] = 42; |
| 293 | } catch (NullPointerException e) { |
| 294 | npe = e; |
| 295 | } |
| 296 | check(npe, thisLine += 7); |
| 297 | |
| 298 | try { |
| 299 | ((boolean[]) null)[0] = true; |
| 300 | } catch (NullPointerException e) { |
| 301 | npe = e; |
| 302 | } |
| 303 | check(npe, thisLine += 7); |
| 304 | |
| 305 | try { |
| 306 | ((char[]) null)[0] = '\u0042'; |
| 307 | } catch (NullPointerException e) { |
| 308 | npe = e; |
| 309 | } |
| 310 | check(npe, thisLine += 7); |
| 311 | |
| 312 | try { |
| 313 | ((short[]) null)[0] = 42; |
| 314 | } catch (NullPointerException e) { |
| 315 | npe = e; |
| 316 | } |
| 317 | check(npe, thisLine += 7); |
| 318 | |
| 319 | try { |
| 320 | useInt(((Object[]) null).length); |
| 321 | } catch (NullPointerException e) { |
| 322 | npe = e; |
| 323 | } |
| 324 | check(npe, thisLine += 7); |
| 325 | |
| 326 | try { |
| 327 | useInt(((int[]) null).length); |
| 328 | } catch (NullPointerException e) { |
| 329 | npe = e; |
| 330 | } |
| 331 | check(npe, thisLine += 7); |
| 332 | |
| 333 | try { |
| 334 | useInt(((float[]) null).length); |
| 335 | } catch (NullPointerException e) { |
| 336 | npe = e; |
| 337 | } |
| 338 | check(npe, thisLine += 7); |
| 339 | |
| 340 | try { |
| 341 | useInt(((long[]) null).length); |
| 342 | } catch (NullPointerException e) { |
| 343 | npe = e; |
| 344 | } |
| 345 | check(npe, thisLine += 7); |
| 346 | |
| 347 | try { |
| 348 | useInt(((double[]) null).length); |
| 349 | } catch (NullPointerException e) { |
| 350 | npe = e; |
| 351 | } |
| 352 | check(npe, thisLine += 7); |
| 353 | |
| 354 | try { |
| 355 | useInt(((byte[]) null).length); |
| 356 | } catch (NullPointerException e) { |
| 357 | npe = e; |
| 358 | } |
| 359 | check(npe, thisLine += 7); |
| 360 | |
| 361 | try { |
| 362 | useInt(((boolean[]) null).length); |
| 363 | } catch (NullPointerException e) { |
| 364 | npe = e; |
| 365 | } |
| 366 | check(npe, thisLine += 7); |
| 367 | |
| 368 | try { |
| 369 | useInt(((char[]) null).length); |
| 370 | } catch (NullPointerException e) { |
| 371 | npe = e; |
| 372 | } |
| 373 | check(npe, thisLine += 7); |
| 374 | |
| 375 | try { |
| 376 | useInt(((short[]) null).length); |
| 377 | } catch (NullPointerException e) { |
| 378 | npe = e; |
| 379 | } |
| 380 | check(npe, thisLine += 7); |
| 381 | |
| 382 | try { |
| 383 | Interface i = null; |
| 384 | i.methodInterface(); // Test null on invokeinterface. |
| 385 | } catch (NullPointerException e) { |
| 386 | npe = e; |
| 387 | } |
| 388 | check(npe, thisLine += 8); |
| 389 | |
| 390 | try { |
| 391 | Object o = null; |
| 392 | o.toString(); // Test null on invokevirtual. |
| 393 | } catch (NullPointerException e) { |
| 394 | npe = e; |
| 395 | } |
| 396 | check(npe, thisLine += 8); |
| 397 | |
| 398 | npe = null; |
| 399 | try { |
| 400 | String s = null; |
| 401 | try { |
| 402 | throw new AssertionError(); |
| 403 | } finally { |
| 404 | // Cause an implicit NPE. |
| 405 | s.getClass(); |
| 406 | } |
| 407 | } catch (NullPointerException e) { |
| 408 | npe = e; |
| 409 | } |
| 410 | check(npe, thisLine += 13); |
| 411 | |
| 412 | npe = null; |
| 413 | try { |
| 414 | String s = null; |
| 415 | try { |
| 416 | throw new AssertionError(); |
| 417 | } catch (AssertionError ex) { |
| 418 | } |
| 419 | s.getClass(); |
| 420 | } catch (NullPointerException e) { |
| 421 | npe = e; |
| 422 | } |
| 423 | check(npe, thisLine += 14); |
| 424 | } |
| 425 | |
| 426 | static void check(NullPointerException npe, int firstLine) { |
| 427 | final boolean debug = false; |
| 428 | if (debug) { |
| 429 | System.out.print("Got to line "); |
| 430 | System.out.print(firstLine); |
| 431 | System.out.println(); |
| 432 | } |
| 433 | StackTraceElement[] trace = npe.getStackTrace(); |
| 434 | checkElement(trace[0], "Main", "methodTwo", "Main.java", firstLine); |
| 435 | checkElement(trace[1], "Main", "methodOne", "Main.java", 27); |
| 436 | checkElement(trace[2], "Main", "main", "Main.java", 23); |
| 437 | } |
| 438 | |
| 439 | static void checkElement(StackTraceElement element, |
| 440 | String declaringClass, String methodName, |
| 441 | String fileName, int lineNumber) { |
| 442 | assertEquals(declaringClass, element.getClassName()); |
| 443 | assertEquals(methodName, element.getMethodName()); |
| 444 | assertEquals(fileName, element.getFileName()); |
| 445 | assertEquals(lineNumber, element.getLineNumber()); |
| 446 | } |
| 447 | |
| 448 | static void assertEquals(Object expected, Object actual) { |
| 449 | if (!expected.equals(actual)) { |
| 450 | String msg = "Expected \"" + expected + "\" but got \"" + actual + "\""; |
| 451 | throw new AssertionError(msg); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static void assertEquals(int expected, int actual) { |
| 456 | if (expected != actual) { |
| 457 | throw new AssertionError("Expected " + expected + " got " + actual); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | interface Interface { |
| 462 | void methodInterface(); |
| 463 | } |
| 464 | |
| 465 | static void useInt(int i) { |
| 466 | } |
| 467 | |
| 468 | static void useFloat(float f) { |
| 469 | } |
| 470 | |
| 471 | static void useDouble(double d) { |
| 472 | } |
| 473 | |
| 474 | static void useLong(long l) { |
| 475 | } |
| 476 | |
| 477 | static class Value { |
| 478 | Object objectField; |
| 479 | int intField; |
| 480 | float floatField; long longField; |
| 481 | double doubleField; |
| 482 | byte byteField; |
| 483 | boolean booleanField; |
| 484 | char charField; |
| 485 | short shortField; |
| 486 | } |
| 487 | } |