FileDocCategorySizeDatePackage
EvolutionExampleOriginalClass.javaAPI DocExample5567Wed Apr 19 11:21:22 BST 2000None

EvolutionExampleOriginalClass

public class EvolutionExampleOriginalClass extends Object
When Java objects use serialization to store objects, the potential arises that the version of the class reading the data is different from the version of class that wrote that data This example demonstrates some of the compatible changes that Serialization handles without class-specific methods How to Run: Compile Original Class: javac EvolutionExampleOriginalClass.java Run Original Class with serialization flag: java EvolutionExampleOriginalClass -s Compile Evolved Class: javac EvolutionExampleEvolvedClass.java Run Evolved Class with deserialization flag: java EvolutionExampleEvolvedClass -d This tests compatibility in one direction only. Perform the same steps in the other direction to see bidirectional compatibility. Compiled and Tested with JDK1.1.4 & JDK1.2 This file contains the original class. The evolved class is in file called EvolutionExampleEvolvedClass.java

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
There are two options: either a user can serialize an object or deserialize it. (using the -s or -d flag). These options allow for the demonstration of bidirection readability and writeability between the original and the evolved class. In other words, one can serialize an object here and deserialize it with the evolved class or vice versa.

	
	boolean serialize = false;
	boolean deserialize = false;
	/* 
	 * see if we are serializing or deserializing.
	 * The ability to deserialize or serialize allows
	 * us to see the bidirectional readability and writeability
	 */
	if (args.length == 1) {
	    if (args[0].equals("-d")) {
		deserialize = true;
	    } else if (args[0].equals("-s")) {
		serialize = true;
	    } else {
		usage();
		System.exit(0);
	    }
	} else {
	    usage();
	    System.exit(0);
	}
		
	AClass serializeclass = new AClass(10, "serializedByOriginalClass");
	AClass deserializeclass = null;

	/* 
	 * Serialize the original class if that's the option chosen
	 */
	if (serialize) {
	    try {
		FileOutputStream fo = new FileOutputStream("evolve.tmp");
		ObjectOutputStream so = new ObjectOutputStream(fo);
		so.writeObject(serializeclass);
		so.flush();
	    } catch (Exception e) {
		System.out.println(e);
		System.exit(1);
	    }
	}
    
	/* 
	 * Deserialize, if that's the option chosen and print the name
	 * of the object, which will allow us to see who serialized the
	 * object, the original class or the evolved class file
	 */
	if (deserialize) {
	    try {
		FileInputStream fi = new FileInputStream("evolve.tmp");
		ObjectInputStream si = new ObjectInputStream(fi);  
		deserializeclass = (AClass) si.readObject();
	    } catch (Exception e) {
		System.out.println(e);
		System.exit(1);
	    }
	    /* 
	     * Print out to see that it is indeed the same object as it was
	     * when it was serialized (depending on whether it was the original
	     * class that serialized it or the evolved class)
	     */
	    System.out.println("Now printing deserialized object's name: ");
	    System.out.println();
	    System.out.println("name: " + deserializeclass.name);
	    System.out.println();
	}
    
static voidusage()
Prints out the usage

	System.out.println("Usage:");
	System.out.println("      -s (in order to serialize)");
	System.out.println("      -d (in order to deserialize)");