Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/evolution/AddedField/WriteAddedField.java
38889 views
/*1* Copyright (c) 1997, 1999, 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 {39int k;4041NewFieldClass(int value) {42k = value;43}44};4546class IncompatibleFieldClass implements Serializable {47private static long serialVersionUID = 3L;48int x = 5;49};5051class NewExternFieldClass implements Externalizable {52byte l;5354public NewExternFieldClass(int value) {55l = (byte)value;56}5758public void readExternal(ObjectInput s)59throws IOException, ClassNotFoundException60{61l = s.readByte();62}6364public void writeExternal(ObjectOutput s) throws IOException65{66s.writeByte(l);67}68}6970class A implements Serializable {71// Version 1.1 of class A. Added superclass NewSerializableSuper.72private static final long serialVersionUID = 1L;73NewFieldClass foo;74NewFieldClass fooarray[];75IncompatibleFieldClass boo;76IncompatibleFieldClass booarray[];77/* Excluded due to Bug 408954078NewExternFieldClass abc;79NewExternFieldClass farray[];80*/81int bar;82A() {83foo = new NewFieldClass(23);84fooarray = new NewFieldClass[24];85for (int i = 0; i < fooarray.length; i++)86fooarray[i] = new NewFieldClass(i);87boo = new IncompatibleFieldClass();88booarray = new IncompatibleFieldClass[24];89for (int i = 0; i < booarray.length; i++)90booarray[i] = new IncompatibleFieldClass();91bar = 4;92/* Excluded due to Bug 408954093abc = new NewExternFieldClass(66);94farray = new NewExternFieldClass[10];95for (int k = 0; k < farray.length; k++)96farray[k] = new NewExternFieldClass(k);97*/98}99}100101/** Test serial persistent fields w/o using Alternate API.102*/103class B implements Serializable {104private static final long serialVersionUID = 2L;105NewFieldClass foo;106NewFieldClass[] array;107IncompatibleFieldClass boo;108IncompatibleFieldClass booarray[];109transient NewExternFieldClass abc;110transient NewExternFieldClass farray[];111int bar;112113B() {114foo = new NewFieldClass(12);115array = new NewFieldClass[12];116for (int i = 0; i < array.length; i++)117array[i] = new NewFieldClass(i);118bar = 4;119abc = new NewExternFieldClass(66);120farray = new NewExternFieldClass[10];121for (int k = 0; k < farray.length; k++)122farray[k] = new NewExternFieldClass(k);123}124}125126/** Test serial persistent fields using Alternate API.127* Also make sure that optional data to non-existent classes can be skipped.128*/129class C implements Serializable {130private static final long serialVersionUID = 3L;131NewFieldClass foo;132NewFieldClass[] array;133int bar;134transient NewExternFieldClass abc;135transient NewExternFieldClass farray[];136137C() {138foo = new NewFieldClass(12);139array = new NewFieldClass[12];140for (int i = 0; i < array.length; i++)141array[i] = new NewFieldClass(i);142bar = 4;143abc = new NewExternFieldClass(3);144farray = new NewExternFieldClass[10];145for (int k = 0; k < farray.length; k++)146farray[k] = new NewExternFieldClass(k);147}148149private void readObject(ObjectInputStream s)150throws IOException, ClassNotFoundException151{152s.defaultReadObject();153/* Excluded due to Bug 4089540154abc = (NewExternFieldClass)fields.get("abc", null);155farray = (NewExternFieldClass[])fields.get("farray", null);156*/157158/* read optional data */159NewFieldClass tmpfoo = (NewFieldClass)s.readObject();160NewFieldClass[] tmparray= (NewFieldClass[])s.readObject();161int tmpbar = s.readInt();162/* Excluded due to Bug 4089540163NewExternFieldClass tmpabc = (NewExternFieldClass)s.readObject();164NewExternFieldClass[] tmpfarray = (NewExternFieldClass[])s.readObject();165*/166}167168private void writeObject(ObjectOutputStream s)169throws IOException170{171s.defaultWriteObject();172173/* write optional data */174s.writeObject(foo);175s.writeObject(array);176s.writeInt(bar);177178/* Excluded due to Bug 4089540179s.writeObject(abc);180s.writeObject(farray);181*/182}183}184185186public class WriteAddedField {187public static void main(String args[]) throws IOException {188A a = new A();189B b = new B();190C c = new C();191File f = new File("tmp.ser");192ObjectOutput out =193new ObjectOutputStream(new FileOutputStream(f));194out.writeObject(a);195out.writeObject(b);196out.writeObject(c);197a = new A();198b = new B();199c = new C();200out.writeObject(a);201out.writeObject(b);202out.writeObject(c);203out.close();204}205}206207208