Path: blob/master/test/jdk/java/io/Serializable/defaulted/GetFieldRead.java
41154 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @bug 418073525* @summary Make sure that fields that are defaulted can be of primitive and26* object type.27*28*/2930import java.io.*;31class TestClass implements Serializable {32public static final Integer DEFAULT_OBJECT_I = 99;33public static final Foo DEFAULT_OBJECT_F = new Foo();34private static final long serialVersionUID = 5748652654655279289L;3536// Fields to be serialized.37private static final ObjectStreamField[] serialPersistentFields = {38new ObjectStreamField("objectI", Integer.class),39new ObjectStreamField("primitiveI", Integer.TYPE),40new ObjectStreamField("foo", Foo.class)41};4243Integer objectI;44int primitiveI;45Foo foo;4647public TestClass(Foo f, Integer I, int i) {48foo = f;49objectI = I;50primitiveI = i;51}5253/**54* Verify GetField.defaulted("fieldName") works for primitive and55* object fields.56*/57private void readObject(ObjectInputStream in)58throws ClassNotFoundException, IOException59{60ObjectInputStream.GetField pfields = in.readFields();61primitiveI = pfields.get("primitiveI", 99);62System.out.println("The primitiveI : " + primitiveI);6364objectI = (Integer)pfields.get("objectI", DEFAULT_OBJECT_I);65System.out.println("The ObjectI : " + objectI);6667foo = (Foo)pfields.get("foo", DEFAULT_OBJECT_F);68System.out.println("The foo : " + foo);6970try {71boolean b = pfields.defaulted("primitiveI");72System.out.println("Defaulted prim : " + b);73if (b == false) {74throw new Error("Bad return value for defaulted() with " +75"primitive type fields");76}7778b = pfields.defaulted("objectI");79System.out.println("Defaulted ObjectI : " + b);80if (b == true) {81throw new Error("Bad return value for defaulted() with " +82"object type fields");83}8485b = pfields.defaulted("foo");86System.out.println("Defaulted Foo : " + b);87if (b == false) {88throw new Error("Bad return value for defaulted() with " +89"object type fields");90}9192} catch (IllegalArgumentException e) {93System.out.println("Exception " + e.getMessage() +94": handled calling " +95"GetField.defaulted(\"fieldName referring to an object\")");96throw e;97}98}99};100101public class GetFieldRead {102public static void main(String[] args)103throws ClassNotFoundException, IOException104{105FileInputStream fis = new FileInputStream("data.ser");106ObjectInputStream in = new ObjectInputStream(fis);107TestClass obj = (TestClass) in.readObject();108in.close();109}110};111112113