The ObjectStreamClass
provides information about classes that are saved in
a Serialization stream. The descriptor provides the fully-qualified name of the
class and its serialization version UID. A streamVersionUID identifies the
unique original class version for which this class is capable of writing streams
and from which it can read.
package java.io;
public class ObjectStreamClass
{
public static ObjectStreamClass lookup(Class cl);
public String getName();
public Class forClass();
public long getSerialVersionUID();
public String toString();
}
lookup
method returns the ObjectStreamClass
descriptor for the
specified class in the Java VM. If the class has defined serialVersionUID it is
retrieved from the class. If not defined by the class it is computed from the
class's definition in the Java Virtual Machine. null is returned if the specified
class is not Serializable or Externalizable. Only class descriptions for classes
that implement the java.io.Serializable or java.io.Externalizable interfaces can
be written to a stream.
The getName
method returns the fully-qualified name of the class. The class
name is saved in the stream and is used when the class must be loaded.
The forClass
method returns the Class in the local Virtual Machine if one is
known. Otherwise, it returns null.
The getSerialVersionUID
method returns the serialVersionUID of this
class. Refer to Stream Unique Identifiers. If not specified by the
class, the value returned is a hash computed from the class's name, interfaces,
methods, and fields using the Secure Hash Algorithm (SHA) as defined by the
National Institute of Standard.
The toString
method returns a printable representation of the class
descriptor including the class's name and serialVersionUID.
serialver
can be used to find out if a class is serializable and to
get its serialVersionUID
. When invoked with -show it puts up a simple
user interface. To find out if a class is serializable and to find out its
serialVersionUID,
enter its full class name and press either the Enter or the
Show button. The string printed can be copied and pasted into the evolved
class.
static final long SerialVersionUID = 3487495895819393L;
The serialVersionUID is computed using the signature of a stream of bytes that reflect the class definition. The National Institute of Standards and Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a signature for the stream. The first two 32-bit quantities are used to form a 64-bit hash. A java.lang.DataOutputStream is used to convert primitive data types to a sequence of bytes. The values input to the stream are defined by the Java virtual machine (VM) specification for classes. The sequence of items in the stream is as follows: