Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Serializable/checkModifiers/CheckModifiers.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 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 {
39
private static final long serialVersionUID = 1L;
40
41
// Missing the "final" modifier
42
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
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
64
field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
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 {
85
private static final long serialVersionUID = 1L;
86
87
// public instead of private
88
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
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
110
field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
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{
130
private static final long serialVersionUID = 1L;
131
132
// Not of type ObjectStreamField
133
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
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
151
field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
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 {
168
private static final long serialVersionUID = 1L;
169
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
192
field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
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{
213
TestClass1 tc1 = new TestClass1(100, 25.56, 2000,
214
new String("Test modifiers of serialPersistentFields"));
215
216
TestClass2 tc2 = new TestClass2(100, 25.56, 2000,
217
new String("Test modifiers of serialPersistentFields"));
218
219
TestClass3 tc3 = new TestClass3(100, 25.56, 2000,
220
new String("Test Type of serialPersistentFields"));
221
222
TestClass4 tc4 = new TestClass4(100, 25.56, 2000,
223
new String("Test modifiers of serialPersistentFields"));
224
225
226
FileOutputStream fos = new FileOutputStream("fields.ser");
227
try {
228
ObjectOutputStream oos = new ObjectOutputStream(fos);
229
System.out.println("Writing obj 1");
230
oos.writeObject(tc1);
231
System.out.println("Writing obj 2");
232
oos.writeObject(tc2);
233
System.out.println("Writing obj 3");
234
oos.writeObject(tc3);
235
System.out.println("Writing obj 4");
236
oos.writeObject(tc4);
237
oos.flush();
238
} finally {
239
fos.close();
240
}
241
242
FileInputStream fis = new FileInputStream("fields.ser");
243
try {
244
ObjectInputStream ois = new ObjectInputStream(fis);
245
System.out.println("Test modifiers for serialPeristentFields ");
246
System.out.println("---------------------------------------- ");
247
System.out.println("Declaration missing final modifier");
248
ois.readObject();
249
System.out.println();
250
System.out.println("Declaration with public instead of private access");
251
ois.readObject();
252
System.out.println();
253
System.out.println("Declaration with different type");
254
ois.readObject();
255
System.out.println();
256
System.out.println("Declaration as in specification");
257
ois.readObject();
258
} finally {
259
fis.close();
260
}
261
}
262
};
263
264