1 /*
2 * Created on 09.03.2004
3 */
4 package org.xpcards.model;
5
6 /***
7 * @author galu02
8 */
9
10 import java.beans.BeanInfo;
11 import java.beans.Encoder;
12 import java.beans.Expression;
13 import java.beans.IntrospectionException;
14 import java.beans.Introspector;
15 import java.beans.PersistenceDelegate;
16 import java.beans.PropertyDescriptor;
17 import java.beans.Statement;
18 import java.lang.reflect.Method;
19
20 /***
21 * @author Mikhail Galyutin
22 */
23 public abstract class BasePersistenceDelegate extends PersistenceDelegate {
24
25 protected Expression instantiate(Object oldInstance, Encoder out) {
26 return new Expression(
27 oldInstance,
28 oldInstance.getClass(),
29 "new",
30 new Object[0]);
31 }
32
33 protected boolean mutatesTo(Object oldInstance, Object newInstance) {
34 return (
35 newInstance != null
36 && oldInstance.getClass() == newInstance.getClass());
37 }
38
39 protected void doProperty(
40 Class type,
41 PropertyDescriptor pd,
42 Object oldInstance,
43 Object newInstance,
44 Encoder out)
45 throws Exception {
46 Method getter = pd.getReadMethod();
47 Method setter = pd.getWriteMethod();
48
49 if (getter != null && setter != null) {
50 Expression oldGetExp =
51 new Expression(oldInstance, getter.getName(), new Object[] {
52 });
53 Expression newGetExp =
54 new Expression(newInstance, getter.getName(), new Object[] {
55 });
56 Object oldValue = oldGetExp.getValue();
57 Object newValue = newGetExp.getValue();
58 out.writeExpression(oldGetExp);
59
60 if (!equals(newValue, out.get(oldValue))) {
61 // Search for a static constant with this value;
62 // Object e = (Object[]) pd.getValue("enumerationValues");
63 // if (e instanceof Object[] && java.lang.reflect.Array.getLength(e) % 3 == 0) {
64 // System.err.println("Strange e: " + e);
65 // Object[] a = (Object[]) e;
66 // for (int i = 0; i < a.length; i = i + 3) {
67 // try {
68 // java.lang.reflect.Field f = type.getField((String) a[i]);
69 // if (f.get(null).equals(oldValue)) {
70 // out.remove(oldValue);
71 // out.writeExpression(new Expression(oldValue, f, "get", new
72 // Object[] { null }));
73 // }
74 // } catch (Exception ex) {
75 // }
76 // }
77 // }
78 invokeStatement(
79 oldInstance,
80 setter.getName(),
81 new Object[] { oldValue },
82 out);
83 }
84 }
85 }
86
87 /* (non-Javadoc)
88 * @see java.beans.PersistenceDelegate#writeObject(java.lang.Object, java.beans.Encoder)
89 */
90 public void writeObject(Object arg0, Encoder arg1) {
91 super.writeObject(arg0, arg1);
92 try {
93 BeanInfo info = Introspector.getBeanInfo(arg0.getClass());
94 PropertyDescriptor[] p = info.getPropertyDescriptors();
95 for ( int i = 0; p != null && i < p.length; i++ ) {
96 // doProperty()
97 }
98 } catch (IntrospectionException e) {
99
100 }
101 }
102
103 protected static boolean equals(Object o1, Object o2) {
104 return (o1 == null) ? (o2 == null) : o1.equals(o2);
105 }
106
107 protected static void invokeStatement(
108 Object instance,
109 String methodName,
110 Object[] args,
111 Encoder out) {
112 Statement st = new Statement(instance, methodName, args);
113 // System.err.println("Statement : " + st);
114 out.writeStatement(st);
115 }
116
117 }
This page was automatically generated by Maven