0N/A/*
3261N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4214888
0N/A * @clean CheckModifiers TestClass1 TestClass2
0N/A * @build CheckModifiers
0N/A * @run main CheckModifiers
0N/A * @summary Make sure that serialpersistentFields data member is used to
0N/A * represent tyhe serializable fields only if it has the modfiers
0N/A * static, final, private and the type is ObjectStreamField.
0N/A * No need to check for static, as ObjectStreamField class is not
0N/A * serializable.
0N/A *
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aclass TestClass1 implements Serializable {
0N/A // Missing the "final" modifier
0N/A private static ObjectStreamField[] serialPersistentFields = {
0N/A new ObjectStreamField("field1", Integer.class),
0N/A new ObjectStreamField("field2", Double.TYPE),
0N/A };
0N/A
0N/A Integer field1;
0N/A double field2;
0N/A int field3;
0N/A String field4;
0N/A
0N/A public TestClass1(Integer f1, double f2, int f3, String f4) {
0N/A field1 = f1;
0N/A field2 = f2;
0N/A field3 = f3;
0N/A field4 = f4;
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A ObjectInputStream.GetField pfields = ois.readFields();
0N/A
0N/A field1 = (Integer) pfields.get("field1", new Integer(100));
0N/A field2 = pfields.get("field2", 99.99);
0N/A
0N/A /* These fields must be present in the stream */
0N/A try {
0N/A field3 = pfields.get("field3", 99);
0N/A System.out.println("Passes test 1a");
0N/A } catch(IllegalArgumentException e) {
0N/A throw new Error("data field: field3 not in the persistent stream");
0N/A }
0N/A try {
0N/A field4 = (String) pfields.get("field4", "Default string");
0N/A System.out.println("Passes test 1b");
0N/A } catch(IllegalArgumentException e) {
0N/A throw new Error("data field: field4 not in the persistent stream");
0N/A }
0N/A }
0N/A};
0N/A
0N/A
0N/Aclass TestClass2 implements Serializable {
0N/A // public instead of private
0N/A public static final ObjectStreamField[] serialPersistentFields = {
0N/A new ObjectStreamField("field1", Integer.class),
0N/A new ObjectStreamField("field2", Double.TYPE),
0N/A };
0N/A
0N/A Integer field1;
0N/A double field2;
0N/A int field3;
0N/A String field4;
0N/A
0N/A public TestClass2(Integer f1, double f2, int f3, String f4) {
0N/A field1 = f1;
0N/A field2 = f2;
0N/A field3 = f3;
0N/A field4 = f4;
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A ObjectInputStream.GetField pfields = ois.readFields();
0N/A
0N/A field1 = (Integer) pfields.get("field1", new Integer(100));
0N/A field2 = pfields.get("field2", 99.99);
0N/A
0N/A /* These fields must be present in the stream */
0N/A try {
0N/A field3 = pfields.get("field3", 99);
0N/A System.out.println("Passes test 2a");
0N/A } catch(IllegalArgumentException e) {
0N/A throw new Error("data field: field3 not in the persistent stream");
0N/A }
0N/A try {
0N/A field4 = (String) pfields.get("field4", "Default string");
0N/A System.out.println("Passes test 2b");
0N/A } catch(IllegalArgumentException e) {
0N/A throw new Error("data field: field4 not in the persistent stream");
0N/A }
0N/A }
0N/A};
0N/A
0N/Aclass TestClass3 implements Serializable{
0N/A // Not of type ObjectStreamField
0N/A private final String[] serialPersistentFields = {"Foo","Foobar"};;
0N/A Integer field1;
0N/A double field2;
0N/A int field3;
0N/A String field4;
0N/A
0N/A public TestClass3(Integer f1, double f2, int f3, String f4) {
0N/A field1 = f1;
0N/A field2 = f2;
0N/A field3 = f3;
0N/A field4 = f4;
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A ObjectInputStream.GetField pfields = ois.readFields();
0N/A
0N/A field1 = (Integer) pfields.get("field1", new Integer(100));
0N/A field2 = pfields.get("field2", 99.99);
0N/A field3 = pfields.get("field3", 99);
0N/A field4 = (String) pfields.get("field4", "Default string");
0N/A
0N/A try {
0N/A String[] tserialPersistentFields =
0N/A (String[])pfields.get("serialPersistentFields", null);
0N/A System.out.println("Passes test 3");
0N/A } catch(IllegalArgumentException e) {
0N/A throw new Error("non-static field: " +
0N/A "serialPersistentFields must be in the persistent stream");
0N/A }
0N/A }
0N/A};
0N/A
0N/Aclass TestClass4 implements Serializable {
0N/A // Correct format
0N/A private static final ObjectStreamField[] serialPersistentFields = {
0N/A new ObjectStreamField("field1", Integer.class),
0N/A new ObjectStreamField("field2", Double.TYPE),
0N/A };
0N/A
0N/A Integer field1;
0N/A double field2;
0N/A int field3;
0N/A String field4;
0N/A
0N/A public TestClass4(Integer f1, double f2, int f3, String f4) {
0N/A field1 = f1;
0N/A field2 = f2;
0N/A field3 = f3;
0N/A field4 = f4;
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A ObjectInputStream.GetField pfields = ois.readFields();
0N/A
0N/A field1 = (Integer) pfields.get("field1", new Integer(100));
0N/A field2 = pfields.get("field2", 99.99);
0N/A
0N/A try {
0N/A field3 = pfields.get("field3", 99);
0N/A throw new Error("data field: field3 in the persistent stream");
0N/A } catch(IllegalArgumentException e) {
0N/A System.out.println("Passes test 4a");
0N/A }
0N/A try {
0N/A field4 = (String) pfields.get("field4", "Default string");
0N/A throw new Error("data field: field4 in the persistent stream");
0N/A } catch(IllegalArgumentException e) {
0N/A System.out.println("Passes test 4b");
0N/A }
0N/A }
0N/A};
0N/A
0N/Apublic class CheckModifiers {
0N/A public static void main(String[] args)
0N/A throws ClassNotFoundException, IOException{
0N/A TestClass1 tc1 = new TestClass1(new Integer(100), 25.56, 2000,
0N/A new String("Test modifiers of serialPersistentFields"));
0N/A
0N/A TestClass2 tc2 = new TestClass2(new Integer(100), 25.56, 2000,
0N/A new String("Test modifiers of serialPersistentFields"));
0N/A
0N/A TestClass3 tc3 = new TestClass3(new Integer(100), 25.56, 2000,
0N/A new String("Test Type of serialPersistentFields"));
0N/A
0N/A TestClass4 tc4 = new TestClass4(new Integer(100), 25.56, 2000,
0N/A new String("Test modifiers of serialPersistentFields"));
0N/A
0N/A
0N/A FileOutputStream fos = new FileOutputStream("fields.ser");
2496N/A try {
2496N/A ObjectOutputStream oos = new ObjectOutputStream(fos);
2496N/A System.out.println("Writing obj 1");
2496N/A oos.writeObject(tc1);
2496N/A System.out.println("Writing obj 2");
2496N/A oos.writeObject(tc2);
2496N/A System.out.println("Writing obj 3");
2496N/A oos.writeObject(tc3);
2496N/A System.out.println("Writing obj 4");
2496N/A oos.writeObject(tc4);
2496N/A oos.flush();
2496N/A } finally {
2496N/A fos.close();
2496N/A }
0N/A
0N/A FileInputStream fis = new FileInputStream("fields.ser");
2496N/A try {
2496N/A ObjectInputStream ois = new ObjectInputStream(fis);
2496N/A System.out.println("Test modifiers for serialPeristentFields ");
2496N/A System.out.println("---------------------------------------- ");
2496N/A System.out.println("Declaration missing final modifier");
2496N/A ois.readObject();
2496N/A System.out.println();
2496N/A System.out.println("Declaration with public instead of private access");
2496N/A ois.readObject();
2496N/A System.out.println();
2496N/A System.out.println("Declaration with different type");
2496N/A ois.readObject();
2496N/A System.out.println();
2496N/A System.out.println("Declaration as in specification");
2496N/A ois.readObject();
2496N/A } finally {
2496N/A fis.close();
2496N/A }
0N/A }
0N/A};