001/* DefaultPersistenceDelegate.java
002 Copyright (C) 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING.  If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library.  Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module.  An independent module is a module which is not derived from
033 or based on this library.  If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so.  If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039package java.beans;
040
041import java.lang.reflect.InvocationTargetException;
042import java.lang.reflect.Method;
043
044/** <p><code>DefaultPersistenceDelegate</code> is a {@link PersistenceDelegate}
045 * implementation that can be used to serialize objects which adhere to the
046 * Java Beans naming convention.</p>
047 *
048 * @author Robert Schuster (robertschuster@fsfe.org)
049 * @since 1.4
050 */
051public class DefaultPersistenceDelegate extends PersistenceDelegate
052{
053
054  private String[] constructorPropertyNames;
055
056  /** Using this constructor the object to be serialized will be instantiated
057   * with the default non-argument constructor.
058   */
059  public DefaultPersistenceDelegate()
060  {
061  }
062
063  /** This constructor allows to specify which Bean properties appear
064   * in the constructor.
065   *
066   * <p>The implementation reads the mentioned properties from the Bean
067   * instance and applies it in the given order to a corresponding
068   * constructor.</p>
069   *
070   * @param constructorPropertyNames The properties the Bean's constructor
071   * should be given to.
072   */
073  public DefaultPersistenceDelegate(String[] constructorPropertyNames)
074  {
075    this.constructorPropertyNames = constructorPropertyNames;
076  }
077
078  protected boolean mutatesTo(Object oldInstance, Object newInstance)
079  {
080    try
081      {
082
083        return (constructorPropertyNames != null
084               && constructorPropertyNames.length > 0
085               && oldInstance.getClass()
086               .getDeclaredMethod("equals",
087                                  new Class[] { Object.class }) != null)
088                                  ? oldInstance.equals(newInstance)
089                                  : super.mutatesTo(oldInstance, newInstance);
090      }
091    catch (NoSuchMethodException nsme)
092      {
093        return super.mutatesTo(oldInstance, newInstance);
094      }
095  }
096
097  protected Expression instantiate(Object oldInstance, Encoder out)
098  {
099    Object[] args = null;
100
101    try
102      {
103        // If there are property names in the array, then we create
104        // a corresponding argument array and store every
105        // argument in it. To retrieve an argument object we have
106        // dig up the right property in the bean class' BeanInfo
107        // object.
108        // This is so costly in terms of execution time I better
109        // not think twice about it ...
110        if (constructorPropertyNames != null)
111          {
112            args = new Object[constructorPropertyNames.length];
113
114            // Look up the properties of oldInstance's class to find matches for
115            // the
116            // names given in the constructor.
117            PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
118                                                                          oldInstance.getClass()).getPropertyDescriptors();
119
120            for (int i = 0; i < constructorPropertyNames.length; i++)
121              {
122                // Scan the property descriptions for a matching name.
123                for (int j = 0; j < propertyDescs.length; j++)
124                  {
125                    if (propertyDescs[i].getName().equals(
126                                                          constructorPropertyNames[i]))
127                      {
128                        Method readMethod = propertyDescs[i].getReadMethod();
129
130                        args[i] = readMethod.invoke(oldInstance);
131                      }
132                  }
133              }
134          }
135
136      }
137    catch (IllegalAccessException iae)
138      {
139        out.getExceptionListener().exceptionThrown(iae);
140      }
141    catch (IllegalArgumentException iarge)
142      {
143        out.getExceptionListener().exceptionThrown(iarge);
144      }
145    catch (InvocationTargetException ite)
146      {
147        out.getExceptionListener().exceptionThrown(ite);
148      }
149    catch (IntrospectionException ie)
150      {
151        out.getExceptionListener().exceptionThrown(ie);
152      }
153
154    return new Expression(oldInstance, oldInstance.getClass(), "new", args);
155  }
156
157  protected void initialize(Class<?> type, Object oldInstance,
158                            Object newInstance, Encoder out)
159  {
160    // Calling the supertype's implementation of initialize makes it
161    // possible that descendants of classes like AbstractHashMap
162    // or Hashtable are serialized correctly. This mechanism grounds on
163    // two other facts:
164    // * Each class which has not registered a special purpose
165    //   PersistenceDelegate is handled by a DefaultPersistenceDelegate
166    //   instance.
167    // * PersistenceDelegate.initialize() is implemented in a way that it
168    //   calls the initialize method of the superclass' persistence delegate.
169    super.initialize(type, oldInstance, newInstance, out);
170
171    // Suppresses the writing of property setting statements when this delegate
172    // is not used for the exact instance type. By doing so the following code
173    // is called only once per object.
174    if (type != oldInstance.getClass())
175      return;
176
177    try
178      {
179        PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
180                                                                      oldInstance.getClass()).getPropertyDescriptors();
181
182        for (int i = 0; i < propertyDescs.length; i++)
183          {
184            Method readMethod = propertyDescs[i].getReadMethod();
185            Method writeMethod = propertyDescs[i].getWriteMethod();
186
187            if (readMethod != null && writeMethod != null)
188              {
189                Object oldValue = readMethod.invoke(oldInstance);
190
191                if (oldValue != null)
192                  out.writeStatement(new Statement(oldInstance,
193                                                   writeMethod.getName(),
194                                                   new Object[] { oldValue }));
195              }
196          }
197      }
198    catch (IntrospectionException ie)
199      {
200        out.getExceptionListener().exceptionThrown(ie);
201      }
202    catch (IllegalAccessException iae)
203      {
204        out.getExceptionListener().exceptionThrown(iae);
205      }
206    catch (InvocationTargetException ite)
207      {
208        out.getExceptionListener().exceptionThrown(ite);
209      }
210  }
211}