Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Deserialization doubt  RSS feed

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the basics of serialization and deserialization,why and when is it used etc.

For eg say i serialize an object and pass it over the network to some other layer.
On the receiving end we will deserialize and get the object back.

But what i want to know is that how will the ClassLoader 1) get the CLASSPATH,2)load the class of the object etc on the receiving end .


To be more specific i can say that

When we need to deserialize an Object that particular class file should be there in the class path. how is it possible at the receiving end, how do i get the classpath...

hope my question is clear


Also i want to know when should one use resolve class and annotateClass

 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you deserialize, you will also be running a Java Virtual Machine, whether it's the same one you used to serialize or not, or even if it's on a different machine entirely. Since it's a JVM, you will specify a classpath in the usual way. The JVM that deserializes an object must be able to find a matching class on its classpath and that class must have exactly the same serial version UID as the one used by the serializing JVM.

There are two ways to get a serial version UID. The first is just to let the JVM compute it. It makes a hash of all the fields in your class, and maybe some of the methods ... I forget. In this case, you have to be sure that both JVMs have the same version of the class, probably even made from the same compiler. If you ever, say, add a field, you won't be able to deserialize objects without that field.

The second way is just to explicitly declare a value, like this : private static final long serialVersionUID = 1L; (You can also use serialver from the JDK to compute the correct value for an existing class, which many people do, not realizing it's unnecessary in most cases.) In this case, you turn off the version check, and the deserializer will do its best to resolve incompatibilities ... throwing away fields that no longer exist, setting new fields to default values. You can customize this version resolution by implementing the private readObject() method.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Charles.

So when do i use resolve class and annotateClass ?
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean ObjectOutputStream.annotateClass() and ObjectInputStream.resolveClass()? I never have used them in a long and varied career as a Java developer. The way you would use them is by extending ObjectInputStream and ObjectOutputStream, overriding their default implementations, which do nothing. In theory you could write the byte code of the class as you serialized it, and read it back on deserialization. You'd then have to jigger the class loader on the deserializer to load the class that way.

In fact, some frameworks built on top of serialization do allow delivering classes remotely ... like RMI and EJB. I have no idea if they're implemented using annotate and resolve though.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Charles
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. My name is Greg actually. The western tradition is to put given name first followed by family name ... not that many Americans don't get my name backwards anyway! Maybe we'll standardize that some day. Family name first makes more sense for sorting. We'll probably get to it right about the same time that America completes its conversion to the metric system, you know, shortly after pigs fly.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!