home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / evolvenewsuper / EvolvedClass.java next >
Encoding:
Java Source  |  1999-09-19  |  5.6 KB  |  221 lines

  1. /*
  2.  * @(#)EvolvedClass.java    1.2 98/10/01        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23.  
  24. /**
  25.  * This example shows how to use versioning in serialization to evolve
  26.  * the class to have a new superclass. 
  27.  * 
  28.  * For example if initially the class structure was:
  29.  *
  30.  * class A {...};
  31.  * class C extends A {...};
  32.  *
  33.  * and the evolved clss structure looks like:
  34.  * 
  35.  * class A {...};
  36.  * class B extends A {...};
  37.  * class C extends B {...};
  38.  *
  39.  * then it should be possible to read the old version of C with
  40.  * the new version of C and vice versa.
  41.  *
  42.  * This example demonstrates this. 
  43.  *
  44.  * NOTE: In this example, the superclasses (A and B in the above example)
  45.  * implement the Serializable interface. If they did not, then it would
  46.  * be the responsibility of the subclass C to save and restore the fields of
  47.  * A and B. See the relevant example called: 
  48.  * "Serialization with a NonSerializable Superclass" 
  49.  *
  50.  * If you want to run this: see file OriginalClass.java
  51.  *
  52.  *
  53.  * Compiled and Tested with JDK1.2
  54.  * This file contains the evolved class.
  55.  * The original class is in file called OriginalClass.java
  56.  */
  57.  
  58. public class EvolvedClass {
  59.  
  60.     /** 
  61.      *  There are two options: either a user can serialize an object or
  62.      *  deserialize it. (using the -s or -d flag). These options allow
  63.      *  for the demonstration of bidirection readability and writeability
  64.      *  between the original and the evolved class. In other words,
  65.      *  one can serialize an object here and deserialize it with the evolved
  66.      *  class or vice versa.
  67.      */
  68.     public static void main(String args[]) {
  69.     ASubClass corg = new ASubClass(1, "SerializedByEvolvedClass", 'a');
  70.     ASubClass cnew = null;
  71.     boolean serialize = false;
  72.     boolean deserialize = false;
  73.  
  74.     /* 
  75.      * see if we are serializing or deserializing.
  76.          * The ability to deserialize or serialize allows
  77.          * us to see the bidirectional readability and writeability
  78.          */
  79.         if (args.length == 1) {
  80.             if (args[0].equals("-d")) {
  81.                 deserialize = true;
  82.             } else if (args[0].equals("-s")) {
  83.                 serialize = true;
  84.             } else {
  85.                 usage();
  86.                 System.exit(0);
  87.             }
  88.         } else {
  89.             usage();
  90.             System.exit(0);
  91.         }
  92.  
  93.     if (serialize) {
  94.         // Serialize the original class object
  95.         try {
  96.         FileOutputStream fo = new FileOutputStream("tmp");
  97.         ObjectOutputStream so = new ObjectOutputStream(fo);
  98.         so.writeObject(corg);
  99.         so.flush();
  100.         } catch (Exception e) {
  101.         System.out.println(e);
  102.         System.exit(1);
  103.         }
  104.     }
  105.  
  106.     if (deserialize) {
  107.         // Deserialize in to new class object
  108.         try {
  109.         FileInputStream fi = new FileInputStream("tmp");
  110.         ObjectInputStream si = new ObjectInputStream(fi);  
  111.         cnew = (ASubClass) si.readObject();
  112.         } catch (Exception e) {
  113.         System.out.println(e);
  114.         System.exit(1);
  115.         }
  116.         System.out.println();
  117.         System.out.println("Printing the deserialized class: ");
  118.         System.out.println();
  119.         System.out.println(cnew);
  120.         System.out.println();
  121.     }
  122.     }
  123.  
  124.     /** 
  125.      * Prints out the usage
  126.      */
  127.     static void usage() {
  128.         System.out.println("Usage:");
  129.         System.out.println("      -s (in order to serialize)");
  130.         System.out.println("      -d (in order to deserialize)");
  131.     }
  132. }
  133.  
  134. /** 
  135.  * A simple superclass that implements serializable
  136.  */
  137. class ASuperClass implements Serializable {
  138.  
  139.     /**
  140.      * @serial
  141.      */
  142.     String name;
  143.     
  144.     /* 
  145.      * mandatory field obtained using the command SerialVer on the
  146.      * original "ASuperClass"
  147.      */
  148.     static final long serialVersionUID = 8904068738375916775L;
  149.  
  150.     ASuperClass(String name) {
  151.     this.name = name;
  152.     }
  153.  
  154.     public String toString() {
  155.     return("Name: " + name);
  156.     }
  157. }
  158.  
  159. /** 
  160.  * The new superclass that has been added - also implements
  161.  * Serializable
  162.  */
  163. class AMiddleSuperClass extends ASuperClass implements Serializable {
  164.  
  165.     /**
  166.      * @serial
  167.      */
  168.     char ch;
  169.  
  170.     AMiddleSuperClass(String name, char ch) {
  171.     super(name);
  172.     this.ch = ch;
  173.     }
  174.  
  175.     public String toString() {
  176.     return(super.toString() + "\nCh:   " + ch);
  177.     }
  178. }
  179.  
  180. /** 
  181.  * The subclass that extends the new super class and implements
  182.  * serializable
  183.  *
  184.  * Again note, if the superclasses were not serializable, then it would
  185.  * be the responsibility of this subclass to save and restore the
  186.  * superclass's fields. See example called "Serialization with a
  187.  * NonSerializable Superclass" for more details.
  188.  */
  189. class ASubClass extends AMiddleSuperClass implements Serializable {
  190.     
  191.     /**
  192.      * @serial
  193.      */
  194.     int num;
  195.  
  196.     /* 
  197.      * mandatory field obtained using the command SerialVer on the
  198.      * original "ASubClass"
  199.      */
  200.     static final long serialVersionUID = 1099529196579429023L;
  201.  
  202.     ASubClass(int num, String name, char c) {
  203.     super(name, c);
  204.     this.num = num;
  205.     }
  206.  
  207.     public String toString() {
  208.     return (super.toString() + "\nNum:  " + num);
  209.     }
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.