CheckModifiers.java revision 3261
395N/A/*
2362N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
395N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
395N/A *
395N/A * This code is free software; you can redistribute it and/or modify it
395N/A * under the terms of the GNU General Public License version 2 only, as
395N/A * published by the Free Software Foundation.
395N/A *
395N/A * This code is distributed in the hope that it will be useful, but WITHOUT
395N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
395N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
395N/A * version 2 for more details (a copy is included in the LICENSE file that
395N/A * accompanied this code).
395N/A *
395N/A * You should have received a copy of the GNU General Public License version
395N/A * 2 along with this work; if not, write to the Free Software Foundation,
395N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
395N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
395N/A */
395N/A
395N/A/* @test
395N/A * @bug 4214888
395N/A * @clean CheckModifiers TestClass1 TestClass2
395N/A * @build CheckModifiers
395N/A * @run main CheckModifiers
395N/A * @summary Make sure that serialpersistentFields data member is used to
395N/A * represent tyhe serializable fields only if it has the modfiers
395N/A * static, final, private and the type is ObjectStreamField.
395N/A * No need to check for static, as ObjectStreamField class is not
395N/A * serializable.
395N/A *
395N/A */
395N/A
395N/Aimport java.io.*;
395N/Aclass TestClass1 implements Serializable {
395N/A // Missing the "final" modifier
395N/A private static ObjectStreamField[] serialPersistentFields = {
395N/A new ObjectStreamField("field1", Integer.class),
395N/A new ObjectStreamField("field2", Double.TYPE),
395N/A };
395N/A
395N/A Integer field1;
395N/A double field2;
395N/A int field3;
395N/A String field4;
395N/A
395N/A public TestClass1(Integer f1, double f2, int f3, String f4) {
395N/A field1 = f1;
395N/A field2 = f2;
395N/A field3 = f3;
395N/A field4 = f4;
395N/A }
395N/A
395N/A private void readObject(ObjectInputStream ois)
395N/A throws IOException, ClassNotFoundException {
395N/A ObjectInputStream.GetField pfields = ois.readFields();
395N/A
395N/A field1 = (Integer) pfields.get("field1", new Integer(100));
395N/A 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();
}
}
};