Path: blob/master/test/jdk/java/io/Serializable/defaulted/GetFieldWrite.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/* @test24* @bug 418073525*26* @clean GetFieldWrite Foo TestClass27* @build GetFieldWrite28* @run main GetFieldWrite29* @clean GetFieldRead TestClass30* @build GetFieldRead31* @run main GetFieldRead32*33* @summary Make sure that fields that are defaulted can be of primitive and34* object type.35*36*/3738import java.io.*;39class TestClass implements Serializable {4041private static final long serialVersionUID = 5748652654655279289L;4243// Fields to be serialized.44private static final ObjectStreamField[] serialPersistentFields = {45new ObjectStreamField("objectI", Integer.class)};4647Integer objectI;48int primitiveI;49Foo foo;5051public TestClass(Foo f, Integer I, int i) {52foo = f;53objectI = I;54primitiveI = i;55}56};5758public class GetFieldWrite {59public static void main(String[] args)60throws ClassNotFoundException, IOException61{62FileOutputStream fos = new FileOutputStream("data.ser");63ObjectOutput out = new ObjectOutputStream(fos);64out.writeObject(new TestClass(new Foo(100, 200), 100, 200));65out.close();66}67};6869/*70* Test class to be used as data field71*/72class Foo implements Serializable{73private static final long serialVersionUID = 1L;7475int a;76int b;77public Foo() {78a = 10; b= 20;79}8081public Foo(int a1, int b1)82{83a = a1; b = b1;84}8586public String toString() {87return new String("a = " + a + " b = " + b);88}89}909192