readObject and writeObject
methods used by the ObjectOutputStream, streams like
the XMLEncoder which
use this delegation model can have their behavior controlled
independently of the classes themselves. Normally, the class
is the best place to put such information and conventions
can easily be expressed in this delegation scheme to do just that.
Sometimes however, it is the case that a minor problem
in a single class prevents an entire object graph from
being written and this can leave the application
developer with no recourse but to attempt to shadow
the problematic classes locally or use alternative
persistence techniques. In situations like these, the
delegation model gives a relatively clean mechanism for
the application developer to intervene in all parts of the
serialization process without requiring that modifications
be made to the implementation of classes which are not part
of the application itself.
In addition to using a delegation model, this persistence
scheme differs from traditional serialization schemes
in requiring an analog of the writeObject
method without a corresponding readObject
method. The writeObject analog encodes each
instance in terms of its public API and there is no need to
define a readObject analog
since the procedure for reading the serialized form
is defined by the semantics of method invocation as laid
out in the Java Language Specification.
Breaking the dependency between writeObject
and readObject implementations, which may
change from version to version, is the key factor
in making the archives produced by this technique immune
to changes in the private implementations of the classes
to which they refer.
A persistence delegate, may take control of all aspects of the persistence of an object including:
XMLEncoder
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected void | initialize(Class<?>
the type of the instances type, Object The instance to be copied. oldInstance, Object The instance that is to be modified. newInstance, Encoder The stream to which any initialization statements should be written. out)Produce a series of statements with side effects on |
| protected abstract Expression | Returns: An expression whose value isoldInstance.The instance that will be created by this expression. oldInstance, Encoder The stream to which this expression will be written. out)Returns an expression whose value is |
| protected boolean | Returns: True if an equivalent copy ofnewInstance may be
created by applying a series of mutations to oldInstance.The instance to be copied. oldInstance, Object The instance that is to be modified. newInstance)Returns true if an equivalent copy of |
| public void | writeObject(Object
The instance that will be created by this expression. oldInstance, Encoder The stream to which this expression will be written. out)The |
| PersistenceDelegate | back to summary |
|---|---|
| protected PersistenceDelegate() Constructs a | |
| initialize | back to summary |
|---|---|
| protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) Produce a series of statements with side effects on
The implementation typically achieves this goal by producing a series of
"what happened" statements involving the
The default implementation, calls the
| |
| instantiate | back to summary |
|---|---|
| protected abstract Expression instantiate(Object oldInstance, Encoder out) Returns an expression whose value is
Field f = (Field)oldInstance;
return new Expression(f, f.getDeclaringClass(), "getField", new Object[]{f.getName()});
Note that we declare the value of the returned expression so that
the value of the expression (as returned by getValue)
will be identical to oldInstance.
| |
| mutatesTo | back to summary |
|---|---|
| protected boolean mutatesTo(Object oldInstance, Object newInstance) Returns true if an equivalent copy of
The default behavior returns | |
| writeObject | back to summary |
|---|---|
| public void writeObject(Object oldInstance, Encoder out) The
This implementation first checks to see if the stream
has already encountered this object. Next the
| |