jdk/test/java/io/Serializable/unresolvableObjectStreamClass/UnresolvableObjectStreamClass.java
changeset 23928 b610d940a61d
equal deleted inserted replaced
23927:225d01ae6469 23928:b610d940a61d
       
     1 /*
       
     2  * Copyright (c) 2014, 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 /* @test
       
    25  * @bug 8039396
       
    26  * @run main UnresolvableObjectStreamClass serialize
       
    27  * @clean MySerializable
       
    28  * @run main UnresolvableObjectStreamClass deserialize
       
    29  *
       
    30  * @summary NPE when writing a class descriptor object to a custom
       
    31  *          ObjectOutputStream
       
    32  */
       
    33 
       
    34 import java.io.*;
       
    35 
       
    36 public class UnresolvableObjectStreamClass {
       
    37     public static void main(String[] args) throws Throwable {
       
    38         if (args.length > 0 && args[0].equals("serialize")) {
       
    39             try (FileOutputStream fos = new FileOutputStream("temp1.ser");
       
    40                  ObjectOutputStream oos = new ObjectOutputStream(fos)) {
       
    41                 ObjectStreamClass osc =
       
    42                          ObjectStreamClass.lookup(MySerializable.class);
       
    43                 oos.writeObject(osc);
       
    44             }
       
    45         } else if (args.length > 0 && args[0].equals("deserialize")) {
       
    46             try (FileInputStream fis = new FileInputStream("temp1.ser");
       
    47                  ObjectInputStream ois = new ObjectInputStream(fis);
       
    48                  FileOutputStream fos = new FileOutputStream("temp2.ser");
       
    49                  ObjectOutputStream oos = new ObjectOutputStream(fos) {
       
    50                          /*must be subclassed*/}) {
       
    51                 ObjectStreamClass osc = (ObjectStreamClass)ois.readObject();
       
    52                 // serialize it again
       
    53                 try {
       
    54                     oos.writeObject(osc);
       
    55                 } catch (NullPointerException e) {
       
    56                     throw new RuntimeException("Failed to write" +
       
    57                             " unresolvable ObjectStreamClass", e);
       
    58                 }
       
    59             }
       
    60         } else {
       
    61             throw new RuntimeException("The command line option must be" +
       
    62                                        " one of: serialize or deserialize");
       
    63         }
       
    64     }
       
    65 }
       
    66 
       
    67 class MySerializable implements Serializable {
       
    68 }