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 * @library /test/lib
30 * @compile -g NPEInHiddenTopFrameTest.java
31 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-ShowHiddenFrames -XX:-ShowCodeDetailsInExceptionMessages NPEInHiddenTopFrameTest hidden
32 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames -XX:+ShowCodeDetailsInExceptionMessages NPEInHiddenTopFrameTest visible
33 */
34
35 import jdk.test.lib.Asserts;
36
37 public class NPEInHiddenTopFrameTest {
38
39 @FunctionalInterface
40 private static interface SomeFunctionalInterface {
41 String someMethod(String a, String b);
42 }
43
44 public static void checkMessage(String expression,
45 String obtainedMsg, String expectedMsg) {
46 System.out.println();
47 System.out.println(" source code: " + expression);
48 System.out.println(" thrown msg: " + obtainedMsg);
49 if (obtainedMsg == null && expectedMsg == null) return;
50 System.out.println("expected msg: " + expectedMsg);
51 Asserts.assertEquals(expectedMsg, obtainedMsg);
52 }
53
54 public static void main(String[] args) throws Exception {
55 boolean framesAreHidden = false;
56 if (args.length > 0) {
57 String arg = args[0];
58 if (arg.equals("hidden")) framesAreHidden = true;
59 }
60
61 try {
62 final SomeFunctionalInterface concatter = String::concat;
63 final String nullString = null;
64 if (concatter != null) {
65 // This throws NPE from the lambda expression which is a hidden frame.
66 concatter.someMethod(nullString, "validString");
67 }
68 } catch (NullPointerException e) {
69 checkMessage("concatter.someMethod(nullString, \"validString\");", e.getMessage(),
70 framesAreHidden ?
71 // This is the message printed if the wrong method/bci are used:
72 // "Cannot invoke 'NPEInHiddenTopFrameTest$SomeFunctionalInterface.someMethod(String, String)'" +
73 // " because 'concatter' is null."
74 // With this fix no message is printed.
75 null :
76 // This is the correct message, but it describes code generated on-the-fly.
77 // You get it if you disable hiding frames (-XX:+ShowHiddenframes).
78 "Cannot invoke 'String.concat(String)' because '<parameter1>' is null." );
79 e.printStackTrace();
80 }
81 }
82 }
83
|
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 }
85
|