Path: blob/master/test/jdk/java/io/Serializable/NoClassDefFoundErrorTrap/NoClassDefFoundErrorTrap.java
41153 views
/*1* Copyright (c) 1999, 2019, 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 {43private static final long serialVersionUID = 1L;44}4546/**47* Test subclass of ObjectInputStream that overrides resolveClass48* to throw a NoClassDefFoundError if our test class "Foo" is to49* be resolved.50*/51public static class TestObjectInputStream extends ObjectInputStream {5253public TestObjectInputStream(InputStream in)54throws IOException55{56super(in);57}5859protected Class<?> resolveClass(ObjectStreamClass desc)60throws IOException, ClassNotFoundException61{62String name = desc.getName();6364if (name.equals(Foo.class.getName())) {65ncdfe = new NoClassDefFoundError("Bar");66throw ncdfe;67} else {68return super.resolveClass(desc);69}70}71}7273public static void main(String[] args) {7475System.err.println("\nRegression test for bug 4205440\n");7677try {78/*79* Serialize a Foo instance to a byte array.80*/81Foo foo = new Foo();82ByteArrayOutputStream bout = new ByteArrayOutputStream();83ObjectOutputStream out = new ObjectOutputStream(bout);84out.writeObject(foo);85byte[] stream = bout.toByteArray();8687/*88* Deserialize the Foo instance using our test subclass of89* ObjectInputStream that will throw NoClassDefFoundError.90*/91ByteArrayInputStream bin = new ByteArrayInputStream(stream);92ObjectInputStream in = new TestObjectInputStream(bin);9394/*95* The test succeeds if we get the NoClassDefFoundError.96*/97try {98in.readObject();99} catch (NoClassDefFoundError e) {100if (e == ncdfe) {101System.err.println("TEST PASSED: " + e.toString());102} else {103throw e;104}105}106107} catch (Exception e) {108System.err.println("\nTEST FAILED:");109e.printStackTrace();110throw new RuntimeException("TEST FAILED: " + e.toString());111}112}113}114115116