Contents | Previous | Next | JavaTM Object Serialization Specification |
When Java™ objects use serialization to save state in files, or as blobs in databases, the potential arises that the version of a class reading the data is different than the version that wrote the data.
Versioning raises some fundamental questions about the identity of a class, including what constitutes a compatible change. A compatible change is a change that does not affect the contract between the class and its callers.
This section describes the goals, assumptions, and a solution that attempts to address this problem by restricting the kinds of changes allowed and by carefully choosing the mechanisms.
The proposed solution provides a mechanism for “automatic” handling of classes that evolve by adding fields and adding classes. Serialization will handle versioning without class-specific methods to be implemented for each version. The stream format can be traversed without invoking class-specific methods.
The goals are to:
The assumptions are that:
In the evolution of classes, it is the responsibility of the evolved (later version) class to maintain the contract established by the nonevolved class. This takes two forms. First, the evolved class must not break the existing assumptions about the interface provided by the original version, so that the evolved class can be used in place of the original. Secondly, when communicating with the original (or previous) versions, the evolved class must provide sufficient and equivalent information to allow the earlier version to continue to satisfy the nonevolved contract.
For the purposes of the discussion here, each class implements and extends the interface or contract defined by its supertype. New versions of a class, for example foo’
, must continue to satisfy the contract for foo
and may extend the interface or modify its implementation.
Communication between objects via serialization is not part of the contract defined by these interfaces. Serialization is a private protocol between the implementations. It is the responsibility of the implementations to communicate sufficiently to allow each implementation to continue to satisfy the contract expected by its clients.
In the Java™ Language Specification, Chapter 13 discusses binary compatibility of Java™ classes as those classes evolve. Most of the flexibility of binary compatibility comes from the use of late binding of symbolic references for the names of classes, interfaces, fields, methods, and so on.
The following are the principle aspects of the design for versioning of serialized object streams.
writeObject
/readObject
methods supersede the default mechanism to write/read the state of the class. These methods write and read the optional data for a class. The required data is written by calling defaultWriteObject
and read by calling defaultReadObject
.ObjectOutputStream
and ObjectInputStream
may include their own information identifying the class using the annotateClass
method; for example, MarshalOutputStream
embeds the URL of the class.With these concepts, we can now describe how the design will cope with the different cases of an evolving class. The cases are described in terms of a stream written by some version of a class. When the stream is read back by the same version of the class, there is no loss of information or functionality. The stream is the only source of information about the original class. Its class descriptions, while a subset of the original class description, are sufficient to match up the data in the stream with the version of the class being reconstituted.
The descriptions are from the perspective of the stream being read in order to reconstitute either an earlier or later version of the class. In the parlance of RPC systems, this is a “receiver makes right” system. The writer writes its data in the most suitable form and the receiver must interpret that information to extract the parts it needs and to fill in the parts that are not available.
Incompatible changes to classes are those changes for which the guarantee of interoperability cannot be maintained. The incompatible changes that may occur while evolving a class are:
writeObject
or readObject
method so that it no longer writes or reads the default field data or changing it so that it attempts to write it or read it when the previous version did not. The default field data must consistently either appear or not appear in the stream.Serializable
to Externalizable
or visa-versa is an incompatible change since the stream will contain data that is incompatible with the implementation in the available class.Serializable
or Externalizable
is an incompatible change since when written it will no longer supply the fields needed by older versions of the class.writeReplace
or readResolve
method to a class is incompatible if the behavior would produce an object that is incompatible with any older version of the class.The compatible changes to a class are handled as follows:
writeObject
/readObject
methods - If the version reading the stream has these methods then readObject
is expected, as usual, to read the required data written to the stream by the default serialization. It should call defaultReadObject
first before reading any optional data. The writeObject
method is expected as usual to call defaultWriteObject
to write the required data and then may write optional data.writeObject
/readObject
methods - If the class reading the stream does not have these methods, the required data will be read by default serialization, and the optional data will be discarded.java.io.Serializable
- This is equivalent to adding types. There will be no values in the stream for this class so its fields will be initialized to default values. The support for subclassing nonserializable classes requires that the class’s supertype have a no-arg constructor and the class itself will be initialized to default values. If the no-arg constructor is not available, the InvalidClassException
is thrown.
Contents | Previous | Next | JavaTM Object Serialization Specification |
Copyright © 2003 Sun Microsystems, Inc. All rights reserved.