1. /*
  2. * @(#)DefaultPersistenceDelegate.java 1.17 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. import java.util.*;
  9. import java.lang.reflect.*;
  10. import java.beans.*;
  11. import java.io.*;
  12. /**
  13. * The <code>DefaultPersistenceDelegate</code> is a concrete implementation of
  14. * the abstract <code>PersistenceDelegate</code> class and
  15. * is the delegate used by default for classes about
  16. * which no information is available. The <code>DefaultPersistenceDelegate</code>
  17. * provides, version resilient, public API-based persistence for
  18. * classes that follow the JavaBeans conventions without any class specific
  19. * configuration.
  20. * <p>
  21. * The key assumptions are that the class has a nullary constructor
  22. * and that its state is accurately represented by matching pairs
  23. * of "setter" and "getter" methods in the order they are returned
  24. * by the Introspector.
  25. * In addition to providing code-free persistence for JavaBeans,
  26. * the <code>DefaultPersistenceDelegate</code> provides a convenient means
  27. * to effect persistent storage for classes that have a constructor
  28. * that, while not nullary, simply requires some property values
  29. * as arguments.
  30. *
  31. * @see #DefaultPersistenceDelegate(String[])
  32. * @see java.beans.Introspector
  33. *
  34. * @since 1.4
  35. *
  36. * @version 1.17 05/05/04
  37. * @author Philip Milne
  38. */
  39. public class DefaultPersistenceDelegate extends PersistenceDelegate {
  40. private String[] constructor;
  41. private Boolean definesEquals;
  42. /**
  43. * Creates a persistence delegate for a class with a nullary constructor.
  44. *
  45. * @see #DefaultPersistenceDelegate(java.lang.String[])
  46. */
  47. public DefaultPersistenceDelegate() {
  48. this(new String[0]);
  49. }
  50. /**
  51. * Creates a default persistence delegate for a class with a
  52. * constructor whose arguments are the values of the property
  53. * names as specified by <code>constructorPropertyNames</code>.
  54. * The constructor arguments are created by
  55. * evaluating the property names in the order they are supplied.
  56. * To use this class to specify a single preferred constructor for use
  57. * in the serialization of a particular type, we state the
  58. * names of the properties that make up the constructor's
  59. * arguments. For example, the <code>Font</code> class which
  60. * does not define a nullary constructor can be handled
  61. * with the following persistence delegate:
  62. *
  63. * <pre>
  64. * new DefaultPersistenceDelegate(new String[]{"name", "style", "size"});
  65. * </pre>
  66. *
  67. * @param constructorPropertyNames The property names for the arguments of this constructor.
  68. *
  69. * @see #instantiate
  70. */
  71. public DefaultPersistenceDelegate(String[] constructorPropertyNames) {
  72. this.constructor = constructorPropertyNames;
  73. }
  74. private static boolean definesEquals(Class type) {
  75. try {
  76. type.getDeclaredMethod("equals", new Class[]{Object.class});
  77. return true;
  78. }
  79. catch(NoSuchMethodException e) {
  80. return false;
  81. }
  82. }
  83. private boolean definesEquals(Object instance) {
  84. if (definesEquals != null) {
  85. return (definesEquals == Boolean.TRUE);
  86. }
  87. else {
  88. boolean result = definesEquals(instance.getClass());
  89. definesEquals = result ? Boolean.TRUE : Boolean.FALSE;
  90. return result;
  91. }
  92. }
  93. /**
  94. * If the number of arguments in the specified constructor is non-zero and
  95. * the class of <code>oldInstance</code> explicitly declares an "equals" method
  96. * this method returns the value of <code>oldInstance.equals(newInstance)</code>.
  97. * Otherwise, this method uses the superclass's definition which returns true if the
  98. * classes of the two instances are equal.
  99. *
  100. * @param oldInstance The instance to be copied.
  101. * @param newInstance The instance that is to be modified.
  102. * @return True if an equivalent copy of <code>newInstance</code> may be
  103. * created by applying a series of mutations to <code>oldInstance</code>.
  104. *
  105. * @see #DefaultPersistenceDelegate(String[])
  106. */
  107. protected boolean mutatesTo(Object oldInstance, Object newInstance) {
  108. // Assume the instance is either mutable or a singleton
  109. // if it has a nullary constructor.
  110. return (constructor.length == 0) || !definesEquals(oldInstance) ?
  111. super.mutatesTo(oldInstance, newInstance) :
  112. oldInstance.equals(newInstance);
  113. }
  114. /**
  115. * This default implementation of the <code>instantiate</code> method returns
  116. * an expression containing the predefined method name "new" which denotes a
  117. * call to a constructor with the arguments as specified in
  118. * the <code>DefaultPersistenceDelegate</code>'s constructor.
  119. *
  120. * @param oldInstance The instance to be instantiated.
  121. * @param out The code output stream.
  122. * @return An expression whose value is <code>oldInstance</code>.
  123. *
  124. * @see #DefaultPersistenceDelegate(String[])
  125. */
  126. protected Expression instantiate(Object oldInstance, Encoder out) {
  127. int nArgs = constructor.length;
  128. Class type = oldInstance.getClass();
  129. // System.out.println("writeObject: " + oldInstance);
  130. Object[] constructorArgs = new Object[nArgs];
  131. for(int i = 0; i < nArgs; i++) {
  132. /*
  133. 1.2 introduces "public double getX()" et al. which return values
  134. which cannot be used in the constructors (they are the wrong type).
  135. In constructors, use public fields in preference to getters
  136. when they are defined.
  137. */
  138. String name = constructor[i];
  139. Field f = null;
  140. try {
  141. // System.out.println("Trying field " + name + " in " + type);
  142. f = type.getDeclaredField(name);
  143. f.setAccessible(true);
  144. }
  145. catch (NoSuchFieldException e) {}
  146. try {
  147. constructorArgs[i] = (f != null && !Modifier.isStatic(f.getModifiers())) ?
  148. f.get(oldInstance) :
  149. type.getMethod("get" + NameGenerator.capitalize(name),
  150. new Class[0]).invoke(oldInstance, new Object[0]);
  151. }
  152. catch (Exception e) {
  153. out.getExceptionListener().exceptionThrown(e);
  154. }
  155. }
  156. return new Expression(oldInstance, oldInstance.getClass(), "new", constructorArgs);
  157. }
  158. // This is a workaround for a bug in the introspector.
  159. // PropertyDescriptors are not shared amongst subclasses.
  160. private boolean isTransient(Class type, PropertyDescriptor pd) {
  161. if (type == null) {
  162. return false;
  163. }
  164. // This code was mistakenly deleted - it may be fine and
  165. // is more efficient than the code below. This should
  166. // all disappear anyway when property descriptors are shared
  167. // by the introspector.
  168. /*
  169. Method getter = pd.getReadMethod();
  170. Class declaringClass = getter.getDeclaringClass();
  171. if (declaringClass == type) {
  172. return Boolean.TRUE.equals(pd.getValue("transient"));
  173. }
  174. */
  175. String pName = pd.getName();
  176. BeanInfo info = MetaData.getBeanInfo(type);
  177. PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
  178. for (int i = 0; i < propertyDescriptors.length; ++i ) {
  179. PropertyDescriptor pd2 = propertyDescriptors[i];
  180. if (pName.equals(pd2.getName())) {
  181. Object value = pd2.getValue("transient");
  182. if (value != null) {
  183. return Boolean.TRUE.equals(value);
  184. }
  185. }
  186. }
  187. return isTransient(type.getSuperclass(), pd);
  188. }
  189. private static boolean equals(Object o1, Object o2) {
  190. return (o1 == null) ? (o2 == null) : o1.equals(o2);
  191. }
  192. private void doProperty(Class type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception {
  193. Method getter = pd.getReadMethod();
  194. Method setter = pd.getWriteMethod();
  195. if (getter != null && setter != null && !isTransient(type, pd)) {
  196. Expression oldGetExp = new Expression(oldInstance, getter.getName(), new Object[]{});
  197. Expression newGetExp = new Expression(newInstance, getter.getName(), new Object[]{});
  198. Object oldValue = oldGetExp.getValue();
  199. Object newValue = newGetExp.getValue();
  200. out.writeExpression(oldGetExp);
  201. if (!equals(newValue, out.get(oldValue))) {
  202. // Search for a static constant with this value;
  203. Object e = (Object[])pd.getValue("enumerationValues");
  204. if (e instanceof Object[] && Array.getLength(e) % 3 == 0) {
  205. Object[] a = (Object[])e;
  206. for(int i = 0; i < a.length; i = i + 3) {
  207. try {
  208. Field f = type.getField((String)a[i]);
  209. if (f.get(null).equals(oldValue)) {
  210. out.remove(oldValue);
  211. out.writeExpression(new Expression(oldValue, f, "get", new Object[]{null}));
  212. }
  213. }
  214. catch (Exception ex) {}
  215. }
  216. }
  217. invokeStatement(oldInstance, setter.getName(), new Object[]{oldValue}, out);
  218. }
  219. }
  220. }
  221. static void invokeStatement(Object instance, String methodName, Object[] args, Encoder out) {
  222. out.writeStatement(new Statement(instance, methodName, args));
  223. }
  224. // Write out the properties of this instance.
  225. private void initBean(Class type, Object oldInstance, Object newInstance, Encoder out) {
  226. // System.out.println("initBean: " + oldInstance);
  227. BeanInfo info = MetaData.getBeanInfo(type);
  228. // Properties
  229. PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
  230. for (int i = 0; i < propertyDescriptors.length; ++i ) {
  231. try {
  232. doProperty(type, propertyDescriptors[i], oldInstance, newInstance, out);
  233. }
  234. catch (Exception e) {
  235. out.getExceptionListener().exceptionThrown(e);
  236. }
  237. }
  238. // Listeners
  239. /*
  240. Pending(milne). There is a general problem with the archival of
  241. listeners which is unresolved as of 1.4. Many of the methods
  242. which install one object inside another (typically "add" methods
  243. or setters) automatically install a listener on the "child" object
  244. so that its "parent" may respond to changes that are made to it.
  245. For example the JTable:setModel() method automatically adds a
  246. TableModelListener (the JTable itself in this case) to the supplied
  247. table model.
  248. We do not need to explictly add these listeners to the model in an
  249. archive as they will be added automatically by, in the above case,
  250. the JTable's "setModel" method. In some cases, we must specifically
  251. avoid trying to do this since the listener may be an inner class
  252. that cannot be instantiated using public API.
  253. No general mechanism currently
  254. exists for differentiating between these kind of listeners and
  255. those which were added explicitly by the user. A mechanism must
  256. be created to provide a general means to differentiate these
  257. special cases so as to provide reliable persistence of listeners
  258. for the general case.
  259. */
  260. if (!java.awt.Component.class.isAssignableFrom(type)) {
  261. return; // Just handle the listeners of Components for now.
  262. }
  263. EventSetDescriptor[] eventSetDescriptors = info.getEventSetDescriptors();
  264. for (int e = 0; e < eventSetDescriptors.length; e++) {
  265. EventSetDescriptor d = eventSetDescriptors[e];
  266. Class listenerType = d.getListenerType();
  267. // The ComponentListener is added automatically, when
  268. // Contatiner:add is called on the parent.
  269. if (listenerType == java.awt.event.ComponentListener.class) {
  270. continue;
  271. }
  272. // JMenuItems have a change listener added to them in
  273. // their "add" methods to enable accessibility support -
  274. // see the add method in JMenuItem for details. We cannot
  275. // instantiate this instance as it is a private inner class
  276. // and do not need to do this anyway since it will be created
  277. // and installed by the "add" method. Special case this for now,
  278. // ignoring all change listeners on JMenuItems.
  279. if (listenerType == javax.swing.event.ChangeListener.class &&
  280. type == javax.swing.JMenuItem.class) {
  281. continue;
  282. }
  283. EventListener[] oldL = new EventListener[0];
  284. EventListener[] newL = new EventListener[0];
  285. try {
  286. Method m = d.getGetListenerMethod();
  287. oldL = (EventListener[])m.invoke(oldInstance, new Object[]{});
  288. newL = (EventListener[])m.invoke(newInstance, new Object[]{});
  289. }
  290. catch (Throwable e2) {
  291. try {
  292. Method m = type.getMethod("getListeners", new Class[]{Class.class});
  293. oldL = (EventListener[])m.invoke(oldInstance, new Object[]{listenerType});
  294. newL = (EventListener[])m.invoke(newInstance, new Object[]{listenerType});
  295. }
  296. catch (Exception e3) {
  297. return;
  298. }
  299. }
  300. // Asssume the listeners are in the same order and that there are no gaps.
  301. // Eventually, this may need to do true differencing.
  302. String addListenerMethodName = d.getAddListenerMethod().getName();
  303. for (int i = newL.length; i < oldL.length; i++) {
  304. // System.out.println("Adding listener: " + addListenerMethodName + oldL[i]);
  305. invokeStatement(oldInstance, addListenerMethodName, new Object[]{oldL[i]}, out);
  306. }
  307. String removeListenerMethodName = d.getRemoveListenerMethod().getName();
  308. for (int i = oldL.length; i < newL.length; i++) {
  309. invokeStatement(oldInstance, removeListenerMethodName, new Object[]{oldL[i]}, out);
  310. }
  311. }
  312. }
  313. /**
  314. * This default implementation of the <code>initialize</code> method assumes
  315. * all state held in objects of this type is exposed via the
  316. * matching pairs of "setter" and "getter" methods in the order
  317. * they are returned by the Introspector. If a property descriptor
  318. * defines a "transient" attribute with a value equal to
  319. * <code>Boolean.TRUE</code> the property is ignored by this
  320. * default implementation. Note that this use of the word
  321. * "transient" is quite independent of the field modifier
  322. * that is used by the <code>ObjectOutputStream</code>.
  323. * <p>
  324. * For each non-transient property, an expression is created
  325. * in which the nullary "getter" method is applied
  326. * to the <code>oldInstance</code>. The value of this
  327. * expression is the value of the property in the instance that is
  328. * being serialized. If the value of this expression
  329. * in the cloned environment <code>mutatesTo</code> the
  330. * target value, the new value is initialized to make it
  331. * equivalent to the old value. In this case, because
  332. * the property value has not changed there is no need to
  333. * call the corresponding "setter" method and no statement
  334. * is emitted. If not however, the expression for this value
  335. * is replaced with another expression (normally a constructor)
  336. * and the corresponding "setter" method is called to install
  337. * the new property value in the object. This scheme removes
  338. * default information from the output produced by streams
  339. * using this delegate.
  340. * <p>
  341. * In passing these statements to the output stream, where they
  342. * will be executed, side effects are made to the <code>newInstance</code>.
  343. * In most cases this allows the problem of properties
  344. * whose values depend on each other to actually help the
  345. * serialization process by making the number of statements
  346. * that need to be written to the output smaller. In general,
  347. * the problem of handling interdependent properties is reduced to
  348. * that of finding an order for the properties in
  349. * a class such that no property value depends on the value of
  350. * a subsequent property.
  351. *
  352. * @param oldInstance The instance to be copied.
  353. * @param newInstance The instance that is to be modified.
  354. * @param out The stream to which any initialization statements should be written.
  355. *
  356. * @see java.beans.Introspector#getBeanInfo
  357. * @see java.beans.PropertyDescriptor
  358. */
  359. protected void initialize(Class<?> type,
  360. Object oldInstance, Object newInstance,
  361. Encoder out)
  362. {
  363. // System.out.println("DefulatPD:initialize" + type);
  364. super.initialize(type, oldInstance, newInstance, out);
  365. if (oldInstance.getClass() == type) { // !type.isInterface()) {
  366. initBean(type, oldInstance, newInstance, out);
  367. }
  368. }
  369. }