- java.lang.Object
-
- java.beans.PersistenceDelegate
-
- java.beans.DefaultPersistenceDelegate
-
public class DefaultPersistenceDelegate extends PersistenceDelegate
TheDefaultPersistenceDelegate
is a concrete implementation of the abstractPersistenceDelegate
class and is the delegate used by default for classes about which no information is available. TheDefaultPersistenceDelegate
provides, version resilient, public API-based persistence for classes that follow the JavaBeans™ conventions without any class specific configuration.The key assumptions are that the class has a nullary constructor and that its state is accurately represented by matching pairs of "setter" and "getter" methods in the order they are returned by the Introspector. In addition to providing code-free persistence for JavaBeans, the
DefaultPersistenceDelegate
provides a convenient means to effect persistent storage for classes that have a constructor that, while not nullary, simply requires some property values as arguments.- Since:
- 1.4
- See Also:
DefaultPersistenceDelegate(String[])
,Introspector
-
-
Constructor Summary
Constructors Constructor and Description DefaultPersistenceDelegate()
Creates a persistence delegate for a class with a nullary constructor.DefaultPersistenceDelegate(String[] constructorPropertyNames)
Creates a default persistence delegate for a class with a constructor whose arguments are the values of the property names as specified byconstructorPropertyNames
.
-
Method Summary
Methods Modifier and Type Method and Description protected void
initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)
This default implementation of theinitialize
method assumes all state held in objects of this type is exposed via the matching pairs of "setter" and "getter" methods in the order they are returned by the Introspector.protected Expression
instantiate(Object oldInstance, Encoder out)
This default implementation of theinstantiate
method returns an expression containing the predefined method name "new" which denotes a call to a constructor with the arguments as specified in theDefaultPersistenceDelegate
's constructor.protected boolean
mutatesTo(Object oldInstance, Object newInstance)
If the number of arguments in the specified constructor is non-zero and the class ofoldInstance
explicitly declares an "equals" method this method returns the value ofoldInstance.equals(newInstance)
.-
Methods inherited from class java.beans.PersistenceDelegate
writeObject
-
-
-
-
Constructor Detail
-
DefaultPersistenceDelegate
public DefaultPersistenceDelegate()
Creates a persistence delegate for a class with a nullary constructor.
-
DefaultPersistenceDelegate
public DefaultPersistenceDelegate(String[] constructorPropertyNames)
Creates a default persistence delegate for a class with a constructor whose arguments are the values of the property names as specified byconstructorPropertyNames
. The constructor arguments are created by evaluating the property names in the order they are supplied. To use this class to specify a single preferred constructor for use in the serialization of a particular type, we state the names of the properties that make up the constructor's arguments. For example, theFont
class which does not define a nullary constructor can be handled with the following persistence delegate:new DefaultPersistenceDelegate(new String[]{"name", "style", "size"});
- Parameters:
constructorPropertyNames
- The property names for the arguments of this constructor.- See Also:
instantiate(java.lang.Object, java.beans.Encoder)
-
-
Method Detail
-
mutatesTo
protected boolean mutatesTo(Object oldInstance, Object newInstance)
If the number of arguments in the specified constructor is non-zero and the class ofoldInstance
explicitly declares an "equals" method this method returns the value ofoldInstance.equals(newInstance)
. Otherwise, this method uses the superclass's definition which returns true if the classes of the two instances are equal.- Overrides:
mutatesTo
in classPersistenceDelegate
- 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
. - See Also:
DefaultPersistenceDelegate(String[])
-
instantiate
protected Expression instantiate(Object oldInstance, Encoder out)
This default implementation of theinstantiate
method returns an expression containing the predefined method name "new" which denotes a call to a constructor with the arguments as specified in theDefaultPersistenceDelegate
's constructor.- Specified by:
instantiate
in classPersistenceDelegate
- Parameters:
oldInstance
- The instance to be instantiated.out
- The code output stream.- Returns:
- An expression whose value is
oldInstance
. - Throws:
NullPointerException
- ifout
isnull
- See Also:
DefaultPersistenceDelegate(String[])
-
initialize
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)
This default implementation of theinitialize
method assumes all state held in objects of this type is exposed via the matching pairs of "setter" and "getter" methods in the order they are returned by the Introspector. If a property descriptor defines a "transient" attribute with a value equal toBoolean.TRUE
the property is ignored by this default implementation. Note that this use of the word "transient" is quite independent of the field modifier that is used by theObjectOutputStream
.For each non-transient property, an expression is created in which the nullary "getter" method is applied to the
oldInstance
. The value of this expression is the value of the property in the instance that is being serialized. If the value of this expression in the cloned environmentmutatesTo
the target value, the new value is initialized to make it equivalent to the old value. In this case, because the property value has not changed there is no need to call the corresponding "setter" method and no statement is emitted. If not however, the expression for this value is replaced with another expression (normally a constructor) and the corresponding "setter" method is called to install the new property value in the object. This scheme removes default information from the output produced by streams using this delegate.In passing these statements to the output stream, where they will be executed, side effects are made to the
newInstance
. In most cases this allows the problem of properties whose values depend on each other to actually help the serialization process by making the number of statements that need to be written to the output smaller. In general, the problem of handling interdependent properties is reduced to that of finding an order for the properties in a class such that no property value depends on the value of a subsequent property.- Overrides:
initialize
in classPersistenceDelegate
- 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
- See Also:
Introspector.getBeanInfo(java.lang.Class<?>)
,PropertyDescriptor
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/11/2024 21:56:23 Cette version de la page est en cache (à la date du 21/11/2024 21:56:23) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 31/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/beans/defaultpersistencedelegate.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.