< prev index next >

test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/NullPointerExceptionTest.java

Print this page
rev 56281 : 8218628: Add detailed message to NullPointerException describing what is null.
Summary: This is the implementation of JEP 358: Helpful NullPointerExceptions.
Reviewed-by: coleenp
rev 56283 : [mq]: fixes_review_of_16.patch


  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 extended NullPointerException message for
  28  *   classfiles generated with debug information. In this case the name
  29  *   of the variable containing the array is printed.

  30  * @modules java.base/java.lang:open
  31  *          java.base/jdk.internal.org.objectweb.asm
  32  * @library /test/lib
  33  * @compile -g NullPointerExceptionTest.java
  34  * @run main/othervm -XX:MaxJavaStackTraceDepth=1 -XX:+ShowCodeDetailsInExceptionMessages NullPointerExceptionTest hasDebugInfo
  35  */
  36 /**
  37  * @test
  38  * @summary Test extended NullPointerException message for class
  39  *   files generated without debugging information. The message lists
  40  *   detailed information about the entity that is null.

  41  * @modules java.base/java.lang:open
  42  *          java.base/jdk.internal.org.objectweb.asm
  43  * @library /test/lib
  44  * @compile NullPointerExceptionTest.java
  45  * @run main/othervm -XX:MaxJavaStackTraceDepth=1 -XX:+ShowCodeDetailsInExceptionMessages NullPointerExceptionTest
  46  */
  47 
  48 import java.io.ByteArrayInputStream;
  49 import java.io.ByteArrayOutputStream;
  50 import java.io.ObjectInputStream;
  51 import java.io.ObjectOutputStream;
  52 import java.util.ArrayList;
  53 
  54 import jdk.test.lib.Asserts;
  55 
  56 import java.lang.reflect.*;
  57 import java.lang.invoke.MethodHandles.Lookup;
  58 import static java.lang.invoke.MethodHandles.*;
  59 import static java.lang.invoke.MethodHandles.Lookup.*;
  60 


  70 public class NullPointerExceptionTest {
  71 
  72     // Some fields used in the test.
  73     static Object nullStaticField;
  74     NullPointerExceptionTest nullInstanceField;
  75     static int[][][][] staticArray;
  76     static long[][] staticLongArray = new long[1000][];
  77     DoubleArrayGen dag;
  78     ArrayList<String> names = new ArrayList<>();
  79     ArrayList<String> curr;
  80     static boolean hasDebugInfo = false;
  81 
  82     static {
  83         staticArray       = new int[1][][][];
  84         staticArray[0]    = new int[1][][];
  85         staticArray[0][0] = new int[1][];
  86     }
  87 
  88     public static void checkMessage(Throwable t, String expression,
  89                                     String obtainedMsg, String expectedMsg) {
  90         //System.out.println("  thrown msg: " + obtainedMsg);
  91         System.out.println("\nSource code:\n  " + expression + "\n\nOutput:");
  92         t.printStackTrace(System.out);
  93         if (obtainedMsg != expectedMsg && // E.g. both are null.
  94             !obtainedMsg.equals(expectedMsg)) {
  95             // GL: need to fix the test cases!
  96             System.out.println("expected msg: " + expectedMsg);
  97             Asserts.assertEquals(expectedMsg, obtainedMsg);
  98         }
  99         System.out.println("\n----");
 100     }
 101 
 102     public static void main(String[] args) throws Exception {
 103         NullPointerExceptionTest t = new NullPointerExceptionTest();
 104         if (args.length > 0) {
 105             hasDebugInfo = true;
 106         }
 107 
 108         System.out.println("Tests for the first part of the message:");
 109         System.out.println("========================================\n");
 110 
 111         // Test the message printed for the failed action.
 112         t.testFailedAction();
 113 
 114         System.out.println("Tests for the second part of the message:");
 115         System.out.println("=========================================\n");




  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 extended NullPointerException message for
  28  *   classfiles generated with debug information. In this case the name
  29  *   of the variable containing the array is printed.
  30  * @bug 8218628
  31  * @modules java.base/java.lang:open
  32  *          java.base/jdk.internal.org.objectweb.asm
  33  * @library /test/lib
  34  * @compile -g NullPointerExceptionTest.java
  35  * @run main/othervm -XX:MaxJavaStackTraceDepth=1 -XX:+ShowCodeDetailsInExceptionMessages NullPointerExceptionTest hasDebugInfo
  36  */
  37 /**
  38  * @test
  39  * @summary Test extended NullPointerException message for class
  40  *   files generated without debugging information. The message lists
  41  *   detailed information about the entity that is null.
  42  * @bug 8218628
  43  * @modules java.base/java.lang:open
  44  *          java.base/jdk.internal.org.objectweb.asm
  45  * @library /test/lib
  46  * @compile NullPointerExceptionTest.java
  47  * @run main/othervm -XX:MaxJavaStackTraceDepth=1 -XX:+ShowCodeDetailsInExceptionMessages NullPointerExceptionTest
  48  */
  49 
  50 import java.io.ByteArrayInputStream;
  51 import java.io.ByteArrayOutputStream;
  52 import java.io.ObjectInputStream;
  53 import java.io.ObjectOutputStream;
  54 import java.util.ArrayList;
  55 
  56 import jdk.test.lib.Asserts;
  57 
  58 import java.lang.reflect.*;
  59 import java.lang.invoke.MethodHandles.Lookup;
  60 import static java.lang.invoke.MethodHandles.*;
  61 import static java.lang.invoke.MethodHandles.Lookup.*;
  62 


  72 public class NullPointerExceptionTest {
  73 
  74     // Some fields used in the test.
  75     static Object nullStaticField;
  76     NullPointerExceptionTest nullInstanceField;
  77     static int[][][][] staticArray;
  78     static long[][] staticLongArray = new long[1000][];
  79     DoubleArrayGen dag;
  80     ArrayList<String> names = new ArrayList<>();
  81     ArrayList<String> curr;
  82     static boolean hasDebugInfo = false;
  83 
  84     static {
  85         staticArray       = new int[1][][][];
  86         staticArray[0]    = new int[1][][];
  87         staticArray[0][0] = new int[1][];
  88     }
  89 
  90     public static void checkMessage(Throwable t, String expression,
  91                                     String obtainedMsg, String expectedMsg) {

  92         System.out.println("\nSource code:\n  " + expression + "\n\nOutput:");
  93         t.printStackTrace(System.out);
  94         if (obtainedMsg != expectedMsg && // E.g. both are null.
  95             !obtainedMsg.equals(expectedMsg)) {

  96             System.out.println("expected msg: " + expectedMsg);
  97             Asserts.assertEquals(expectedMsg, obtainedMsg);
  98         }
  99         System.out.println("\n----");
 100     }
 101 
 102     public static void main(String[] args) throws Exception {
 103         NullPointerExceptionTest t = new NullPointerExceptionTest();
 104         if (args.length > 0) {
 105             hasDebugInfo = true;
 106         }
 107 
 108         System.out.println("Tests for the first part of the message:");
 109         System.out.println("========================================\n");
 110 
 111         // Test the message printed for the failed action.
 112         t.testFailedAction();
 113 
 114         System.out.println("Tests for the second part of the message:");
 115         System.out.println("=========================================\n");


< prev index next >