Path: blob/master/test/jdk/java/io/Serializable/evolution/AddedField/WriteAddedField.java
41161 views
/*1* Copyright (c) 1997, 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 408817625* @summary Deserialize an evolved class with a new field, field type is new.26*27* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField28* @compile WriteAddedField.java29* @run main WriteAddedField30* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField31* @compile ReadAddedField.java32* @run main ReadAddedField333435*/36import java.io.*;3738class NewFieldClass implements Serializable {39private static final long serialVersionUID = 1L;4041int k;4243NewFieldClass(int value) {44k = value;45}46};4748class IncompatibleFieldClass implements Serializable {49private static final long serialVersionUID = 3L;50int x = 5;51};5253@SuppressWarnings("serial") /* Incorrect use is being tested */54class NewExternFieldClass implements Externalizable {55private static final long serialVersionUID = 1L;5657byte l;5859public NewExternFieldClass(int value) {60l = (byte)value;61}6263public void readExternal(ObjectInput s)64throws IOException, ClassNotFoundException65{66l = s.readByte();67}6869public void writeExternal(ObjectOutput s) throws IOException70{71s.writeByte(l);72}73}7475class A implements Serializable {76// Version 1.1 of class A. Added superclass NewSerializableSuper.77private static final long serialVersionUID = 1L;78NewFieldClass foo;79NewFieldClass fooarray[];80IncompatibleFieldClass boo;81IncompatibleFieldClass booarray[];82/* Excluded due to Bug 408954083NewExternFieldClass abc;84NewExternFieldClass farray[];85*/86int bar;87A() {88foo = new NewFieldClass(23);89fooarray = new NewFieldClass[24];90for (int i = 0; i < fooarray.length; i++)91fooarray[i] = new NewFieldClass(i);92boo = new IncompatibleFieldClass();93booarray = new IncompatibleFieldClass[24];94for (int i = 0; i < booarray.length; i++)95booarray[i] = new IncompatibleFieldClass();96bar = 4;97/* Excluded due to Bug 408954098abc = new NewExternFieldClass(66);99farray = new NewExternFieldClass[10];100for (int k = 0; k < farray.length; k++)101farray[k] = new NewExternFieldClass(k);102*/103}104}105106/** Test serial persistent fields w/o using Alternate API.107*/108class B implements Serializable {109private static final long serialVersionUID = 2L;110NewFieldClass foo;111NewFieldClass[] array;112IncompatibleFieldClass boo;113IncompatibleFieldClass booarray[];114transient NewExternFieldClass abc;115transient NewExternFieldClass farray[];116int bar;117118B() {119foo = new NewFieldClass(12);120array = new NewFieldClass[12];121for (int i = 0; i < array.length; i++)122array[i] = new NewFieldClass(i);123bar = 4;124abc = new NewExternFieldClass(66);125farray = new NewExternFieldClass[10];126for (int k = 0; k < farray.length; k++)127farray[k] = new NewExternFieldClass(k);128}129}130131/** Test serial persistent fields using Alternate API.132* Also make sure that optional data to non-existent classes can be skipped.133*/134class C implements Serializable {135private static final long serialVersionUID = 3L;136NewFieldClass foo;137NewFieldClass[] array;138int bar;139transient NewExternFieldClass abc;140transient NewExternFieldClass farray[];141142C() {143foo = new NewFieldClass(12);144array = new NewFieldClass[12];145for (int i = 0; i < array.length; i++)146array[i] = new NewFieldClass(i);147bar = 4;148abc = new NewExternFieldClass(3);149farray = new NewExternFieldClass[10];150for (int k = 0; k < farray.length; k++)151farray[k] = new NewExternFieldClass(k);152}153154private void readObject(ObjectInputStream s)155throws IOException, ClassNotFoundException156{157s.defaultReadObject();158/* Excluded due to Bug 4089540159abc = (NewExternFieldClass)fields.get("abc", null);160farray = (NewExternFieldClass[])fields.get("farray", null);161*/162163/* read optional data */164NewFieldClass tmpfoo = (NewFieldClass)s.readObject();165NewFieldClass[] tmparray= (NewFieldClass[])s.readObject();166int tmpbar = s.readInt();167/* Excluded due to Bug 4089540168NewExternFieldClass tmpabc = (NewExternFieldClass)s.readObject();169NewExternFieldClass[] tmpfarray = (NewExternFieldClass[])s.readObject();170*/171}172173private void writeObject(ObjectOutputStream s)174throws IOException175{176s.defaultWriteObject();177178/* write optional data */179s.writeObject(foo);180s.writeObject(array);181s.writeInt(bar);182183/* Excluded due to Bug 4089540184s.writeObject(abc);185s.writeObject(farray);186*/187}188}189190191public class WriteAddedField {192public static void main(String args[]) throws IOException {193A a = new A();194B b = new B();195C c = new C();196File f = new File("tmp.ser");197ObjectOutput out =198new ObjectOutputStream(new FileOutputStream(f));199out.writeObject(a);200out.writeObject(b);201out.writeObject(c);202a = new A();203b = new B();204c = new C();205out.writeObject(a);206out.writeObject(b);207out.writeObject(c);208out.close();209}210}211212213