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 * @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