1 /* DefaultPersistenceDelegate.java
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.lang
.reflect
.InvocationTargetException
;
42 import java
.lang
.reflect
.Method
;
44 /** <p><code>DefaultPersistenceDelegate</code> is a {@link PersistenceDelegate}
45 * implementation that can be used to serialize objects which adhere to the
46 * Java Beans naming convention.</p>
48 * @author Robert Schuster (robertschuster@fsfe.org)
51 public class DefaultPersistenceDelegate
extends PersistenceDelegate
54 private String
[] constructorPropertyNames
;
56 /** Using this constructor the object to be serialized will be instantiated
57 * with the default non-argument constructor.
59 public DefaultPersistenceDelegate()
63 /** This constructor allows to specify which Bean properties appear
66 * <p>The implementation reads the mentioned properties from the Bean
67 * instance and applies it in the given order to a corresponding
70 * @param constructorPropertyNames The properties the Bean's constructor
73 public DefaultPersistenceDelegate(String
[] constructorPropertyNames
)
75 this.constructorPropertyNames
= constructorPropertyNames
;
78 protected boolean mutatesTo(Object oldInstance
, Object newInstance
)
83 return (constructorPropertyNames
!= null
84 && constructorPropertyNames
.length
> 0
85 && oldInstance
.getClass()
86 .getDeclaredMethod("equals",
87 new Class
[] { Object
.class }) != null)
88 ? oldInstance
.equals(newInstance
)
89 : super.mutatesTo(oldInstance
, newInstance
);
91 catch (NoSuchMethodException nsme
)
93 return super.mutatesTo(oldInstance
, newInstance
);
97 protected Expression
instantiate(Object oldInstance
, Encoder out
)
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
108 // This is so costly in terms of execution time I better
109 // not think twice about it ...
110 if (constructorPropertyNames
!= null)
112 args
= new Object
[constructorPropertyNames
.length
];
114 // Look up the properties of oldInstance's class to find matches for
116 // names given in the constructor.
117 PropertyDescriptor
[] propertyDescs
= Introspector
.getBeanInfo(
118 oldInstance
.getClass()).getPropertyDescriptors();
120 for (int i
= 0; i
< constructorPropertyNames
.length
; i
++)
122 // Scan the property descriptions for a matching name.
123 for (int j
= 0; j
< propertyDescs
.length
; j
++)
125 if (propertyDescs
[i
].getName().equals(
126 constructorPropertyNames
[i
]))
128 Method readMethod
= propertyDescs
[i
].getReadMethod();
130 args
[i
] = readMethod
.invoke(oldInstance
, null);
137 catch (IllegalAccessException iae
)
139 out
.getExceptionListener().exceptionThrown(iae
);
141 catch (IllegalArgumentException iarge
)
143 out
.getExceptionListener().exceptionThrown(iarge
);
145 catch (InvocationTargetException ite
)
147 out
.getExceptionListener().exceptionThrown(ite
);
149 catch (IntrospectionException ie
)
151 out
.getExceptionListener().exceptionThrown(ie
);
154 return new Expression(oldInstance
, oldInstance
.getClass(), "new", args
);
157 protected void initialize(Class type
, Object oldInstance
, Object newInstance
,
162 PropertyDescriptor
[] propertyDescs
= Introspector
.getBeanInfo(
163 oldInstance
.getClass()).getPropertyDescriptors();
165 for (int i
= 0; i
< propertyDescs
.length
; i
++)
167 Method readMethod
= propertyDescs
[i
].getReadMethod();
168 Method writeMethod
= propertyDescs
[i
].getWriteMethod();
170 if (readMethod
!= null && writeMethod
!= null)
172 Object oldValue
= readMethod
.invoke(oldInstance
, null);
174 if (oldValue
!= null)
175 out
.writeStatement(new Statement(oldInstance
,
176 writeMethod
.getName(),
177 new Object
[] { oldValue
}));
181 catch (IntrospectionException ie
)
183 out
.getExceptionListener().exceptionThrown(ie
);
185 catch (IllegalAccessException iae
)
187 out
.getExceptionListener().exceptionThrown(iae
);
189 catch (InvocationTargetException ite
)
191 out
.getExceptionListener().exceptionThrown(ite
);