Loading serialized classes
What is the easiest way to load a class that has been serialized using the standard Jav开发者_StackOverflow社区a Serialization API?
Is there any trivial relationship between the .class
format and the serialized form of a Class<?>
instance?
Is there any trivial relationship between the .class format and the serialized form of a Class instance?
A Serializable class when compiled gets a identifier called the serialVersionUID (SUID). Each object that is serialized using this version of the class also gets stamped with this SUID.
What is the easiest way to load a class that has been serialized using the standard Java Serialization API?
Depends on what you are trying to achieve, *.class file as you would already know, consists of byte code in a series of bytes. Objects dont replicate the byte codes, so you cannot recover the byte series of its class from the serialized object. But you have the serialVersionUID as a final long variable in the serialized object. Using this information you can match the appropriate version of the *.class.
But thats more said than done, you would come across lots of interesting stuff while actually implementing this 'adaptive' loading.
If you are asking whether you can serialize a Class<?>
object and then use that serialized file as a .class
file to load in a ClassLoader, then the answer is no.
Object serialization means you are saving the object state as a sequence of bytes. This is not the same as bytecode.
What you need to do to read the class file and converts into byte[] and write them into a file.
Then when deserlalizing, you would need load byte[] from files and define the class by default system classloader or self-defined classloader depends on your need.
精彩评论