1. /*
  2. * @(#)PersistenceDelegate.java 1.11 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.beans;
  8. /**
  9. * The PersistenceDelegate class takes the responsibility
  10. * for expressing the state of an instance of a given class
  11. * in terms of the methods in the class's public API. Instead
  12. * of associating the responsibility of persistence with
  13. * the class itself as is done, for example, by the
  14. * <code>readObject</code> and <code>writeObject</code>
  15. * methods used by the <code>ObjectOutputStream</code>, streams like
  16. * the <code>XMLEncoder</code> which
  17. * use this delegation model can have their behavior controlled
  18. * independently of the classes themselves. Normally, the class
  19. * is the best place to put such information and conventions
  20. * can easily be expressed in this delegation scheme to do just that.
  21. * Sometimes however, it is the case that a minor problem
  22. * in a single class prevents an entire object graph from
  23. * being written and this can leave the application
  24. * developer with no recourse but to attempt to shadow
  25. * the problematic classes locally or use alternative
  26. * persistence techniques. In situations like these, the
  27. * delegation model gives a relatively clean mechanism for
  28. * the application developer to intervene in all parts of the
  29. * serialization process without requiring that modifications
  30. * be made to the implementation of classes which are not part
  31. * of the application itself.
  32. * <p>
  33. * In addition to using a delegation model, this persistence
  34. * scheme differs from traditional serialization schemes
  35. * in requiring an analog of the <code>writeObject</code>
  36. * method without a corresponding <code>readObject</code>
  37. * method. The <code>writeObject</code> analog encodes each
  38. * instance in terms of its public API and there is no need to
  39. * define a <code>readObject</code> analog
  40. * since the procedure for reading the serialized form
  41. * is defined by the semantics of method invocation as laid
  42. * out in the Java Language Specification.
  43. * Breaking the dependency between <code>writeObject</code>
  44. * and <code>readObject</code> implementations, which may
  45. * change from version to version, is the key factor
  46. * in making the archives produced by this technique immune
  47. * to changes in the private implementations of the classes
  48. * to which they refer.
  49. * <p>
  50. * A persistence delegate, may take control of all
  51. * aspects of the persistence of an object including:
  52. * <ul>
  53. * <li>
  54. * Deciding whether or not an instance can be mutated
  55. * into another instance of the same class.
  56. * <li>
  57. * Instantiating the object, either by calling a
  58. * public constructor or a public factory method.
  59. * <li>
  60. * Performing the initialization of the object.
  61. * </ul>
  62. * @see XMLEncoder
  63. *
  64. * @since 1.4
  65. *
  66. * @version 1.11 05/05/04
  67. * @author Philip Milne
  68. */
  69. public abstract class PersistenceDelegate {
  70. /**
  71. * The <code>writeObject</code> is a single entry point to the persistence
  72. * and is used by a <code>Encoder</code> in the traditional
  73. * mode of delegation. Although this method is not final,
  74. * it should not need to be subclassed under normal circumstances.
  75. * <p>
  76. * This implementation first checks to see if the stream
  77. * has already encountered this object. Next the
  78. * <code>mutatesTo</code> method is called to see if
  79. * that candidate returned from the stream can
  80. * be mutated into an accurate copy of <code>oldInstance</code>.
  81. * If it can, the <code>initialize</code> method is called to
  82. * perform the initialization. If not, the candidate is removed
  83. * from the stream, and the <code>instantiate</code> method
  84. * is called to create a new candidate for this object.
  85. *
  86. * @param oldInstance The instance that will be created by this expression.
  87. * @param out The stream to which this expression will be written.
  88. * @return An expression whose value is <code>oldInstance</code>.
  89. */
  90. public void writeObject(Object oldInstance, Encoder out) {
  91. Object newInstance = out.get(oldInstance);
  92. if (!mutatesTo(oldInstance, newInstance)) {
  93. out.remove(oldInstance);
  94. out.writeExpression(instantiate(oldInstance, out));
  95. }
  96. else {
  97. initialize(oldInstance.getClass(), oldInstance, newInstance, out);
  98. }
  99. }
  100. /**
  101. * Returns true if an <em>equivalent</em> copy of <code>oldInstance</code> may be
  102. * created by applying a series of statements to <code>newInstance</code>.
  103. * In the specification of this method, we mean by equivalent that the modified instance
  104. * is indistinguishable from <code>oldInstance</code> in the behavior
  105. * of the relevant methods in its public API. [Note: we use the
  106. * phrase <em>relevant</em> methods rather than <em>all</em> methods
  107. * here only because, to be strictly correct, methods like <code>hashCode</code>
  108. * and <code>toString</code> prevent most classes from producing truly
  109. * indistinguishable copies of their instances].
  110. * <p>
  111. * The default behavior returns <code>true</code>
  112. * if the classes of the two instances are the same.
  113. *
  114. * @param oldInstance The instance to be copied.
  115. * @param newInstance The instance that is to be modified.
  116. * @return True if an equivalent copy of <code>newInstance</code> may be
  117. * created by applying a series of mutations to <code>oldInstance</code>.
  118. */
  119. protected boolean mutatesTo(Object oldInstance, Object newInstance) {
  120. return (newInstance != null && oldInstance != null &&
  121. oldInstance.getClass() == newInstance.getClass());
  122. }
  123. /**
  124. * Returns an expression whose value is <code>oldInstance</code>.
  125. * This method is used to characterize the constructor
  126. * or factory method that should be used to create the given object.
  127. * For example, the <code>instantiate</code> method of the persistence
  128. * delegate for the <code>Field</code> class could be defined as follows:
  129. * <pre>
  130. * Field f = (Field)oldInstance;
  131. * return new Expression(f, f.getDeclaringClass(), "getField", new Object[]{f.getName()});
  132. * </pre>
  133. * Note that we declare the value of the returned expression so that
  134. * the value of the expression (as returned by <code>getValue</code>)
  135. * will be identical to <code>oldInstance</code>.
  136. *
  137. * @param oldInstance The instance that will be created by this expression.
  138. * @param out The stream to which this expression will be written.
  139. * @return An expression whose value is <code>oldInstance</code>.
  140. */
  141. protected abstract Expression instantiate(Object oldInstance, Encoder out);
  142. /**
  143. * Produce a series of statements with side effects on <code>newInstance</code>
  144. * so that the new instance becomes <em>equivalent</em> to <code>oldInstance</code>.
  145. * In the specification of this method, we mean by equivalent that, after the method
  146. * returns, the modified instance is indistinguishable from
  147. * <code>newInstance</code> in the behavior of all methods in its
  148. * public API.
  149. * <p>
  150. * The implementation typically achieves this goal by producing a series of
  151. * "what happened" statements involving the <code>oldInstance</code>
  152. * and its publicly available state. These statements are sent
  153. * to the output stream using its <code>writeExpression</code>
  154. * method which returns an expression involving elements in
  155. * a cloned environment simulating the state of an input stream during
  156. * reading. Each statement returned will have had all instances
  157. * the old environment replaced with objects which exist in the new
  158. * one. In particular, references to the target of these statements,
  159. * which start out as references to <code>oldInstance</code> are returned
  160. * as references to the <code>newInstance</code> instead.
  161. * Executing these statements effects an incremental
  162. * alignment of the state of the two objects as a series of
  163. * modifications to the objects in the new environment.
  164. * By the time the initialize method returns it should be impossible
  165. * to tell the two instances apart by using their public APIs.
  166. * Most importantly, the sequence of steps that were used to make
  167. * these objects appear equivalent will have been recorded
  168. * by the output stream and will form the actual output when
  169. * the stream is flushed.
  170. * <p>
  171. * The default implementation, calls the <code>initialize</code>
  172. * method of the type's superclass.
  173. *
  174. * @param oldInstance The instance to be copied.
  175. * @param newInstance The instance that is to be modified.
  176. * @param out The stream to which any initialization statements should be written.
  177. */
  178. protected void initialize(Class<?> type,
  179. Object oldInstance, Object newInstance,
  180. Encoder out)
  181. {
  182. Class superType = type.getSuperclass();
  183. PersistenceDelegate info = out.getPersistenceDelegate(superType);
  184. info.initialize(superType, oldInstance, newInstance, out);
  185. }
  186. }