- java.lang.Object
-
- java.beans.PersistenceDelegate
-
- Direct Known Subclasses:
- DefaultPersistenceDelegate
public abstract class PersistenceDelegate extends Object
The PersistenceDelegate class takes the responsibility for expressing the state of an instance of a given class in terms of the methods in the class's public API. Instead of associating the responsibility of persistence with the class itself as is done, for example, by thereadObject
andwriteObject
methods used by theObjectOutputStream
, streams like theXMLEncoder
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 correspondingreadObject
method. ThewriteObject
analog encodes each instance in terms of its public API and there is no need to define areadObject
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 betweenwriteObject
andreadObject
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:
- Deciding whether or not an instance can be mutated into another instance of the same class.
- Instantiating the object, either by calling a public constructor or a public factory method.
- Performing the initialization of the object.
- Since:
- 1.4
- See Also:
XMLEncoder
-
-
Constructor Summary
Constructors Constructor and Description PersistenceDelegate()
-
Method Summary
Methods Modifier and Type Method and Description protected void
initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)
Produce a series of statements with side effects onnewInstance
so that the new instance becomes equivalent tooldInstance
.protected abstract Expression
instantiate(Object oldInstance, Encoder out)
Returns an expression whose value isoldInstance
.protected boolean
mutatesTo(Object oldInstance, Object newInstance)
Returns true if an equivalent copy ofoldInstance
may be created by applying a series of statements tonewInstance
.void
writeObject(Object oldInstance, Encoder out)
ThewriteObject
is a single entry point to the persistence and is used by aEncoder
in the traditional mode of delegation.
-
-
-
Method Detail
-
writeObject
public void writeObject(Object oldInstance, Encoder out)
ThewriteObject
is a single entry point to the persistence and is used by aEncoder
in the traditional mode of delegation. Although this method is not final, it should not need to be subclassed under normal circumstances.This implementation first checks to see if the stream has already encountered this object. Next the
mutatesTo
method is called to see if that candidate returned from the stream can be mutated into an accurate copy ofoldInstance
. If it can, theinitialize
method is called to perform the initialization. If not, the candidate is removed from the stream, and theinstantiate
method is called to create a new candidate for this object.- Parameters:
oldInstance
- The instance that will be created by this expression.out
- The stream to which this expression will be written.- Throws:
NullPointerException
- ifout
isnull
-
mutatesTo
protected boolean mutatesTo(Object oldInstance, Object newInstance)
Returns true if an equivalent copy ofoldInstance
may be created by applying a series of statements tonewInstance
. In the specification of this method, we mean by equivalent that the modified instance is indistinguishable fromoldInstance
in the behavior of the relevant methods in its public API. [Note: we use the phrase relevant methods rather than all methods here only because, to be strictly correct, methods likehashCode
andtoString
prevent most classes from producing truly indistinguishable copies of their instances].The default behavior returns
true
if the classes of the two instances are the same.- Parameters:
oldInstance
- The instance to be copied.newInstance
- The instance that is to be modified.- Returns:
- True if an equivalent copy of
newInstance
may be created by applying a series of mutations tooldInstance
.
-
instantiate
protected abstract Expression instantiate(Object oldInstance, Encoder out)
Returns an expression whose value isoldInstance
. This method is used to characterize the constructor or factory method that should be used to create the given object. For example, theinstantiate
method of the persistence delegate for theField
class could be defined as follows: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 bygetValue
) will be identical tooldInstance
.- Parameters:
oldInstance
- The instance that will be created by this expression.out
- The stream to which this expression will be written.- Returns:
- An expression whose value is
oldInstance
. - Throws:
NullPointerException
- ifout
isnull
-
initialize
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)
Produce a series of statements with side effects onnewInstance
so that the new instance becomes equivalent tooldInstance
. In the specification of this method, we mean by equivalent that, after the method returns, the modified instance is indistinguishable fromnewInstance
in the behavior of all methods in its public API.The implementation typically achieves this goal by producing a series of "what happened" statements involving the
oldInstance
and its publicly available state. These statements are sent to the output stream using itswriteExpression
method which returns an expression involving elements in a cloned environment simulating the state of an input stream during reading. Each statement returned will have had all instances the old environment replaced with objects which exist in the new one. In particular, references to the target of these statements, which start out as references tooldInstance
are returned as references to thenewInstance
instead. Executing these statements effects an incremental alignment of the state of the two objects as a series of modifications to the objects in the new environment. By the time the initialize method returns it should be impossible to tell the two instances apart by using their public APIs. Most importantly, the sequence of steps that were used to make these objects appear equivalent will have been recorded by the output stream and will form the actual output when the stream is flushed.The default implementation, calls the
initialize
method of the type's superclass.- Parameters:
oldInstance
- The instance to be copied.newInstance
- The instance that is to be modified.out
- The stream to which any initialization statements should be written.- Throws:
NullPointerException
- ifout
isnull
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-java/beans/persistencedelegate.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.