< prev index next >

test/hotspot/jtreg/runtime/exceptionMsgs/NoClassDefFoundError/NoClassDefFoundErrorTest.java

Print this page
rev 50339 : 8203824: Chain exception from initialization in later NoClassDefFoundErrors.

*** 1,7 **** /* ! * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 19,51 **** * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! /* * @test * @bug 8056900 * @summary Verifies message returned with NoClassDefFoundError exception. * @library /test/lib * @modules java.base/jdk.internal.misc * java.compiler ! * @run main/native NoClassDefFoundMsg */ import jdk.test.lib.compiler.InMemoryJavaCompiler; import jdk.internal.misc.Unsafe; ! public class NoClassDefFoundMsg { static native void callDefineClass(String className); static native void callFindClass(String className); static { System.loadLibrary("NoClassDefFoundMsg"); } ! ! public static void main(String args[]) throws Exception { Unsafe unsafe = Unsafe.getUnsafe(); byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }"); // Create a class name of length 65536. --- 19,52 ---- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! /** * @test * @bug 8056900 * @summary Verifies message returned with NoClassDefFoundError exception. * @library /test/lib * @modules java.base/jdk.internal.misc * java.compiler ! * @run main/native NoClassDefFoundErrorTest */ + import jdk.test.lib.Asserts; import jdk.test.lib.compiler.InMemoryJavaCompiler; import jdk.internal.misc.Unsafe; ! public class NoClassDefFoundErrorTest { static native void callDefineClass(String className); static native void callFindClass(String className); static { System.loadLibrary("NoClassDefFoundMsg"); } ! // Test illegal class names. Too long or null. ! static void test_classNames() throws Exception { Unsafe unsafe = Unsafe.getUnsafe(); byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }"); // Create a class name of length 65536.
*** 92,97 **** --- 93,151 ---- if (!e.getMessage().contains("No class name given")) { throw new RuntimeException("Wrong NoClassDefFoundError: " + e.getMessage()); } } } + + private static class ClassWithFailedInitializer { + public static final int i = (new Object[1])[0].hashCode(); + } + + // If static initialization of a class failed, later access to it + // throws a NoClassDefFoundError. This error chains the exeption + // thrown in the previous initialization. This test assures this is + // working properly. + static void test_chainStaticInitializerException() throws Exception { + Object[] o = new Object[1024]; + System.gc(); + + try { + if (ClassWithFailedInitializer.i == 0) { + throw new RuntimeException("Class initialization succeeded but is designed to fail."); + } + throw new Exception("Expected exception was not thrown."); + } + catch (ExceptionInInitializerError e) { + Asserts.assertNE(e.getCause(), null, "Expecting cause in ExceptionInInitializerError."); + Asserts.assertEQ(e.getCause().getClass(), NullPointerException.class, "sanity"); + } + + // Make sure the VM kept the stored exception alive. + o = null; + System.gc(); + o = new Object[16384]; + o = new Object[16384]; + o = new Object[16384]; + o = new Object[16384]; + System.gc(); + + try { + if (ClassWithFailedInitializer.i == 0) { + throw new RuntimeException("Class initialization succeeded but is designed to fail."); + } + throw new Exception("Expected exception was not thrown."); + } catch (NoClassDefFoundError e) { + e.printStackTrace(); + Asserts.assertNE(o, null, "sanity"); + Asserts.assertNE(e.getCause(), null, "Expecting chained exception"); + Asserts.assertEQ(e.getCause().getClass(), ExceptionInInitializerError.class, + "Wrong exception chained: " + e.getCause()); + System.identityHashCode(e.getCause()); + System.identityHashCode(e); + } + } + + public static void main(String args[]) throws Exception { + test_classNames(); + test_chainStaticInitializerException(); + } }
< prev index next >