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) 1997, 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 4088176 |
|
26 |
* @summary Deserialize an evolved class with a new field, field type is new. |
|
27 |
* |
|
28 |
* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField |
|
29 |
* @compile WriteAddedField.java |
|
30 |
* @run main WriteAddedField |
|
31 |
* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField |
|
32 |
* @compile ReadAddedField.java |
|
33 |
* @run main ReadAddedField |
|
34 |
||
35 |
||
36 |
*/ |
|
37 |
import java.io.*; |
|
38 |
||
39 |
class NewFieldClass implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
40 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
41 |
|
2 | 42 |
int k; |
43 |
||
44 |
NewFieldClass(int value) { |
|
45 |
k = value; |
|
46 |
} |
|
47 |
}; |
|
48 |
||
49 |
class IncompatibleFieldClass implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
50 |
private static final long serialVersionUID = 3L; |
2 | 51 |
int x = 5; |
52 |
}; |
|
53 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
54 |
@SuppressWarnings("serial") /* Incorrect use is being tested */ |
2 | 55 |
class NewExternFieldClass implements Externalizable { |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
56 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
57 |
|
2 | 58 |
byte l; |
59 |
||
60 |
public NewExternFieldClass(int value) { |
|
61 |
l = (byte)value; |
|
62 |
} |
|
63 |
||
64 |
public void readExternal(ObjectInput s) |
|
65 |
throws IOException, ClassNotFoundException |
|
66 |
{ |
|
67 |
l = s.readByte(); |
|
68 |
} |
|
69 |
||
70 |
public void writeExternal(ObjectOutput s) throws IOException |
|
71 |
{ |
|
72 |
s.writeByte(l); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
class A implements Serializable { |
|
77 |
// Version 1.1 of class A. Added superclass NewSerializableSuper. |
|
78 |
private static final long serialVersionUID = 1L; |
|
79 |
NewFieldClass foo; |
|
80 |
NewFieldClass fooarray[]; |
|
81 |
IncompatibleFieldClass boo; |
|
82 |
IncompatibleFieldClass booarray[]; |
|
83 |
/* Excluded due to Bug 4089540 |
|
84 |
NewExternFieldClass abc; |
|
85 |
NewExternFieldClass farray[]; |
|
86 |
*/ |
|
87 |
int bar; |
|
88 |
A() { |
|
89 |
foo = new NewFieldClass(23); |
|
90 |
fooarray = new NewFieldClass[24]; |
|
91 |
for (int i = 0; i < fooarray.length; i++) |
|
92 |
fooarray[i] = new NewFieldClass(i); |
|
93 |
boo = new IncompatibleFieldClass(); |
|
94 |
booarray = new IncompatibleFieldClass[24]; |
|
95 |
for (int i = 0; i < booarray.length; i++) |
|
96 |
booarray[i] = new IncompatibleFieldClass(); |
|
97 |
bar = 4; |
|
98 |
/* Excluded due to Bug 4089540 |
|
99 |
abc = new NewExternFieldClass(66); |
|
100 |
farray = new NewExternFieldClass[10]; |
|
101 |
for (int k = 0; k < farray.length; k++) |
|
102 |
farray[k] = new NewExternFieldClass(k); |
|
103 |
*/ |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
/** Test serial persistent fields w/o using Alternate API. |
|
108 |
*/ |
|
109 |
class B implements Serializable { |
|
110 |
private static final long serialVersionUID = 2L; |
|
111 |
NewFieldClass foo; |
|
112 |
NewFieldClass[] array; |
|
113 |
IncompatibleFieldClass boo; |
|
114 |
IncompatibleFieldClass booarray[]; |
|
115 |
transient NewExternFieldClass abc; |
|
116 |
transient NewExternFieldClass farray[]; |
|
117 |
int bar; |
|
118 |
||
119 |
B() { |
|
120 |
foo = new NewFieldClass(12); |
|
121 |
array = new NewFieldClass[12]; |
|
122 |
for (int i = 0; i < array.length; i++) |
|
123 |
array[i] = new NewFieldClass(i); |
|
124 |
bar = 4; |
|
125 |
abc = new NewExternFieldClass(66); |
|
126 |
farray = new NewExternFieldClass[10]; |
|
127 |
for (int k = 0; k < farray.length; k++) |
|
128 |
farray[k] = new NewExternFieldClass(k); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
/** Test serial persistent fields using Alternate API. |
|
133 |
* Also make sure that optional data to non-existent classes can be skipped. |
|
134 |
*/ |
|
135 |
class C implements Serializable { |
|
136 |
private static final long serialVersionUID = 3L; |
|
137 |
NewFieldClass foo; |
|
138 |
NewFieldClass[] array; |
|
139 |
int bar; |
|
140 |
transient NewExternFieldClass abc; |
|
141 |
transient NewExternFieldClass farray[]; |
|
142 |
||
143 |
C() { |
|
144 |
foo = new NewFieldClass(12); |
|
145 |
array = new NewFieldClass[12]; |
|
146 |
for (int i = 0; i < array.length; i++) |
|
147 |
array[i] = new NewFieldClass(i); |
|
148 |
bar = 4; |
|
149 |
abc = new NewExternFieldClass(3); |
|
150 |
farray = new NewExternFieldClass[10]; |
|
151 |
for (int k = 0; k < farray.length; k++) |
|
152 |
farray[k] = new NewExternFieldClass(k); |
|
153 |
} |
|
154 |
||
155 |
private void readObject(ObjectInputStream s) |
|
156 |
throws IOException, ClassNotFoundException |
|
157 |
{ |
|
158 |
s.defaultReadObject(); |
|
159 |
/* Excluded due to Bug 4089540 |
|
160 |
abc = (NewExternFieldClass)fields.get("abc", null); |
|
161 |
farray = (NewExternFieldClass[])fields.get("farray", null); |
|
162 |
*/ |
|
163 |
||
164 |
/* read optional data */ |
|
165 |
NewFieldClass tmpfoo = (NewFieldClass)s.readObject(); |
|
166 |
NewFieldClass[] tmparray= (NewFieldClass[])s.readObject(); |
|
167 |
int tmpbar = s.readInt(); |
|
168 |
/* Excluded due to Bug 4089540 |
|
169 |
NewExternFieldClass tmpabc = (NewExternFieldClass)s.readObject(); |
|
170 |
NewExternFieldClass[] tmpfarray = (NewExternFieldClass[])s.readObject(); |
|
171 |
*/ |
|
172 |
} |
|
173 |
||
174 |
private void writeObject(ObjectOutputStream s) |
|
175 |
throws IOException |
|
176 |
{ |
|
177 |
s.defaultWriteObject(); |
|
178 |
||
179 |
/* write optional data */ |
|
180 |
s.writeObject(foo); |
|
181 |
s.writeObject(array); |
|
182 |
s.writeInt(bar); |
|
183 |
||
184 |
/* Excluded due to Bug 4089540 |
|
185 |
s.writeObject(abc); |
|
186 |
s.writeObject(farray); |
|
187 |
*/ |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
||
192 |
public class WriteAddedField { |
|
193 |
public static void main(String args[]) throws IOException { |
|
194 |
A a = new A(); |
|
195 |
B b = new B(); |
|
196 |
C c = new C(); |
|
197 |
File f = new File("tmp.ser"); |
|
198 |
ObjectOutput out = |
|
199 |
new ObjectOutputStream(new FileOutputStream(f)); |
|
200 |
out.writeObject(a); |
|
201 |
out.writeObject(b); |
|
202 |
out.writeObject(c); |
|
203 |
a = new A(); |
|
204 |
b = new B(); |
|
205 |
c = new C(); |
|
206 |
out.writeObject(a); |
|
207 |
out.writeObject(b); |
|
208 |
out.writeObject(c); |
|
209 |
out.close(); |
|
210 |
} |
|
211 |
} |