Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/nestedReplace/NestedReplace.java
38828 views
/*1* Copyright (c) 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 421773725* @clean NestedReplace A B C D26* @build NestedReplace27* @run main NestedReplace28* @summary Ensure that replacement objects can nominate their own replacements,29* so long as the replacement is not the same class as the30* just-replaced object.31*32*/3334import java.io.*;3536class A implements Serializable {37Object writeReplace() throws ObjectStreamException {38return new B();39}40}4142class B implements Serializable {43Object writeReplace() throws ObjectStreamException {44return new C();45}46}4748class C implements Serializable {4950static int writeReplaceCalled = 0;5152Object writeReplace() throws ObjectStreamException {53writeReplaceCalled++;54return new C();55}5657Object readResolve() throws ObjectStreamException {58return new D();59}60}6162class D implements Serializable {63Object readResolve() throws ObjectStreamException {64throw new Error("readResolve() called more than once");65}66}6768public class NestedReplace {69public static void main(String[] args) throws Exception {70ByteArrayOutputStream bout;71ObjectOutputStream oout;72ByteArrayInputStream bin;73ObjectInputStream oin;74Object obj;7576bout = new ByteArrayOutputStream();77oout = new ObjectOutputStream(bout);78oout.writeObject(new A());79oout.flush();80bin = new ByteArrayInputStream(bout.toByteArray());81oin = new ObjectInputStream(bin);82obj = oin.readObject();8384if (! (obj instanceof D))85throw new Error("Deserialized object is of wrong class");86if (C.writeReplaceCalled != 1)87throw new Error("C.writeReplace() should only get called once");88}89}909192