APIdia Launch Partner
Top Description Fields Constructors Methods
java.beans

public Class DefaultPersistenceDelegate

extends PersistenceDelegate
Class Inheritance
Known Direct Subclasses
java.beans.MetaData.java_util_Collection_PersistenceDelegate, java.beans.MetaData.java_util_List_PersistenceDelegate, java.beans.MetaData.java_util_Map_PersistenceDelegate, java.beans.MetaData.java_awt_Component_PersistenceDelegate, java.beans.MetaData.java_awt_Container_PersistenceDelegate, java.beans.MetaData.java_awt_Choice_PersistenceDelegate, java.beans.MetaData.java_awt_Menu_PersistenceDelegate, java.beans.MetaData.java_awt_MenuBar_PersistenceDelegate, java.beans.MetaData.java_awt_List_PersistenceDelegate, java.beans.MetaData.java_awt_BorderLayout_PersistenceDelegate, java.beans.MetaData.java_awt_CardLayout_PersistenceDelegate, java.beans.MetaData.java_awt_GridBagLayout_PersistenceDelegate, java.beans.MetaData.javax_swing_JFrame_PersistenceDelegate, java.beans.MetaData.javax_swing_DefaultListModel_PersistenceDelegate, java.beans.MetaData.javax_swing_DefaultComboBoxModel_PersistenceDelegate, java.beans.MetaData.javax_swing_tree_DefaultMutableTreeNode_PersistenceDelegate, java.beans.MetaData.javax_swing_JTabbedPane_PersistenceDelegate, java.beans.MetaData.javax_swing_Box_PersistenceDelegate, java.beans.MetaData.javax_swing_JMenu_PersistenceDelegate
Imports
java.util.*, .Objects, java.lang.reflect.*, sun.reflect.misc.MethodUtil

The DefaultPersistenceDelegate is a concrete implementation of the abstract PersistenceDelegate class and is the delegate used by default for classes about which no information is available. The DefaultPersistenceDelegate 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.

Author
Philip Milne
Since
1.4
See Also
DefaultPersistenceDelegate(String[]), java.beans.Introspector

Field Summary

Modifier and TypeField and Description
private final String[]
private Boolean
private static final String[]

Constructor Summary

AccessConstructor and Description
public
DefaultPersistenceDelegate()

Creates a persistence delegate for a class with a nullary constructor.

public
DefaultPersistenceDelegate(String[]
The property names for the arguments of this constructor.
constructorPropertyNames
)

Creates a default persistence delegate for a class with a constructor whose arguments are the values of the property names as specified by constructorPropertyNames.

Method Summary

Modifier and TypeMethod and Description
private static boolean
private boolean
private void
doProperty(Class<?> type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out)

private Method
findMethod(Class<?> type, String property)

private static PropertyDescriptor
getPropertyDescriptor(Class<?> type, String property)

private void
initBean(Class<?> type, Object oldInstance, Object newInstance, Encoder out)

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
)

Overrides java.beans.PersistenceDelegate.initialize.

This default implementation of the initialize 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

Returns:

An expression whose value is oldInstance.
instantiate
(Object
The instance to be instantiated.
oldInstance
,
Encoder
The code output stream.
out
)

Implements abstract java.beans.PersistenceDelegate.instantiate.

This default implementation of the instantiate method returns an expression containing the predefined method name "new" which denotes a call to a constructor with the arguments as specified in the DefaultPersistenceDelegate's constructor.

pack-priv static void
invokeStatement(Object instance, String methodName, Object[] args, Encoder out)

protected boolean

Returns:

True if an equivalent copy of newInstance may be created by applying a series of mutations to oldInstance.
mutatesTo
(Object
The instance to be copied.
oldInstance
,
Object
The instance that is to be modified.
newInstance
)

Overrides java.beans.PersistenceDelegate.mutatesTo.

If the number of arguments in the specified constructor is non-zero and the class of oldInstance explicitly declares an "equals" method this method returns the value of oldInstance.equals(newInstance).

Inherited from java.beans.PersistenceDelegate:
writeObject

Field Detail

constructorback to summary
private final String[] constructor
definesEqualsback to summary
private Boolean definesEquals
EMPTYback to summary
private static final String[] EMPTY

Constructor Detail

DefaultPersistenceDelegateback to summary
public DefaultPersistenceDelegate()

Creates a persistence delegate for a class with a nullary constructor.

See Also
DefaultPersistenceDelegate(java.lang.String[])
DefaultPersistenceDelegateback to summary
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 by constructorPropertyNames. 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, the Font 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:String[]

The property names for the arguments of this constructor.

See Also
instantiate

Method Detail

definesEqualsback to summary
private static boolean definesEquals(Class<?> type)
definesEqualsback to summary
private boolean definesEquals(Object instance)
doPropertyback to summary
private void doProperty(Class<?> type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception
findMethodback to summary
private Method findMethod(Class<?> type, String property)
getPropertyDescriptorback to summary
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String property)
initBeanback to summary
private void initBean(Class<?> type, Object oldInstance, Object newInstance, Encoder out)
initializeback to summary
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)

Overrides java.beans.PersistenceDelegate.initialize.

This default implementation of the initialize 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 to Boolean.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 the ObjectOutputStream.

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 environment mutatesTo 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.

Parameters
type:Class<?>

the type of the instances

oldInstance:Object

The instance to be copied.

newInstance:Object

The instance that is to be modified.

out:Encoder

The stream to which any initialization statements should be written.

Annotations
@Override
Exceptions
NullPointerException:
if out is null
See Also
java.beans.Introspector#getBeanInfo, java.beans.PropertyDescriptor
instantiateback to summary
protected Expression instantiate(Object oldInstance, Encoder out)

Implements abstract java.beans.PersistenceDelegate.instantiate.

This default implementation of the instantiate method returns an expression containing the predefined method name "new" which denotes a call to a constructor with the arguments as specified in the DefaultPersistenceDelegate's constructor.

Parameters
oldInstance:Object

The instance to be instantiated.

out:Encoder

The code output stream.

Returns:Expression

An expression whose value is oldInstance.

Annotations
@Override
Exceptions
NullPointerException:
if out is null and this value is used in the method
See Also
DefaultPersistenceDelegate(String[])
invokeStatementback to summary
pack-priv static void invokeStatement(Object instance, String methodName, Object[] args, Encoder out)
mutatesToback to summary
protected boolean mutatesTo(Object oldInstance, Object newInstance)

Overrides java.beans.PersistenceDelegate.mutatesTo.

If the number of arguments in the specified constructor is non-zero and the class of oldInstance explicitly declares an "equals" method this method returns the value of oldInstance.equals(newInstance). Otherwise, this method uses the superclass's definition which returns true if the classes of the two instances are equal.

Parameters
oldInstance:Object

The instance to be copied.

newInstance:Object

The instance that is to be modified.

Returns:boolean

True if an equivalent copy of newInstance may be created by applying a series of mutations to oldInstance.

Annotations
@Override
See Also
DefaultPersistenceDelegate(String[])