author | rriggs |
Fri, 11 Oct 2019 13:11:56 -0400 | |
changeset 58565 | baa5969ecf34 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 {} |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
43 |
public static class Foo implements Bar, java.io.Serializable { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
44 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
45 |
} |
2 | 46 |
|
47 |
/** |
|
48 |
* Test subclass of ObjectInputStream that overrides resolveClass |
|
49 |
* to throw a NoClassDefFoundError if our test class "Foo" is to |
|
50 |
* be resolved. |
|
51 |
*/ |
|
52 |
public static class TestObjectInputStream extends ObjectInputStream { |
|
53 |
||
54 |
public TestObjectInputStream(InputStream in) |
|
55 |
throws IOException |
|
56 |
{ |
|
57 |
super(in); |
|
58 |
} |
|
59 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
60 |
protected Class<?> resolveClass(ObjectStreamClass desc) |
2 | 61 |
throws IOException, ClassNotFoundException |
62 |
{ |
|
63 |
String name = desc.getName(); |
|
64 |
||
65 |
if (name.equals(Foo.class.getName())) { |
|
66 |
ncdfe = new NoClassDefFoundError("Bar"); |
|
67 |
throw ncdfe; |
|
68 |
} else { |
|
69 |
return super.resolveClass(desc); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
public static void main(String[] args) { |
|
75 |
||
76 |
System.err.println("\nRegression test for bug 4205440\n"); |
|
77 |
||
78 |
try { |
|
79 |
/* |
|
80 |
* Serialize a Foo instance to a byte array. |
|
81 |
*/ |
|
82 |
Foo foo = new Foo(); |
|
83 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
84 |
ObjectOutputStream out = new ObjectOutputStream(bout); |
|
85 |
out.writeObject(foo); |
|
86 |
byte[] stream = bout.toByteArray(); |
|
87 |
||
88 |
/* |
|
89 |
* Deserialize the Foo instance using our test subclass of |
|
90 |
* ObjectInputStream that will throw NoClassDefFoundError. |
|
91 |
*/ |
|
92 |
ByteArrayInputStream bin = new ByteArrayInputStream(stream); |
|
93 |
ObjectInputStream in = new TestObjectInputStream(bin); |
|
94 |
||
95 |
/* |
|
96 |
* The test succeeds if we get the NoClassDefFoundError. |
|
97 |
*/ |
|
98 |
try { |
|
99 |
in.readObject(); |
|
100 |
} catch (NoClassDefFoundError e) { |
|
101 |
if (e == ncdfe) { |
|
102 |
System.err.println("TEST PASSED: " + e.toString()); |
|
103 |
} else { |
|
104 |
throw e; |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
} catch (Exception e) { |
|
109 |
System.err.println("\nTEST FAILED:"); |
|
110 |
e.printStackTrace(); |
|
111 |
throw new RuntimeException("TEST FAILED: " + e.toString()); |
|
112 |
} |
|
113 |
} |
|
114 |
} |