Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/NoClassDefFoundErrorTrap/NoClassDefFoundErrorTrap.java
38828 views
1
/*
2
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 4205440
26
* @summary When ObjectInputStream.inputClassDescriptor calls its protected
27
* resolveClass, if a NoClassDefFoundError is thrown, that Error should be
28
* propagated to the caller, instead of being trapped and transformed into
29
* a ClassNotFoundException for the class being resolved.
30
* @author Peter Jones
31
*
32
* @build NoClassDefFoundErrorTrap
33
* @run main NoClassDefFoundErrorTrap
34
*/
35
36
import java.io.*;
37
38
public class NoClassDefFoundErrorTrap {
39
40
private static NoClassDefFoundError ncdfe;
41
42
public interface Bar {}
43
public static class Foo implements Bar, java.io.Serializable {}
44
45
/**
46
* Test subclass of ObjectInputStream that overrides resolveClass
47
* to throw a NoClassDefFoundError if our test class "Foo" is to
48
* be resolved.
49
*/
50
public static class TestObjectInputStream extends ObjectInputStream {
51
52
public TestObjectInputStream(InputStream in)
53
throws IOException
54
{
55
super(in);
56
}
57
58
protected Class resolveClass(ObjectStreamClass desc)
59
throws IOException, ClassNotFoundException
60
{
61
String name = desc.getName();
62
63
if (name.equals(Foo.class.getName())) {
64
ncdfe = new NoClassDefFoundError("Bar");
65
throw ncdfe;
66
} else {
67
return super.resolveClass(desc);
68
}
69
}
70
}
71
72
public static void main(String[] args) {
73
74
System.err.println("\nRegression test for bug 4205440\n");
75
76
try {
77
/*
78
* Serialize a Foo instance to a byte array.
79
*/
80
Foo foo = new Foo();
81
ByteArrayOutputStream bout = new ByteArrayOutputStream();
82
ObjectOutputStream out = new ObjectOutputStream(bout);
83
out.writeObject(foo);
84
byte[] stream = bout.toByteArray();
85
86
/*
87
* Deserialize the Foo instance using our test subclass of
88
* ObjectInputStream that will throw NoClassDefFoundError.
89
*/
90
ByteArrayInputStream bin = new ByteArrayInputStream(stream);
91
ObjectInputStream in = new TestObjectInputStream(bin);
92
93
/*
94
* The test succeeds if we get the NoClassDefFoundError.
95
*/
96
try {
97
in.readObject();
98
} catch (NoClassDefFoundError e) {
99
if (e == ncdfe) {
100
System.err.println("TEST PASSED: " + e.toString());
101
} else {
102
throw e;
103
}
104
}
105
106
} catch (Exception e) {
107
System.err.println("\nTEST FAILED:");
108
e.printStackTrace();
109
throw new RuntimeException("TEST FAILED: " + e.toString());
110
}
111
}
112
}
113
114