GetFieldRead.java revision 0
0N/A/*
0N/A * Copyright 1999 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @bug 4180735
0N/A * @summary Make sure that fields that are defaulted can be of primitive and
0N/A * object type.
0N/A *
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aclass TestClass implements Serializable {
0N/A public static final Integer DEFAULT_OBJECT_I = new Integer(99);
0N/A public static final Foo DEFAULT_OBJECT_F = new Foo();
0N/A private static final long serialVersionUID=5748652654655279289L;
0N/A
0N/A // Fields to be serialized.
0N/A private final static ObjectStreamField[] serialPersistentFields = {
0N/A new ObjectStreamField("objectI", Integer.class),
0N/A new ObjectStreamField("primitiveI", Integer.TYPE),
0N/A new ObjectStreamField("foo", Foo.class)
0N/A };
0N/A
0N/A Integer objectI;
0N/A int primitiveI;
0N/A Foo foo;
0N/A
0N/A public TestClass(Foo f, Integer I, int i) {
0N/A foo = f;
0N/A objectI = I;
0N/A primitiveI = i;
0N/A }
0N/A
0N/A /**
0N/A * Verify GetField.defaulted("fieldName") works for primitive and
0N/A * object fields.
0N/A */
0N/A private void readObject(ObjectInputStream in)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A ObjectInputStream.GetField pfields = in.readFields();
0N/A primitiveI = pfields.get("primitiveI", 99);
0N/A System.out.println("The primitiveI : " + primitiveI);
0N/A
0N/A objectI = (Integer)pfields.get("objectI", DEFAULT_OBJECT_I);
0N/A System.out.println("The ObjectI : " + objectI);
0N/A
0N/A foo = (Foo)pfields.get("foo", DEFAULT_OBJECT_F);
0N/A System.out.println("The foo : " + foo);
0N/A
0N/A try {
0N/A boolean b = pfields.defaulted("primitiveI");
0N/A System.out.println("Defaulted prim : " + b);
0N/A if (b == false) {
0N/A throw new Error("Bad return value for defaulted() with " +
0N/A "primitive type fields");
0N/A }
0N/A
0N/A b = pfields.defaulted("objectI");
0N/A System.out.println("Defaulted ObjectI : " + b);
0N/A if (b == true) {
0N/A throw new Error("Bad return value for defaulted() with " +
0N/A "object type fields");
0N/A }
0N/A
0N/A b = pfields.defaulted("foo");
0N/A System.out.println("Defaulted Foo : " + b);
0N/A if (b == false) {
0N/A throw new Error("Bad return value for defaulted() with " +
0N/A "object type fields");
0N/A }
0N/A
0N/A } catch (IllegalArgumentException e) {
0N/A System.out.println("Exception " + e.getMessage() +
0N/A ": handled calling " +
0N/A "GetField.defaulted(\"fieldName referring to an object\")");
0N/A throw e;
0N/A }
0N/A }
0N/A};
0N/A
0N/Apublic class GetFieldRead {
0N/A public static void main(String[] args)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A FileInputStream fis = new FileInputStream("data.ser");
0N/A ObjectInputStream in = new ObjectInputStream(fis);
0N/A TestClass obj = (TestClass) in.readObject();
0N/A in.close();
0N/A }
0N/A};