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) 1998, 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 4099013 |
|
26 |
* @summary Enable substitution of String and Array by ObjectStreams. |
|
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.lang.reflect.Array; |
|
31 |
||
32 |
class A implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
33 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
34 |
|
2 | 35 |
String stringA; |
36 |
String stringB; |
|
37 |
String stringC; |
|
38 |
String[] arrayOfString; |
|
39 |
||
40 |
A() { |
|
41 |
stringA = "hello"; |
|
42 |
stringB = "goodbye"; |
|
43 |
stringC = stringA; |
|
44 |
arrayOfString = new String[2]; |
|
45 |
for (int i = 0; i < arrayOfString.length; i++) |
|
46 |
arrayOfString[i] = new String("array element " + i); |
|
47 |
} |
|
48 |
||
49 |
void report() { |
|
50 |
System.out.println("stringA = " + stringA); |
|
51 |
System.out.println("stringB = " + stringB); |
|
52 |
System.out.println("stringC = " + stringC); |
|
53 |
System.out.println("length of arrayOfString = " + |
|
54 |
arrayOfString.length); |
|
55 |
for (int i = 0; i < arrayOfString.length; i++) |
|
56 |
System.out.println("arrayOfString[" + i + "]= " + |
|
57 |
arrayOfString[i]); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
class SubstituteObjectOutputStream extends ObjectOutputStream { |
|
62 |
public int numStringsReplaced = 0; |
|
63 |
public int numArraysCounted = 0; |
|
64 |
||
65 |
public SubstituteObjectOutputStream(OutputStream out) throws IOException { |
|
66 |
super(out); |
|
67 |
enableReplaceObject(true); |
|
68 |
} |
|
69 |
||
70 |
protected Object replaceObject(Object obj) throws IOException { |
|
71 |
if (obj instanceof String) { |
|
72 |
numStringsReplaced++; |
|
73 |
return obj + "_WriteReplaced"; |
|
74 |
} |
|
75 |
if (obj.getClass().isArray()) { |
|
76 |
Object[] array = (Object[]) obj; |
|
77 |
/* Double the array. |
|
78 |
* Initialize new array elements with original array. */ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
79 |
Class<?> arrayComponentType = array.getClass().getComponentType(); |
2 | 80 |
Object[] newarray = |
81 |
(Object[])Array.newInstance(arrayComponentType, |
|
82 |
array.length * 2); |
|
83 |
for (int i = 0; i < array.length; i++) |
|
84 |
newarray[i] = array[i]; |
|
85 |
for (int ni = array.length; ni < 2* array.length; ni++) |
|
86 |
newarray[ni] = array[ni - array.length]; |
|
87 |
numArraysCounted++; |
|
88 |
obj = newarray; |
|
89 |
} |
|
90 |
return obj; |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
class SubstituteObjectInputStream extends ObjectInputStream { |
|
95 |
public int numStringsReplaced = 0; |
|
96 |
public int numArraysCounted = 0; |
|
97 |
||
98 |
public SubstituteObjectInputStream(InputStream in) throws IOException { |
|
99 |
super(in); |
|
100 |
enableResolveObject(true); |
|
101 |
} |
|
102 |
||
103 |
protected Object resolveObject(Object obj) throws IOException { |
|
104 |
if (obj instanceof String) { |
|
105 |
numStringsReplaced++; |
|
106 |
return obj + "_ReadResolved"; |
|
107 |
} |
|
108 |
if (obj.getClass().isArray()) { |
|
109 |
Object[] array = (Object[])obj; |
|
110 |
||
111 |
/* Double the array. |
|
112 |
* Initialize new array elements with original array. */ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
113 |
Class<?> arrayComponentType = array.getClass().getComponentType(); |
2 | 114 |
Object[] newarray = |
115 |
(Object[])Array.newInstance(arrayComponentType, |
|
116 |
array.length * 2); |
|
117 |
for (int i = 0; i < array.length; i++) |
|
118 |
newarray[i] = array[i]; |
|
119 |
for (int ni = array.length; ni < 2* array.length; ni++) |
|
120 |
newarray[ni] = array[ni - array.length]; |
|
121 |
numArraysCounted++; |
|
122 |
obj = newarray; |
|
123 |
} |
|
124 |
return obj; |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
public class ReplaceStringArray { |
|
129 |
public static void main(String args[]) throws IOException, ClassNotFoundException { |
|
130 |
boolean verbose = false; |
|
131 |
if (args.length >= 1 && args[0].compareTo("verbose") == 0) |
|
132 |
verbose = true; |
|
133 |
||
134 |
||
135 |
A a = new A(); |
|
136 |
if (verbose) { |
|
137 |
System.out.println("Value of Class A"); |
|
138 |
a.report(); |
|
139 |
System.out.println(""); |
|
140 |
} |
|
141 |
||
142 |
||
143 |
/* Serialize object a to bytestream. */ |
|
144 |
if (verbose) { |
|
145 |
System.out.println("Serialize A to SubstituteObjectOutputStream"); |
|
146 |
System.out.println(""); |
|
147 |
} |
|
148 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
149 |
SubstituteObjectOutputStream out = new SubstituteObjectOutputStream(baos); |
|
150 |
out.writeObject(a); |
|
151 |
out.close(); |
|
152 |
a = null; |
|
153 |
||
154 |
/* Validate that writeReplace called by SubstituteObjectOutputStream.*/ |
|
155 |
boolean expectedResult = (out.numStringsReplaced == 4); |
|
156 |
if (!expectedResult) |
|
157 |
throw new Error("Expected " + 4 + " strings to be replaced during serialization;" + |
|
158 |
" only " + out.numStringsReplaced + " strings were replaced."); |
|
159 |
if (out.numArraysCounted != 1) |
|
160 |
throw new Error("Expected 1 array during serialization; only " + |
|
161 |
out.numArraysCounted + " arrays"); |
|
162 |
||
163 |
if (verbose) { |
|
164 |
System.out.println("DeSerialize A from SubstituteObjectInputStream"); |
|
165 |
System.out.println(""); |
|
166 |
} |
|
167 |
SubstituteObjectInputStream in = |
|
168 |
new SubstituteObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); |
|
169 |
a = (A)in.readObject(); |
|
170 |
in.close(); |
|
171 |
||
172 |
/* Validate that readResolve called by SubstituteObjectInputStream.*/ |
|
173 |
if (in.numStringsReplaced != 4) |
|
174 |
throw new Error("Expected 4 strings to be resolved during deserialization;" + |
|
175 |
" only " + in.numStringsReplaced + " strings were resolved."); |
|
176 |
if (in.numArraysCounted != 1) |
|
177 |
throw new Error("Expected 1 array during deserialization; only " + |
|
178 |
out.numArraysCounted + " arrays"); |
|
179 |
if (a.arrayOfString.length != 8) |
|
180 |
throw new Error("Expected a.arrayOfString.length to be 8, observed " + |
|
181 |
a.arrayOfString.length); |
|
182 |
if (verbose) { |
|
183 |
System.out.println("Value of Class A after serialize/deserialize with writeReplace/readResolve"); |
|
184 |
a.report(); |
|
185 |
} |
|
186 |
} |
|
187 |
} |