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

  1. /*
  2.  * @(#)EvolutionExampleEvolvedClass.java    1.1 98/10/03        
  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. import java.util.*;
  24.  
  25. /** 
  26.  * When Java objects use serialization to store objects, the potential arises
  27.  * that the version of the class reading the data is different from the version
  28.  * of class that wrote that data
  29.  
  30.  *
  31.  * This example demonstratres some of the compatible changes that Serialization
  32.  * handles without class-specific methods
  33.  *
  34.  * For directions of How to Run: see the Original Class file.
  35.  *
  36.  * Compiled and Tested with JDK1.2
  37.  * This file contains the evolved class.
  38.  * The original class is in file called EvolutionExampleOriginalClass.java
  39.  */
  40. public class EvolutionExampleEvolvedClass {
  41.  
  42.     /** 
  43.      *  There are two options: either a user can serialize an object or
  44.      *  deserialize it. (using the -s or -d flag). These options allow
  45.      *  for the demonstration of bidirection readability and writeability
  46.      *  between the original and the evolved class. In other words,
  47.      *  one can serialize an object here and deserialize it with the evolved
  48.      *  class or vice versa.
  49.      */
  50.     public static void main(String args[]) {
  51.  
  52.     AClass serializeclass = new AClass(20, "serializedByEvolvedClass");
  53.     AClass deserializeclass = null;
  54.  
  55.         boolean serialize = false;
  56.     boolean deserialize = false;
  57.     /* 
  58.      * see if we are serializing or deserializing.
  59.      * The ability to deserialize or serialize allows
  60.      * us to see the bidirectional readability and writeability
  61.      */
  62.     if (args.length == 1) {
  63.         if (args[0].equals("-d")) {
  64.         deserialize = true;
  65.         } else if (args[0].equals("-s")) {
  66.         serialize = true;
  67.         } else {
  68.         usage();
  69.         System.exit(0);
  70.         }
  71.     } else {
  72.         usage();
  73.         System.exit(0);
  74.     }
  75.     
  76.     /* 
  77.      * Serialize the original class if that's the option chosen
  78.      */
  79.     if (serialize) {
  80.         try {
  81.         FileOutputStream fo = new FileOutputStream("evolve.tmp");
  82.         ObjectOutputStream so = new ObjectOutputStream(fo);
  83.         so.writeObject(serializeclass);
  84.         so.flush();
  85.         } catch (Exception e) {
  86.         System.out.println(e);
  87.         System.exit(1);
  88.         }
  89.     }
  90.     
  91.     /* 
  92.      * Deserialize, if that's the option chosen and print the name
  93.      * of the object, which will allow us to see who serialized the
  94.      * object, the original class or the evolved class file
  95.      */
  96.     if (deserialize) {
  97.         try {
  98.         FileInputStream fi = new FileInputStream("evolve.tmp");
  99.         ObjectInputStream si = new ObjectInputStream(fi);  
  100.         deserializeclass = (AClass) si.readObject();
  101.         } catch (Exception e) {
  102.         System.out.println(e);
  103.         System.exit(1);
  104.         }
  105.         
  106.         /* 
  107.          * Print out to see that it is indeed the same object as it was
  108.          * when it was serialized (depending on whether it was the original
  109.          * class that serialized it or the evolved class)
  110.          */
  111.         System.out.println("Now printing deserialized object's name: ");
  112.         System.out.println();
  113.         System.out.println("name: " + deserializeclass.name);
  114.         System.out.println();
  115.     }
  116.   }
  117.      /** 
  118.       * Prints out the usage
  119.       */
  120.     static void usage() {
  121.     System.out.println("Usage:");
  122.     System.out.println("      -s (in order to serialize)");
  123.     System.out.println("      -d (in order to deserialize)");
  124.     }
  125. }
  126.  
  127.  
  128.     /** 
  129.      * Evolved Class      
  130.      * Has the following compatible changes 
  131.      * 1) add a field
  132.      * 2) change a access to a field (for example -> public to private)
  133.      * 3) Remove the writeObject/readObject methods
  134.      * 4) change a static field to non-static - this is equivalent to adding
  135.      *    a field.
  136.       
  137.      * Compatible changes that are NOT demonstrated by this example
  138.      * 1) Adding classes/Removing classes
  139.      * 2) Adding writeObject/readObject methods
  140.      */
  141. class AClass implements Serializable {
  142.  
  143.     // mandatory suid field (gotten using serialver on the original Aclass)
  144.     static final long serialVersionUID = -6756364686697947626L;
  145.     
  146.     // Change: removed the private
  147.     /**
  148.      * @serial
  149.      */
  150.     int num;
  151.     
  152.     // Change: added this field
  153.     /**
  154.      * @serial
  155.      */
  156.     boolean b;
  157.     
  158.     /**
  159.      * @serial
  160.      */
  161.     String name;
  162.     
  163.     // Change: removed the static.. so this field will now be serialized
  164.     // equivalent to adding a field
  165.     /**
  166.      * @serial
  167.      */
  168.     Hashtable ht = new Hashtable();
  169.     // ...
  170.     // ...
  171.     // ...
  172.     
  173.     AClass(int n, String s) {
  174.     num = n;
  175.     name = s;
  176.     boolean b = true;
  177.     }
  178.     
  179.     // some methods...
  180.     // ...
  181.     // ...
  182.  
  183.     /** 
  184.      * These writeObject and readObject merely defaultwriteObject and
  185.      * defaultreadObject - so they don't do anything.. but they are placed
  186.      * here so that we can show that we can remove them in the evolved 
  187.      * class without any effect.
  188.      *
  189.      * @serialData Write serializable fields. No optional data written.
  190.      */
  191.     private void writeObject(ObjectOutputStream s)
  192.     throws IOException {    
  193.     s.defaultWriteObject();
  194.     }
  195.   
  196.     /** 
  197.      * readObject - just calls defaultreadObject()
  198.      *
  199.      * @serialData Read serializable fields. No optional data read.
  200.      */
  201.     private void readObject(ObjectInputStream s)
  202.     throws IOException, ClassNotFoundException {
  203.         s.defaultReadObject();
  204.     }
  205. }
  206.  
  207.  
  208.  
  209.