Hi, Campbell
Look private static final long serialVersionUID is used by the deserialization process to check if the version of the serialized class corresponds to the deserialized one.
You know you could serialize a class today and deserialize it a year after that. That class could have changed.
When you declare a class serializable
you should declare private static final long serialVersionUID variable. The initial value of variable can be set by yourself, or you could use the serailver tool that comes with the JDK and that you can find in the bin directory, to generate a unique number identifier for the class, or you could ask Eclipse to do it, just as you did.
Now, let's say I have this serializable class.
I am going to put Jedi.class in my personal Apache Web Server path (at
/var/www), so that the class is available through
http://localhost/Jedi.class. Which might perfectly be an atypical way to prove how to use annotateClass() and readResolve(), but it makes a simple example.
Now, in order to user annotateClass() I have to extend the.ObjectOutputStream class, just as you suggested. In this case I am going to save during class annotation that URL path where I can get a class representation for the object I intend to serialize.
Like this:
Now, another JVM is probably going to deserialize your object. You have to make sure if the class is already loaded. In this case I am going to assume that the class was actually not loaded. And I extend a ObjectInputStream in orde to override the readResolve() method, in order to load the class from the previously URL path saved by the annotateClass method.
Like this:
Now, the JVM that serializes the object should do somewhat like this:
Now, the JVM that deserializes the object should do somewhat like this:
When the writeObject method is called, it automatically invoke annotateClass. And when the readObject is invoked, it automatically invoke readResolve().
I would like to know how I can save the binary representation of the class, instead of its URL. I guess I would have to write my own class loader for it.