|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * Copyright (c) 2019 SAP SE. All rights reserved. |
|
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
5 * |
|
6 * This code is free software; you can redistribute it and/or modify it |
|
7 * under the terms of the GNU General Public License version 2 only, as |
|
8 * published by the Free Software Foundation. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 /** |
|
26 * @test |
|
27 * @summary Test NullPointerException messages thrown in frames that |
|
28 * are hidden in the backtrace/stackTrace. |
|
29 * @bug 8218628 |
|
30 * @library /test/lib |
|
31 * @compile -g NPEInHiddenTopFrameTest.java |
|
32 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-ShowHiddenFrames -XX:+ShowCodeDetailsInExceptionMessages NPEInHiddenTopFrameTest hidden |
|
33 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames -XX:+ShowCodeDetailsInExceptionMessages NPEInHiddenTopFrameTest visible |
|
34 */ |
|
35 |
|
36 import jdk.test.lib.Asserts; |
|
37 |
|
38 public class NPEInHiddenTopFrameTest { |
|
39 |
|
40 @FunctionalInterface |
|
41 private static interface SomeFunctionalInterface { |
|
42 String someMethod(String a, String b); |
|
43 } |
|
44 |
|
45 public static void checkMessage(String expression, |
|
46 String obtainedMsg, String expectedMsg) { |
|
47 System.out.println(); |
|
48 System.out.println(" source code: " + expression); |
|
49 System.out.println(" thrown msg: " + obtainedMsg); |
|
50 if (obtainedMsg == null && expectedMsg == null) return; |
|
51 System.out.println("expected msg: " + expectedMsg); |
|
52 Asserts.assertEquals(expectedMsg, obtainedMsg); |
|
53 } |
|
54 |
|
55 public static void main(String[] args) throws Exception { |
|
56 boolean framesAreHidden = false; |
|
57 if (args.length > 0) { |
|
58 String arg = args[0]; |
|
59 if (arg.equals("hidden")) framesAreHidden = true; |
|
60 } |
|
61 |
|
62 try { |
|
63 final SomeFunctionalInterface concatter = String::concat; |
|
64 final String nullString = null; |
|
65 if (concatter != null) { |
|
66 // This throws NPE from the lambda expression which is a hidden frame. |
|
67 concatter.someMethod(nullString, "validString"); |
|
68 } |
|
69 } catch (NullPointerException e) { |
|
70 checkMessage("concatter.someMethod(nullString, \"validString\");", e.getMessage(), |
|
71 framesAreHidden ? |
|
72 // This is the message that would be printed if the wrong method/bci are used: |
|
73 // "Cannot invoke 'NPEInHiddenTopFrameTest$SomeFunctionalInterface.someMethod(String, String)'" + |
|
74 // " because 'concatter' is null." |
|
75 // But the NPE message generation now recognizes this situation and skips the |
|
76 // message. So we expect null: |
|
77 null : |
|
78 // This is the correct message, but it describes code generated on-the-fly. |
|
79 // You get it if you disable hiding frames (-XX:+ShowHiddenframes). |
|
80 "Cannot invoke \"String.concat(String)\" because \"<parameter1>\" is null" ); |
|
81 e.printStackTrace(); |
|
82 } |
|
83 } |
|
84 } |