Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/NoClassDefFoundErrorTrap/NoClassDefFoundErrorTrap.java
38828 views
/*1* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 420544025* @summary When ObjectInputStream.inputClassDescriptor calls its protected26* resolveClass, if a NoClassDefFoundError is thrown, that Error should be27* propagated to the caller, instead of being trapped and transformed into28* a ClassNotFoundException for the class being resolved.29* @author Peter Jones30*31* @build NoClassDefFoundErrorTrap32* @run main NoClassDefFoundErrorTrap33*/3435import java.io.*;3637public class NoClassDefFoundErrorTrap {3839private static NoClassDefFoundError ncdfe;4041public interface Bar {}42public static class Foo implements Bar, java.io.Serializable {}4344/**45* Test subclass of ObjectInputStream that overrides resolveClass46* to throw a NoClassDefFoundError if our test class "Foo" is to47* be resolved.48*/49public static class TestObjectInputStream extends ObjectInputStream {5051public TestObjectInputStream(InputStream in)52throws IOException53{54super(in);55}5657protected Class resolveClass(ObjectStreamClass desc)58throws IOException, ClassNotFoundException59{60String name = desc.getName();6162if (name.equals(Foo.class.getName())) {63ncdfe = new NoClassDefFoundError("Bar");64throw ncdfe;65} else {66return super.resolveClass(desc);67}68}69}7071public static void main(String[] args) {7273System.err.println("\nRegression test for bug 4205440\n");7475try {76/*77* Serialize a Foo instance to a byte array.78*/79Foo foo = new Foo();80ByteArrayOutputStream bout = new ByteArrayOutputStream();81ObjectOutputStream out = new ObjectOutputStream(bout);82out.writeObject(foo);83byte[] stream = bout.toByteArray();8485/*86* Deserialize the Foo instance using our test subclass of87* ObjectInputStream that will throw NoClassDefFoundError.88*/89ByteArrayInputStream bin = new ByteArrayInputStream(stream);90ObjectInputStream in = new TestObjectInputStream(bin);9192/*93* The test succeeds if we get the NoClassDefFoundError.94*/95try {96in.readObject();97} catch (NoClassDefFoundError e) {98if (e == ncdfe) {99System.err.println("TEST PASSED: " + e.toString());100} else {101throw e;102}103}104105} catch (Exception e) {106System.err.println("\nTEST FAILED:");107e.printStackTrace();108throw new RuntimeException("TEST FAILED: " + e.toString());109}110}111}112113114