Path: blob/master/test/jdk/java/io/Serializable/enum/ignoreSerializationMethods/Test.java
41159 views
/*1* Copyright (c) 2003, 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 483837925* @summary Verify that custom serialization methods defined by enum types are26* not invoked during serialization or deserialization.27*/2829import java.io.*;3031enum Foo {3233foo,34bar {35@SuppressWarnings("serial") /* Incorrect declarations are being tested */36private void writeObject(ObjectOutputStream out) throws IOException {37throw new Error("bar.writeObject invoked");38}39@SuppressWarnings("serial") /* Incorrect declarations are being tested */40private void readObject(ObjectInputStream in)41throws IOException, ClassNotFoundException42{43throw new Error("bar.readObject invoked");44}45@SuppressWarnings("serial") /* Incorrect declarations are being tested */46Object writeReplace() throws ObjectStreamException {47throw new Error("bar.writeReplace invoked");48}49// readResolve cannot be defined until Enum.readResolve is removed50// Object readResolve() throws ObjectStreamException {51// throw new Error("bar.readResolve invoked");52// }53};5455@SuppressWarnings("serial") /* Incorrect use is being tested */56private void writeObject(ObjectOutputStream out) throws IOException {57throw new Error("Foo.writeObject invoked");58}59@SuppressWarnings("serial") /* Incorrect use is being tested */60private void readObject(ObjectInputStream in)61throws IOException, ClassNotFoundException62{63throw new Error("Foo.readObject invoked");64}65@SuppressWarnings("serial") /* Incorrect use is being tested */66Object writeReplace() throws ObjectStreamException {67throw new Error("Foo.writeReplace invoked");68}69// readResolve cannot be defined until Enum.readResolve is removed70// Object readResolve() throws ObjectStreamException {71// throw new Error("Foo.readResolve invoked");72// }73}7475public class Test {76public static void main(String[] args) throws Exception {77ByteArrayOutputStream bout = new ByteArrayOutputStream();78ObjectOutputStream oout = new ObjectOutputStream(bout);79for (Foo f : Foo.values()) {80oout.writeObject(f);81}82oout.close();83ObjectInputStream oin = new ObjectInputStream(84new ByteArrayInputStream(bout.toByteArray()));85for (Foo f : Foo.values()) {86Object obj = oin.readObject();87if (obj != f) {88throw new Error("expected " + f + ", got " + obj);89}90}91}92}939495