Path: blob/master/test/jdk/java/io/Serializable/evolution/AddedExternField/WriteAddedField.java
41161 views
/*1* Copyright (c) 1998, 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*26* @clean A D NewExternFieldClass ReadAddedField WriteAddedField27* @compile WriteAddedField.java28* @run main WriteAddedField29* @clean A D NewExternFieldClass ReadAddedField WriteAddedField30* @compile ReadAddedField.java31* @run main ReadAddedField32*33* @summary Evolution: read evolved class with new field of a non-existing Externalizable class.34*/35import java.io.*;3637class NewExternFieldClass implements Externalizable {38private static final long serialVersionUID = 1L;3940byte l;4142public NewExternFieldClass() {43l = 0;44}4546public NewExternFieldClass(byte value) {47l = value;48}4950public void readExternal(ObjectInput s)51throws IOException, ClassNotFoundException52{53l = s.readByte();54System.out.println("readExternal read " + l);55}5657public void writeExternal(ObjectOutput s) throws IOException58{59s.writeByte(l);60}61}6263class D implements Serializable {64private static final long serialVersionUID = 1L;6566public int x;67D(int y) {68x = y;69}70}7172class A implements Serializable {73// Version 1.1 of class A. Added superclass NewSerializableSuper.74private static final long serialVersionUID = 1L;75NewExternFieldClass foo;76D zoo;7778int bar;79A() {80bar = 4;81foo = new NewExternFieldClass((byte)66);82zoo = new D(22);83}84}8586public class WriteAddedField {87public static void main(String args[]) throws IOException {88A a = new A();89File f = new File("tmp.ser");90ObjectOutput out =91new ObjectOutputStream(new FileOutputStream(f));92out.writeObject(a);93out.writeObject(new A());94out.close();95}96}979899