1 /* 2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @bug 4075221 26 * @summary Enable serialize of nonSerializable Class descriptor. 27 */ 28 29 import java.io.EOFException; 30 import java.io.File; 31 import java.io.FileOutputStream; 32 import java.io.FileInputStream; 33 import java.io.InvalidClassException; 34 import java.io.ObjectInputStream; 35 import java.io.ObjectOutputStream; 36 import java.io.ObjectStreamClass; 37 38 class TestEntry { main(String args[])39 public static void main(String args[]) throws Exception { 40 File f = new File("tmp.ser"); 41 if (args[0].compareTo("-s") == 0) { 42 FileOutputStream of = new FileOutputStream(f); 43 ObjectOutputStream oos = new ObjectOutputStream(of); 44 Class cl = Class.forName(args[1]); 45 oos.writeObject(cl); 46 if (ObjectStreamClass.lookup(cl) != null) 47 oos.writeObject(cl.newInstance()); 48 oos.close(); 49 System.out.println("Serialized Class " + cl.getName()); 50 } else if (args[0].compareTo("-de") == 0) { 51 FileInputStream inf = new FileInputStream(f); 52 ObjectInputStream ois = new ObjectInputStream(inf); 53 Class cl = null; 54 try { 55 cl = (Class)ois.readObject(); 56 throw new Error("Expected InvalidClassException to be thrown"); 57 } catch (InvalidClassException e) { 58 System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); 59 } 60 ois.close(); 61 } else if (args[0].compareTo("-doe") == 0) { 62 FileInputStream inf = new FileInputStream(f); 63 ObjectInputStream ois = new ObjectInputStream(inf); 64 Class cl = null; 65 cl = (Class)ois.readObject(); 66 try { 67 ois.readObject(); 68 throw new Error("Expected InvalidClassException to be thrown"); 69 } catch (InvalidClassException e) { 70 System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); 71 } 72 ois.close(); 73 } else if (args[0].compareTo("-d") == 0) { 74 FileInputStream inf = new FileInputStream(f); 75 ObjectInputStream ois = new ObjectInputStream(inf); 76 Class cl = (Class)ois.readObject(); 77 try { 78 ois.readObject(); 79 } catch (EOFException e) { 80 } 81 ois.close(); 82 System.out.println("DeSerialized Class " + cl.getName()); 83 } else { 84 throw new RuntimeException("Unrecognized argument"); 85 } 86 } 87 } 88