Path: blob/master/test/jdk/java/io/Serializable/evolution/RenamePackage/install/SerialDriver.java
41162 views
/*1* Copyright (c) 1998, 2011, 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*25* @bug 408729526* @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java27* @summary Enable resolveClass() to accommodate package renaming.28* This fix enables one to implement a resolveClass method that maps a29* Serializable class within a serialization stream to the same class30* in a different package within the JVM runtime. See run shell script31* for instructions on how to run this test.32*/3334package install;3536import java.io.*;37import extension.ExtendedObjectInputStream;3839public class SerialDriver implements Serializable {40private static final long serialVersionUID = 1L;4142String name;43SerialDriver next;44transient Object objarray[];4546public SerialDriver() {47name = "<terminator>";48next = null;49}5051public SerialDriver(String name, SerialDriver next) {52this.name = name;53this.next = next;54}5556static boolean serialize;57static boolean deserialize;5859public static void main(String args[]) throws Exception {60SerialDriver obj = new SerialDriver("SerialDriver_2",61new SerialDriver());62SerialDriver[] array = new SerialDriver[5];63for (int i = 0; i < array.length; i++)64array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());6566/*67* see if we are serializing or deserializing.68* The ability to deserialize or serialize allows69* us to see the bidirectional readability and writeability70*/71if (args.length == 1) {72if (args[0].equals("-d")) {73deserialize = true;74} else if (args[0].equals("-s")) {75serialize = true;76} else {77usage();78throw new Exception("incorrect command line arguments");79}80} else {81usage();82throw new Exception("incorrect command line arguments");83}8485File f = new File("stream.ser");86if (serialize) {87// Serialize the subclass88try (FileOutputStream fo = new FileOutputStream(f);89ObjectOutputStream so = new ObjectOutputStream(fo))90{91so.writeObject(obj);92/* Skip arrays since they do not work with rename yet.93The serialVersionUID changes due to the name change94and there is no way to set the serialVersionUID for an95array. */96so.writeObject(array);97} catch (Exception e) {98System.out.println(e);99throw e;100}101}102if (deserialize) {103// Deserialize the subclass104try (FileInputStream fi = new FileInputStream(f);105ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))106{107si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");108si.addRenamedClassName("[Ltest.SerialDriver;",109"[Linstall.SerialDriver");110obj = (SerialDriver) si.readObject();111array = (SerialDriver[]) si.readObject();112} catch (Exception e) {113System.out.println(e);114throw e;115}116System.out.println();117System.out.println("Printing deserialized class: ");118System.out.println();119System.out.println(obj.toString());120System.out.println();121}122}123124125public String toString() {126String nextString = next != null ? next.toString() : "<null>";127return "name =" + name + " next = <" + nextString + ">";128}129130/**131* Prints out the usage132*/133static void usage() {134System.out.println("Usage:");135System.out.println(" -s (in order to serialize)");136System.out.println(" -d (in order to deserialize)");137}138}139140141