Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/class/Test.java
38828 views
/*1* Copyright (c) 1998, 2001, 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/*24* @bug 407522125* @summary Enable serialize of nonSerializable Class descriptor.26*/2728import java.io.*;2930class Test {31public static void main(String args[]) throws Exception {32File f = new File("tmp.ser");33if (args[0].compareTo("-s") == 0) {34FileOutputStream of = new FileOutputStream(f);35ObjectOutputStream oos = new ObjectOutputStream(of);36Class cl = Class.forName(args[1]);37oos.writeObject(cl);38if (ObjectStreamClass.lookup(cl) != null)39oos.writeObject(cl.newInstance());40oos.close();41System.out.println("Serialized Class " + cl.getName());42} else if (args[0].compareTo("-de") == 0) {43FileInputStream inf = new FileInputStream(f);44ObjectInputStream ois = new ObjectInputStream(inf);45Class cl = null;46try {47cl = (Class)ois.readObject();48throw new Error("Expected InvalidClassException to be thrown");49} catch (InvalidClassException e) {50System.out.println("Caught expected exception DeSerializing class " + e.getMessage());51}52ois.close();53} else if (args[0].compareTo("-doe") == 0) {54FileInputStream inf = new FileInputStream(f);55ObjectInputStream ois = new ObjectInputStream(inf);56Class cl = null;57cl = (Class)ois.readObject();58try {59ois.readObject();60throw new Error("Expected InvalidClassException to be thrown");61} catch (InvalidClassException e) {62System.out.println("Caught expected exception DeSerializing class " + e.getMessage());63}64ois.close();65} else if (args[0].compareTo("-d") == 0) {66FileInputStream inf = new FileInputStream(f);67ObjectInputStream ois = new ObjectInputStream(inf);68Class cl = (Class)ois.readObject();69try {70ois.readObject();71} catch (EOFException e) {72}73ois.close();74System.out.println("DeSerialized Class " + cl.getName());75}76}77}787980