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 4214888 |
|
26 |
* @clean CheckModifiers TestClass1 TestClass2 |
|
27 |
* @build CheckModifiers |
|
28 |
* @run main CheckModifiers |
|
29 |
* @summary Make sure that serialpersistentFields data member is used to |
|
30 |
* represent tyhe serializable fields only if it has the modfiers |
|
31 |
* static, final, private and the type is ObjectStreamField. |
|
32 |
* No need to check for static, as ObjectStreamField class is not |
|
33 |
* serializable. |
|
34 |
* |
|
35 |
*/ |
|
36 |
||
37 |
import java.io.*; |
|
38 |
class TestClass1 implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
39 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
40 |
|
2 | 41 |
// Missing the "final" modifier |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
42 |
@SuppressWarnings("serial") /* Incorrect declarations are being tested */ |
2 | 43 |
private static ObjectStreamField[] serialPersistentFields = { |
44 |
new ObjectStreamField("field1", Integer.class), |
|
45 |
new ObjectStreamField("field2", Double.TYPE), |
|
46 |
}; |
|
47 |
||
48 |
Integer field1; |
|
49 |
double field2; |
|
50 |
int field3; |
|
51 |
String field4; |
|
52 |
||
53 |
public TestClass1(Integer f1, double f2, int f3, String f4) { |
|
54 |
field1 = f1; |
|
55 |
field2 = f2; |
|
56 |
field3 = f3; |
|
57 |
field4 = f4; |
|
58 |
} |
|
59 |
||
60 |
private void readObject(ObjectInputStream ois) |
|
61 |
throws IOException, ClassNotFoundException { |
|
62 |
ObjectInputStream.GetField pfields = ois.readFields(); |
|
63 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
64 |
field1 = (Integer) pfields.get("field1", Integer.valueOf(100)); |
2 | 65 |
field2 = pfields.get("field2", 99.99); |
66 |
||
67 |
/* These fields must be present in the stream */ |
|
68 |
try { |
|
69 |
field3 = pfields.get("field3", 99); |
|
70 |
System.out.println("Passes test 1a"); |
|
71 |
} catch(IllegalArgumentException e) { |
|
72 |
throw new Error("data field: field3 not in the persistent stream"); |
|
73 |
} |
|
74 |
try { |
|
75 |
field4 = (String) pfields.get("field4", "Default string"); |
|
76 |
System.out.println("Passes test 1b"); |
|
77 |
} catch(IllegalArgumentException e) { |
|
78 |
throw new Error("data field: field4 not in the persistent stream"); |
|
79 |
} |
|
80 |
} |
|
81 |
}; |
|
82 |
||
83 |
||
84 |
class TestClass2 implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
85 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
86 |
|
2 | 87 |
// public instead of private |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
88 |
@SuppressWarnings("serial") /* Incorrect declarations are being tested */ |
2 | 89 |
public static final ObjectStreamField[] serialPersistentFields = { |
90 |
new ObjectStreamField("field1", Integer.class), |
|
91 |
new ObjectStreamField("field2", Double.TYPE), |
|
92 |
}; |
|
93 |
||
94 |
Integer field1; |
|
95 |
double field2; |
|
96 |
int field3; |
|
97 |
String field4; |
|
98 |
||
99 |
public TestClass2(Integer f1, double f2, int f3, String f4) { |
|
100 |
field1 = f1; |
|
101 |
field2 = f2; |
|
102 |
field3 = f3; |
|
103 |
field4 = f4; |
|
104 |
} |
|
105 |
||
106 |
private void readObject(ObjectInputStream ois) |
|
107 |
throws IOException, ClassNotFoundException { |
|
108 |
ObjectInputStream.GetField pfields = ois.readFields(); |
|
109 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
110 |
field1 = (Integer) pfields.get("field1", Integer.valueOf(100)); |
2 | 111 |
field2 = pfields.get("field2", 99.99); |
112 |
||
113 |
/* These fields must be present in the stream */ |
|
114 |
try { |
|
115 |
field3 = pfields.get("field3", 99); |
|
116 |
System.out.println("Passes test 2a"); |
|
117 |
} catch(IllegalArgumentException e) { |
|
118 |
throw new Error("data field: field3 not in the persistent stream"); |
|
119 |
} |
|
120 |
try { |
|
121 |
field4 = (String) pfields.get("field4", "Default string"); |
|
122 |
System.out.println("Passes test 2b"); |
|
123 |
} catch(IllegalArgumentException e) { |
|
124 |
throw new Error("data field: field4 not in the persistent stream"); |
|
125 |
} |
|
126 |
} |
|
127 |
}; |
|
128 |
||
129 |
class TestClass3 implements Serializable{ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
130 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
131 |
|
2 | 132 |
// Not of type ObjectStreamField |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
133 |
@SuppressWarnings("serial") /* Incorrect declarations are being tested */ |
2 | 134 |
private final String[] serialPersistentFields = {"Foo","Foobar"};; |
135 |
Integer field1; |
|
136 |
double field2; |
|
137 |
int field3; |
|
138 |
String field4; |
|
139 |
||
140 |
public TestClass3(Integer f1, double f2, int f3, String f4) { |
|
141 |
field1 = f1; |
|
142 |
field2 = f2; |
|
143 |
field3 = f3; |
|
144 |
field4 = f4; |
|
145 |
} |
|
146 |
||
147 |
private void readObject(ObjectInputStream ois) |
|
148 |
throws IOException, ClassNotFoundException { |
|
149 |
ObjectInputStream.GetField pfields = ois.readFields(); |
|
150 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
151 |
field1 = (Integer) pfields.get("field1", Integer.valueOf(100)); |
2 | 152 |
field2 = pfields.get("field2", 99.99); |
153 |
field3 = pfields.get("field3", 99); |
|
154 |
field4 = (String) pfields.get("field4", "Default string"); |
|
155 |
||
156 |
try { |
|
157 |
String[] tserialPersistentFields = |
|
158 |
(String[])pfields.get("serialPersistentFields", null); |
|
159 |
System.out.println("Passes test 3"); |
|
160 |
} catch(IllegalArgumentException e) { |
|
161 |
throw new Error("non-static field: " + |
|
162 |
"serialPersistentFields must be in the persistent stream"); |
|
163 |
} |
|
164 |
} |
|
165 |
}; |
|
166 |
||
167 |
class TestClass4 implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
168 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
169 |
|
2 | 170 |
// Correct format |
171 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
172 |
new ObjectStreamField("field1", Integer.class), |
|
173 |
new ObjectStreamField("field2", Double.TYPE), |
|
174 |
}; |
|
175 |
||
176 |
Integer field1; |
|
177 |
double field2; |
|
178 |
int field3; |
|
179 |
String field4; |
|
180 |
||
181 |
public TestClass4(Integer f1, double f2, int f3, String f4) { |
|
182 |
field1 = f1; |
|
183 |
field2 = f2; |
|
184 |
field3 = f3; |
|
185 |
field4 = f4; |
|
186 |
} |
|
187 |
||
188 |
private void readObject(ObjectInputStream ois) |
|
189 |
throws IOException, ClassNotFoundException { |
|
190 |
ObjectInputStream.GetField pfields = ois.readFields(); |
|
191 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
192 |
field1 = (Integer) pfields.get("field1", Integer.valueOf(100)); |
2 | 193 |
field2 = pfields.get("field2", 99.99); |
194 |
||
195 |
try { |
|
196 |
field3 = pfields.get("field3", 99); |
|
197 |
throw new Error("data field: field3 in the persistent stream"); |
|
198 |
} catch(IllegalArgumentException e) { |
|
199 |
System.out.println("Passes test 4a"); |
|
200 |
} |
|
201 |
try { |
|
202 |
field4 = (String) pfields.get("field4", "Default string"); |
|
203 |
throw new Error("data field: field4 in the persistent stream"); |
|
204 |
} catch(IllegalArgumentException e) { |
|
205 |
System.out.println("Passes test 4b"); |
|
206 |
} |
|
207 |
} |
|
208 |
}; |
|
209 |
||
210 |
public class CheckModifiers { |
|
211 |
public static void main(String[] args) |
|
212 |
throws ClassNotFoundException, IOException{ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
213 |
TestClass1 tc1 = new TestClass1(100, 25.56, 2000, |
2 | 214 |
new String("Test modifiers of serialPersistentFields")); |
215 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
216 |
TestClass2 tc2 = new TestClass2(100, 25.56, 2000, |
2 | 217 |
new String("Test modifiers of serialPersistentFields")); |
218 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
219 |
TestClass3 tc3 = new TestClass3(100, 25.56, 2000, |
2 | 220 |
new String("Test Type of serialPersistentFields")); |
221 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
222 |
TestClass4 tc4 = new TestClass4(100, 25.56, 2000, |
2 | 223 |
new String("Test modifiers of serialPersistentFields")); |
224 |
||
225 |
||
226 |
FileOutputStream fos = new FileOutputStream("fields.ser"); |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
227 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
228 |
ObjectOutputStream oos = new ObjectOutputStream(fos); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
229 |
System.out.println("Writing obj 1"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
230 |
oos.writeObject(tc1); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
231 |
System.out.println("Writing obj 2"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
232 |
oos.writeObject(tc2); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
233 |
System.out.println("Writing obj 3"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
234 |
oos.writeObject(tc3); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
235 |
System.out.println("Writing obj 4"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
236 |
oos.writeObject(tc4); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
237 |
oos.flush(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
238 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
239 |
fos.close(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
240 |
} |
2 | 241 |
|
242 |
FileInputStream fis = new FileInputStream("fields.ser"); |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
243 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
244 |
ObjectInputStream ois = new ObjectInputStream(fis); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
245 |
System.out.println("Test modifiers for serialPeristentFields "); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
246 |
System.out.println("---------------------------------------- "); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
247 |
System.out.println("Declaration missing final modifier"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
248 |
ois.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
249 |
System.out.println(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
250 |
System.out.println("Declaration with public instead of private access"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
251 |
ois.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
252 |
System.out.println(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
253 |
System.out.println("Declaration with different type"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
254 |
ois.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
255 |
System.out.println(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
256 |
System.out.println("Declaration as in specification"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
257 |
ois.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
258 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
259 |
fis.close(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
260 |
} |
2 | 261 |
} |
262 |
}; |