1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44:
51: public class DefaultPersistenceDelegate extends PersistenceDelegate
52: {
53:
54: private String[] constructorPropertyNames;
55:
56:
59: public DefaultPersistenceDelegate()
60: {
61: }
62:
63:
73: public DefaultPersistenceDelegate(String[] constructorPropertyNames)
74: {
75: this.constructorPropertyNames = constructorPropertyNames;
76: }
77:
78: protected boolean mutatesTo(Object oldInstance, Object newInstance)
79: {
80: try
81: {
82:
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);
90: }
91: catch (NoSuchMethodException nsme)
92: {
93: return super.mutatesTo(oldInstance, newInstance);
94: }
95: }
96:
97: protected Expression instantiate(Object oldInstance, Encoder out)
98: {
99: Object[] args = null;
100:
101: try
102: {
103:
104:
105:
106:
107:
108:
109:
110: if (constructorPropertyNames != null)
111: {
112: args = new Object[constructorPropertyNames.length];
113:
114:
115:
116:
117: PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
118: oldInstance.getClass()).getPropertyDescriptors();
119:
120: for (int i = 0; i < constructorPropertyNames.length; i++)
121: {
122:
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, null);
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, Object newInstance,
158: Encoder out)
159: {
160: try
161: {
162: PropertyDescriptor[] propertyDescs = Introspector.getBeanInfo(
163: oldInstance.getClass()).getPropertyDescriptors();
164:
165: for (int i = 0; i < propertyDescs.length; i++)
166: {
167: Method readMethod = propertyDescs[i].getReadMethod();
168: Method writeMethod = propertyDescs[i].getWriteMethod();
169:
170: if (readMethod != null && writeMethod != null)
171: {
172: Object oldValue = readMethod.invoke(oldInstance, null);
173:
174: if (oldValue != null)
175: out.writeStatement(new Statement(oldInstance,
176: writeMethod.getName(),
177: new Object[] { oldValue }));
178: }
179: }
180: }
181: catch (IntrospectionException ie)
182: {
183: out.getExceptionListener().exceptionThrown(ie);
184: }
185: catch (IllegalAccessException iae)
186: {
187: out.getExceptionListener().exceptionThrown(iae);
188: }
189: catch (InvocationTargetException ite)
190: {
191: out.getExceptionListener().exceptionThrown(ite);
192: }
193: }
194: }