< prev index next >

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

Print this page
rev 59858 : 8248476: No helpful NullPointerException message after calling fillInStackTrace
Summary: reported by christoph.dreis@freenet.de
Reviewed-by:
rev 58801 : 8242311: use reproducible random in hotspot runtime tests
Reviewed-by: iklam, mseledtsov
rev 56645 : 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


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


1264                                "\"<local71>[<local70>][<local73>[<local65>][<local64>]][<local72>[<parameter63>][<parameter62>]" +
1265                                   "[<parameter47>]][<local72>[<local73>[<parameter33>][<parameter32>]][<parameter31>][<parameter17>]]\"") +
1266                              " is null");
1267             }
1268     }
1269 
1270 
1271     public void testParameters() throws Exception {
1272         A a = new A();
1273         a.to_b = new B();
1274         testParametersTestMethod(a, 0.0, null, null);
1275     }
1276 
1277 
1278     public void testCreation() throws Exception {
1279         // If allocated with new, the message should not be generated.
1280         Asserts.assertNull(new NullPointerException().getMessage());
1281         String msg = new String("A pointless message");
1282         Asserts.assertTrue(new NullPointerException(msg).getMessage() == msg);
1283 




































































1284         // If created via reflection, the message should not be generated.
1285         Exception ex = NullPointerException.class.getDeclaredConstructor().newInstance();
1286         Asserts.assertNull(ex.getMessage());
1287     }
1288 
1289     public void testNative() throws Exception {
1290         // If NPE is thrown in a native method, the message should
1291         // not be generated.
1292         try {
1293             Class.forName(null);
1294             Asserts.fail();
1295         } catch (NullPointerException e) {
1296             Asserts.assertNull(e.getMessage());
1297         }
1298 
1299     }
1300 
1301     // Test we get the same message calling npe.getMessage() twice.
1302     @SuppressWarnings("null")
1303     public void testSameMessage() throws Exception {
1304         Object null_o = null;
1305         String expectedMsg =




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


1264                                "\"<local71>[<local70>][<local73>[<local65>][<local64>]][<local72>[<parameter63>][<parameter62>]" +
1265                                   "[<parameter47>]][<local72>[<local73>[<parameter33>][<parameter32>]][<parameter31>][<parameter17>]]\"") +
1266                              " is null");
1267             }
1268     }
1269 
1270 
1271     public void testParameters() throws Exception {
1272         A a = new A();
1273         a.to_b = new B();
1274         testParametersTestMethod(a, 0.0, null, null);
1275     }
1276 
1277 
1278     public void testCreation() throws Exception {
1279         // If allocated with new, the message should not be generated.
1280         Asserts.assertNull(new NullPointerException().getMessage());
1281         String msg = new String("A pointless message");
1282         Asserts.assertTrue(new NullPointerException(msg).getMessage() == msg);
1283 
1284         // If the stack trace was set by the user, the message should not be
1285         // created.
1286         // This holds for explicitly crated NPEs, but also for implicilty
1287         // thrown ones.
1288         System.out.println(new NullPointerException().fillInStackTrace().getMessage());
1289         System.out.println(new NullPointerException(msg).fillInStackTrace().getMessage());
1290 
1291         Asserts.assertNull(new NullPointerException().fillInStackTrace().getMessage());
1292         Asserts.assertTrue(new NullPointerException(msg).fillInStackTrace().getMessage() == msg);
1293 
1294         NullPointerException ex = new NullPointerException();
1295         Throwable t = ex.fillInStackTrace();
1296 
1297         Asserts.assertNull(t.getMessage());
1298 
1299         ex = new NullPointerException(msg);
1300         t = ex.fillInStackTrace();
1301         Asserts.assertTrue(t.getMessage() == msg);
1302 
1303         F f = null;
1304         try {
1305             f.i = 17;
1306         } catch (NullPointerException e) {
1307             checkMessage(e, "f.i = 17;", e.getMessage(),
1308                          "Cannot assign field \"i\" because " +
1309                          (hasDebugInfo ? "\"f\"" : "\"<local4>\"") + " is null");
1310             t = e.fillInStackTrace();
1311         }
1312         checkMessage(t, "e.fillInStackTrace()", t.getMessage(), null);
1313 
1314         // Make sure a new exception thrown when calling fillInStackTrace()
1315         // gets the correct message.
1316         ex = null;
1317         try {
1318             ex.fillInStackTrace();
1319         } catch (NullPointerException e) {
1320             checkMessage(e, "ex.fillInStackTrace()", e.getMessage(),
1321                          "Cannot invoke \"java.lang.NullPointerException.fillInStackTrace()\" because " +
1322                          (hasDebugInfo ? "\"ex\"" : "\"<local2>\"") + " is null");
1323         }
1324 
1325         // setStackTrace does not affect computing the message.
1326         // Message and stack trace won't match, though.
1327         F f1 = null;
1328         F f2 = null;
1329         NullPointerException e1 = null;
1330         NullPointerException e2 = null;
1331         try {
1332             f1.i = 18;
1333         } catch (NullPointerException e) {
1334             checkMessage(e, "f1.i = 18;", e.getMessage(),
1335                          "Cannot assign field \"i\" because " +
1336                          (hasDebugInfo ? "\"f1\"" : "\"<local5>\"") + " is null");
1337             e1 = e;
1338         }
1339         try {
1340             f2.i = 19;
1341         } catch (NullPointerException e) {
1342             checkMessage(e, "f2.i = 19;", e.getMessage(),
1343                          "Cannot assign field \"i\" because " +
1344                          (hasDebugInfo ? "\"f2\"" : "\"<local6>\"") + " is null");
1345             e2 = e;
1346         }
1347         e1.setStackTrace(e2.getStackTrace());
1348         checkMessage(e1, "f1.i = 18;", e1.getMessage(),
1349                      "Cannot assign field \"i\" because " +
1350                      (hasDebugInfo ? "\"f1\"" : "\"<local5>\"") + " is null");
1351 
1352         // If created via reflection, the message should not be generated.
1353         ex = NullPointerException.class.getDeclaredConstructor().newInstance();
1354         Asserts.assertNull(ex.getMessage());
1355     }
1356 
1357     public void testNative() throws Exception {
1358         // If NPE is thrown in a native method, the message should
1359         // not be generated.
1360         try {
1361             Class.forName(null);
1362             Asserts.fail();
1363         } catch (NullPointerException e) {
1364             Asserts.assertNull(e.getMessage());
1365         }
1366 
1367     }
1368 
1369     // Test we get the same message calling npe.getMessage() twice.
1370     @SuppressWarnings("null")
1371     public void testSameMessage() throws Exception {
1372         Object null_o = null;
1373         String expectedMsg =


< prev index next >