1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4214888
* @clean CheckModifiers TestClass1 TestClass2
* @build CheckModifiers
* @run main CheckModifiers
* @summary Make sure that serialpersistentFields data member is used to
* represent tyhe serializable fields only if it has the modfiers
* static, final, private and the type is ObjectStreamField.
* No need to check for static, as ObjectStreamField class is not
* serializable.
*
*/
import java.io.*;
class TestClass1 implements Serializable {
// Missing the "final" modifier
private static ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("field1", Integer.class),
new ObjectStreamField("field2", Double.TYPE),
};
Integer field1;
double field2;
int field3;
String field4;
public TestClass1(Integer f1, double f2, int f3, String f4) {
field1 = f1;
field2 = f2;
field3 = f3;
field4 = f4;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField pfields = ois.readFields();
field1 = (Integer) pfields.get("field1", new Integer(100));
field2 = pfields.get("field2", 99.99);
/* These fields must be present in the stream */
try {
field3 = pfields.get("field3", 99);
System.out.println("Passes test 1a");
} catch(IllegalArgumentException e) {
throw new Error("data field: field3 not in the persistent stream");
}
try {
field4 = (String) pfields.get("field4", "Default string");
System.out.println("Passes test 1b");
} catch(IllegalArgumentException e) {
throw new Error("data field: field4 not in the persistent stream");
}
}
};
class TestClass2 implements Serializable {
// public instead of private
public static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("field1", Integer.class),
new ObjectStreamField("field2", Double.TYPE),
};
Integer field1;
double field2;
int field3;
String field4;
public TestClass2(Integer f1, double f2, int f3, String f4) {
field1 = f1;
field2 = f2;
field3 = f3;
field4 = f4;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField pfields = ois.readFields();
field1 = (Integer) pfields.get("field1", new Integer(100));
field2 = pfields.get("field2", 99.99);
/* These fields must be present in the stream */
try {
field3 = pfields.get("field3", 99);
System.out.println("Passes test 2a");
} catch(IllegalArgumentException e) {
throw new Error("data field: field3 not in the persistent stream");
}
try {
field4 = (String) pfields.get("field4", "Default string");
System.out.println("Passes test 2b");
} catch(IllegalArgumentException e) {
throw new Error("data field: field4 not in the persistent stream");
}
}
};
class TestClass3 implements Serializable{
// Not of type ObjectStreamField
private final String[] serialPersistentFields = {"Foo","Foobar"};;
Integer field1;
double field2;
int field3;
String field4;
public TestClass3(Integer f1, double f2, int f3, String f4) {
field1 = f1;
field2 = f2;
field3 = f3;
field4 = f4;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField pfields = ois.readFields();
field1 = (Integer) pfields.get("field1", new Integer(100));
field2 = pfields.get("field2", 99.99);
field3 = pfields.get("field3", 99);
field4 = (String) pfields.get("field4", "Default string");
try {
String[] tserialPersistentFields =
(String[])pfields.get("serialPersistentFields", null);
System.out.println("Passes test 3");
} catch(IllegalArgumentException e) {
throw new Error("non-static field: " +
"serialPersistentFields must be in the persistent stream");
}
}
};
class TestClass4 implements Serializable {
// Correct format
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("field1", Integer.class),
new ObjectStreamField("field2", Double.TYPE),
};
Integer field1;
double field2;
int field3;
String field4;
public TestClass4(Integer f1, double f2, int f3, String f4) {
field1 = f1;
field2 = f2;
field3 = f3;
field4 = f4;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField pfields = ois.readFields();
field1 = (Integer) pfields.get("field1", new Integer(100));
field2 = pfields.get("field2", 99.99);
try {
field3 = pfields.get("field3", 99);
throw new Error("data field: field3 in the persistent stream");
} catch(IllegalArgumentException e) {
System.out.println("Passes test 4a");
}
try {
field4 = (String) pfields.get("field4", "Default string");
throw new Error("data field: field4 in the persistent stream");
} catch(IllegalArgumentException e) {
System.out.println("Passes test 4b");
}
}
};
public class CheckModifiers {
public static void main(String[] args)
throws ClassNotFoundException, IOException{
TestClass1 tc1 = new TestClass1(new Integer(100), 25.56, 2000,
new String("Test modifiers of serialPersistentFields"));
TestClass2 tc2 = new TestClass2(new Integer(100), 25.56, 2000,
new String("Test modifiers of serialPersistentFields"));
TestClass3 tc3 = new TestClass3(new Integer(100), 25.56, 2000,
new String("Test Type of serialPersistentFields"));
TestClass4 tc4 = new TestClass4(new Integer(100), 25.56, 2000,
new String("Test modifiers of serialPersistentFields"));
FileOutputStream fos = new FileOutputStream("fields.ser");
try {
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing obj 1");
oos.writeObject(tc1);
System.out.println("Writing obj 2");
oos.writeObject(tc2);
System.out.println("Writing obj 3");
oos.writeObject(tc3);
System.out.println("Writing obj 4");
oos.writeObject(tc4);
oos.flush();
} finally {
fos.close();
}
FileInputStream fis = new FileInputStream("fields.ser");
try {
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Test modifiers for serialPeristentFields ");
System.out.println("---------------------------------------- ");
System.out.println("Declaration missing final modifier");
ois.readObject();
System.out.println();
System.out.println("Declaration with public instead of private access");
ois.readObject();
System.out.println();
System.out.println("Declaration with different type");
ois.readObject();
System.out.println();
System.out.println("Declaration as in specification");
ois.readObject();
} finally {
fis.close();
}
}
};
|