DefaultPersistenceDelegate.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* DefaultPersistenceDelegate.java
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. import java.lang.reflect.InvocationTargetException;
  33. import java.lang.reflect.Method;
  34. /** <p><code>DefaultPersistenceDelegate</code> is a {@link PersistenceDelegate}
  35. * implementation that can be used to serialize objects which adhere to the
  36. * Java Beans naming convention.</p>
  37. *
  38. * @author Robert Schuster (robertschuster@fsfe.org)
  39. * @since 1.4
  40. */
  41. public class DefaultPersistenceDelegate extends PersistenceDelegate
  42. {
  43. private String[] constructorPropertyNames;
  44. /** Using this constructor the object to be serialized will be instantiated
  45. * with the default non-argument constructor.
  46. */
  47. public DefaultPersistenceDelegate()
  48. {
  49. }
  50. /** This constructor allows to specify which Bean properties appear
  51. * in the constructor.
  52. *
  53. * <p>The implementation reads the mentioned properties from the Bean
  54. * instance and applies it in the given order to a corresponding
  55. * constructor.</p>
  56. *
  57. * @param constructorPropertyNames The properties the Bean's constructor
  58. * should be given to.
  59. */
  60. public DefaultPersistenceDelegate(String[] constructorPropertyNames)
  61. {
  62. this.constructorPropertyNames = constructorPropertyNames;
  63. }
  64. protected boolean mutatesTo(Object oldInstance, Object newInstance)
  65. {
  66. try
  67. {
  68. return (constructorPropertyNames != null
  69. && constructorPropertyNames.length > 0
  70. && oldInstance.getClass()
  71. .getDeclaredMethod("equals",
  72. new Class[] { Object.class }) != null)
  73. ? oldInstance.equals(newInstance)
  74. : super.mutatesTo(oldInstance, newInstance);
  75. }
  76. catch (NoSuchMethodException nsme)
  77. {
  78. return super.mutatesTo(oldInstance, newInstance);
  79. }
  80. }
  81. protected Expression instantiate(Object oldInstance, Encoder out)
  82. {
  83. Object[] args = null;
  84. try
  85. {
  86. // If there are property names in the array, then we create
  87. // a corresponding argument array and store every
  88. // argument in it. To retrieve an argument object we have
  89. // dig up the right property in the bean class' BeanInfo
  90. // object.
  91. // This is so costly in terms of execution time I better
  92. // not think twice about it ...
  93. if (constructorPropertyNames != null)
  94. {
  95. args = new Object[constructorPropertyNames.length];
  96. // Look up the properties of oldInstance's class to find matches for
  97. // the
  98. // names given in the constructor.
  99. PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
  100. oldInstance.getClass()).getPropertyDescriptors();
  101. for (int i = 0; i < constructorPropertyNames.length; i++)
  102. {
  103. // Scan the property descriptions for a matching name.
  104. for (int j = 0; j < propertyDescs.length; j++)
  105. {
  106. if (propertyDescs[i].getName().equals(
  107. constructorPropertyNames[i]))
  108. {
  109. Method readMethod = propertyDescs[i].getReadMethod();
  110. args[i] = readMethod.invoke(oldInstance);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. catch (IllegalAccessException iae)
  117. {
  118. out.getExceptionListener().exceptionThrown(iae);
  119. }
  120. catch (IllegalArgumentException iarge)
  121. {
  122. out.getExceptionListener().exceptionThrown(iarge);
  123. }
  124. catch (InvocationTargetException ite)
  125. {
  126. out.getExceptionListener().exceptionThrown(ite);
  127. }
  128. catch (IntrospectionException ie)
  129. {
  130. out.getExceptionListener().exceptionThrown(ie);
  131. }
  132. return new Expression(oldInstance, oldInstance.getClass(), "new", args);
  133. }
  134. protected void initialize(Class<?> type, Object oldInstance,
  135. Object newInstance, Encoder out)
  136. {
  137. // Calling the supertype's implementation of initialize makes it
  138. // possible that descendants of classes like AbstractHashMap
  139. // or Hashtable are serialized correctly. This mechanism grounds on
  140. // two other facts:
  141. // * Each class which has not registered a special purpose
  142. // PersistenceDelegate is handled by a DefaultPersistenceDelegate
  143. // instance.
  144. // * PersistenceDelegate.initialize() is implemented in a way that it
  145. // calls the initialize method of the superclass' persistence delegate.
  146. super.initialize(type, oldInstance, newInstance, out);
  147. // Suppresses the writing of property setting statements when this delegate
  148. // is not used for the exact instance type. By doing so the following code
  149. // is called only once per object.
  150. if (type != oldInstance.getClass())
  151. return;
  152. try
  153. {
  154. PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
  155. oldInstance.getClass()).getPropertyDescriptors();
  156. for (int i = 0; i < propertyDescs.length; i++)
  157. {
  158. Method readMethod = propertyDescs[i].getReadMethod();
  159. Method writeMethod = propertyDescs[i].getWriteMethod();
  160. if (readMethod != null && writeMethod != null)
  161. {
  162. Object oldValue = readMethod.invoke(oldInstance);
  163. if (oldValue != null)
  164. out.writeStatement(new Statement(oldInstance,
  165. writeMethod.getName(),
  166. new Object[] { oldValue }));
  167. }
  168. }
  169. }
  170. catch (IntrospectionException ie)
  171. {
  172. out.getExceptionListener().exceptionThrown(ie);
  173. }
  174. catch (IllegalAccessException iae)
  175. {
  176. out.getExceptionListener().exceptionThrown(iae);
  177. }
  178. catch (InvocationTargetException ite)
  179. {
  180. out.getExceptionListener().exceptionThrown(ite);
  181. }
  182. }
  183. }