0N/A/*
3909N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * @bug 4087295
0N/A * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
0N/A * @summary Enable resolveClass() to accommodate package renaming.
0N/A * This fix enables one to implement a resolveClass method that maps a
3662N/A * Serializable class within a serialization stream to the same class
0N/A * in a different package within the JVM runtime. See run shell script
0N/A * for instructions on how to run this test.
0N/A */
0N/A
0N/Apackage install;
0N/A
0N/Aimport java.io.*;
0N/Aimport extension.ExtendedObjectInputStream;
0N/A
0N/Apublic class SerialDriver implements Serializable {
0N/A private static final long serialVersionUID = 1L;
0N/A
0N/A String name;
0N/A SerialDriver next;
0N/A transient Object objarray[];
0N/A
0N/A public SerialDriver() {
0N/A name = "<terminator>";
0N/A next = null;
0N/A }
0N/A
0N/A public SerialDriver(String name, SerialDriver next) {
0N/A this.name = name;
0N/A this.next = next;
0N/A }
0N/A
0N/A static boolean serialize;
0N/A static boolean deserialize;
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A SerialDriver obj = new SerialDriver("SerialDriver_2",
0N/A new SerialDriver());
0N/A SerialDriver[] array = new SerialDriver[5];
0N/A for (int i = 0; i < array.length; i++)
0N/A array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
0N/A
0N/A /*
0N/A * see if we are serializing or deserializing.
0N/A * The ability to deserialize or serialize allows
0N/A * us to see the bidirectional readability and writeability
0N/A */
0N/A if (args.length == 1) {
0N/A if (args[0].equals("-d")) {
0N/A deserialize = true;
0N/A } else if (args[0].equals("-s")) {
0N/A serialize = true;
0N/A } else {
0N/A usage();
0N/A throw new Exception("incorrect command line arguments");
0N/A }
0N/A } else {
0N/A usage();
0N/A throw new Exception("incorrect command line arguments");
0N/A }
0N/A
0N/A File f = new File("stream.ser");
0N/A if (serialize) {
0N/A // Serialize the subclass
3662N/A try (FileOutputStream fo = new FileOutputStream(f);
3662N/A ObjectOutputStream so = new ObjectOutputStream(fo))
3662N/A {
0N/A so.writeObject(obj);
0N/A /* Skip arrays since they do not work with rename yet.
0N/A The serialVersionUID changes due to the name change
0N/A and there is no way to set the serialVersionUID for an
0N/A array. */
0N/A so.writeObject(array);
0N/A } catch (Exception e) {
0N/A System.out.println(e);
0N/A throw e;
0N/A }
0N/A }
0N/A if (deserialize) {
0N/A // Deserialize the subclass
3662N/A try (FileInputStream fi = new FileInputStream(f);
3662N/A ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
3662N/A {
0N/A si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
0N/A si.addRenamedClassName("[Ltest.SerialDriver;",
0N/A "[Linstall.SerialDriver");
0N/A obj = (SerialDriver) si.readObject();
0N/A array = (SerialDriver[]) si.readObject();
0N/A } catch (Exception e) {
0N/A System.out.println(e);
0N/A throw e;
0N/A }
0N/A System.out.println();
0N/A System.out.println("Printing deserialized class: ");
0N/A System.out.println();
0N/A System.out.println(obj.toString());
0N/A System.out.println();
0N/A }
0N/A }
0N/A
0N/A
0N/A public String toString() {
0N/A String nextString = next != null ? next.toString() : "<null>";
0N/A return "name =" + name + " next = <" + nextString + ">";
0N/A }
0N/A
0N/A /**
0N/A * Prints out the usage
0N/A */
0N/A static void usage() {
0N/A System.out.println("Usage:");
0N/A System.out.println(" -s (in order to serialize)");
0N/A System.out.println(" -d (in order to deserialize)");
0N/A }
0N/A}