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