< prev index next >

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

Print this page
rev 56326 : 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
rev 56327 : [mq]: find_local_stores.patch

@@ -60,13 +60,18 @@
 import jdk.test.lib.Asserts;
 
 import static java.lang.invoke.MethodHandles.lookup;
 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER;
+import static jdk.internal.org.objectweb.asm.Opcodes.ACONST_NULL;
 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
+import static jdk.internal.org.objectweb.asm.Opcodes.ASTORE;
 import static jdk.internal.org.objectweb.asm.Opcodes.GETFIELD;
+import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC;
+import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1;
 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static jdk.internal.org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
 import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN;
 import static jdk.internal.org.objectweb.asm.Opcodes.RETURN;
 
 /**
  * Tests NullPointerExceptions

@@ -1421,10 +1426,20 @@
     // Generates:
     // class E implements E0 {
     //     public int throwNPE(F f) {
     //         return f.i;
     //     }
+    //     public void throwNPE_reuseStackSlot1(String s1) {
+    //         System.out.println(s1.substring(1));
+    //         String s1_2 = null; // Reuses slot 1.
+    //         System.out.println(s1_2.substring(1));
+    //     }
+    //     public void throwNPE_reuseStackSlot4(String s1, String s2, String s3, String s4) {
+    //         System.out.println(s4.substring(1));
+    //         String s4_2 = null;  // Reuses slot 4.
+    //         System.out.println(s4_2.substring(1));
+    //     }
     // }
     static byte[] generateTestClass() {
         ClassWriter cw = new ClassWriter(0);
         MethodVisitor mv;
 

@@ -1454,15 +1469,90 @@
             mv.visitLocalVariable("this", "LE;", null, label0, label1, 0);
             mv.visitLocalVariable("f", "LE;", null, label0, label1, 1);
             mv.visitMaxs(1, 2);
             mv.visitEnd();
         }
+
+        {
+            mv = cw.visitMethod(ACC_PUBLIC, "throwNPE_reuseStackSlot1", "(Ljava/lang/String;)V", null, null);
+            mv.visitCode();
+            Label label0 = new Label();
+            mv.visitLabel(label0);
+            mv.visitLineNumber(7, label0);
+            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
+            mv.visitVarInsn(ALOAD, 1);
+            mv.visitInsn(ICONST_1);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
+            Label label1 = new Label();
+            mv.visitLabel(label1);
+            mv.visitLineNumber(8, label1);
+            mv.visitInsn(ACONST_NULL);
+            mv.visitVarInsn(ASTORE, 1);
+            Label label2 = new Label();
+            mv.visitLabel(label2);
+            mv.visitLineNumber(9, label2);
+            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
+            mv.visitVarInsn(ALOAD, 1);
+            mv.visitInsn(ICONST_1);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
+            Label label3 = new Label();
+            mv.visitLabel(label3);
+            mv.visitLineNumber(10, label3);
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(3, 3);
+            mv.visitEnd();
+        }
+
+        {
+            mv = cw.visitMethod(ACC_PUBLIC, "throwNPE_reuseStackSlot4", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", null, null);
+            mv.visitCode();
+            Label label0 = new Label();
+            mv.visitLabel(label0);
+            mv.visitLineNumber(12, label0);
+            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
+            mv.visitVarInsn(ALOAD, 4);
+            mv.visitInsn(ICONST_1);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
+            Label label1 = new Label();
+            mv.visitLabel(label1);
+            mv.visitLineNumber(13, label1);
+            mv.visitInsn(ACONST_NULL);
+            mv.visitVarInsn(ASTORE, 4);
+            Label label2 = new Label();
+            mv.visitLabel(label2);
+            mv.visitLineNumber(14, label2);
+            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
+            mv.visitVarInsn(ALOAD, 4);
+            mv.visitInsn(ICONST_1);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false);
+            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
+            Label label3 = new Label();
+            mv.visitLabel(label3);
+            mv.visitLineNumber(15, label3);
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(3, 6);
+            mv.visitEnd();
+        }
+
         cw.visitEnd();
 
         return cw.toByteArray();
     }
 
+    // Assign to a parameter.
+    // Without debug information, this will print "parameter1" if a NPE
+    // is raised in the first line because null was passed to the method.
+    // It will print "local1" if a NPE is raised in line three.
+    public void assign_to_parameter(String s1) {
+        System.out.println(s1.substring(1));
+        s1 = null;
+        System.out.println(s1.substring(2));
+    }
+
     // Tests that a class generated on the fly is handled properly.
     public void testGeneratedCode() throws Exception {
         byte[] classBytes = generateTestClass();
         Lookup lookup = lookup();
         Class<?> clazz = lookup.defineClass(classBytes);

@@ -1472,16 +1562,83 @@
         } catch (NullPointerException ex) {
             checkMessage(ex, "return f.i;",
                          ex.getMessage(),
                          "Cannot read field 'i' because 'f' is null.");
         }
+
+        // Optimized bytecode can reuse local variable slots for several
+        // local variables.
+        // If there is no variable name information, we print 'parameteri'
+        // if a parameter maps to a local slot. Once a local slot has been
+        // written, we don't know any more whether it was written as the
+        // corresponding parameter, or whether another local has been
+        // mapped to the slot. So we don't want to print 'parameteri' any
+        // more, but 'locali'. Similary for 'this'.
+
+        // Expect message saying "parameter0".
+        try {
+            e.throwNPE_reuseStackSlot1(null);
+        } catch (NullPointerException ex) {
+            checkMessage(ex, "s1.substring(1)",
+                         ex.getMessage(),
+                         "Cannot invoke 'String.substring(int)' because '<parameter1>' is null.");
+        }
+        // Expect message saying "local0".
+        try {
+            e.throwNPE_reuseStackSlot1("aa");
+        } catch (NullPointerException ex) {
+            checkMessage(ex, "s1_2.substring(1)",
+                         ex.getMessage(),
+                         "Cannot invoke 'String.substring(int)' because '<local1>' is null.");
+        }
+        // Expect message saying "parameter4".
+        try {
+            e.throwNPE_reuseStackSlot4("aa", "bb", "cc", null);
+        } catch (NullPointerException ex) {
+            checkMessage(ex, "s4.substring(1)",
+                         ex.getMessage(),
+                         "Cannot invoke 'String.substring(int)' because '<parameter4>' is null.");
+        }
+        // Expect message saying "local4".
+        try {
+            e.throwNPE_reuseStackSlot4("aa", "bb", "cc", "dd");
+        } catch (NullPointerException ex) {
+            checkMessage(ex, "s4_2.substring(1)",
+                         ex.getMessage(),
+                         "Cannot invoke 'String.substring(int)' because '<local4>' is null.");
+        }
+
+        // Unfortunately, with the fix for optimized code as described above
+        // we don't write 'parameteri' any more after the parameter variable
+        // has been assigned.
+
+        if (!hasDebugInfo) {
+            // Expect message saying "parameter1".
+            try {
+                assign_to_parameter(null);
+            } catch (NullPointerException ex) {
+                checkMessage(ex, "s1.substring(1)",
+                             ex.getMessage(),
+                             "Cannot invoke 'String.substring(int)' because '<parameter1>' is null.");
+            }
+            // The message says "local1" although "parameter1" would be correct.
+            try {
+                assign_to_parameter("aaa");
+            } catch (NullPointerException ex) {
+                checkMessage(ex, "s1.substring(2)",
+                             ex.getMessage(),
+                             "Cannot invoke 'String.substring(int)' because '<local1>' is null.");
+            }
+        }
     }
 }
 
 // Helper interface for test cases needed for generateTestClass().
 interface E0 {
     public int throwNPE(F f);
+    public void throwNPE_reuseStackSlot1(String s1);
+    public void throwNPE_reuseStackSlot4(String s1, String s2, String s3, String s4);
 }
 
 // Helper class for test cases needed for generateTestClass().
 class F {
     int i;
< prev index next >