Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Serializable/nestedReplace/NestedReplace.java
41153 views
1
/*
2
* Copyright (c) 1999, 2019, 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 4217737
26
* @clean NestedReplace A B C D
27
* @build NestedReplace
28
* @run main NestedReplace
29
* @summary Ensure that replacement objects can nominate their own replacements,
30
* so long as the replacement is not the same class as the
31
* just-replaced object.
32
*
33
*/
34
35
import java.io.*;
36
37
class A implements Serializable {
38
private static final long serialVersionUID = 1L;
39
40
Object writeReplace() throws ObjectStreamException {
41
return new B();
42
}
43
}
44
45
class B implements Serializable {
46
private static final long serialVersionUID = 1L;
47
48
Object writeReplace() throws ObjectStreamException {
49
return new C();
50
}
51
}
52
53
class C implements Serializable {
54
private static final long serialVersionUID = 1L;
55
56
static int writeReplaceCalled = 0;
57
58
Object writeReplace() throws ObjectStreamException {
59
writeReplaceCalled++;
60
return new C();
61
}
62
63
Object readResolve() throws ObjectStreamException {
64
return new D();
65
}
66
}
67
68
class D implements Serializable {
69
private static final long serialVersionUID = 1L;
70
71
Object readResolve() throws ObjectStreamException {
72
throw new Error("readResolve() called more than once");
73
}
74
}
75
76
public class NestedReplace {
77
public static void main(String[] args) throws Exception {
78
ByteArrayOutputStream bout;
79
ObjectOutputStream oout;
80
ByteArrayInputStream bin;
81
ObjectInputStream oin;
82
Object obj;
83
84
bout = new ByteArrayOutputStream();
85
oout = new ObjectOutputStream(bout);
86
oout.writeObject(new A());
87
oout.flush();
88
bin = new ByteArrayInputStream(bout.toByteArray());
89
oin = new ObjectInputStream(bin);
90
obj = oin.readObject();
91
92
if (! (obj instanceof D))
93
throw new Error("Deserialized object is of wrong class");
94
if (C.writeReplaceCalled != 1)
95
throw new Error("C.writeReplace() should only get called once");
96
}
97
}
98
99