KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > MetaData


1 /*
2  * @(#)MetaData.java 1.39 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
9 import java.lang.reflect.Array JavaDoc;
10 import java.lang.reflect.Field JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12
13 import java.util.Vector JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Enumeration JavaDoc;
17
18 /*
19  * Like the <code>Intropector</code>, the <code>MetaData</code> class
20  * contains <em>meta</em> objects that describe the way
21  * classes should express their state in terms of their
22  * own public APIs.
23  *
24  * @see java.beans.Intropector
25  *
26  * @version 1.39 05/05/04
27  * @author Philip Milne
28  * @author Steve Langley
29  */

30
31 class NullPersistenceDelegate extends PersistenceDelegate JavaDoc {
32     // Note this will be called by all classes when they reach the
33
// top of their superclass chain.
34
protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
35     }
36     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) { return null; }
37
38     public void writeObject(Object JavaDoc oldInstance, Encoder JavaDoc out) {
39     // System.out.println("NullPersistenceDelegate:writeObject " + oldInstance);
40
}
41 }
42
43 class PrimitivePersistenceDelegate extends PersistenceDelegate JavaDoc {
44     protected boolean mutatesTo(Object JavaDoc oldInstance, Object JavaDoc newInstance) {
45         return oldInstance.equals(newInstance);
46     }
47
48     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
49         return new Expression JavaDoc(oldInstance, oldInstance.getClass(),
50                   "new", new Object JavaDoc[]{oldInstance.toString()});
51     }
52 }
53
54 class ArrayPersistenceDelegate extends PersistenceDelegate JavaDoc {
55     protected boolean mutatesTo(Object JavaDoc oldInstance, Object JavaDoc newInstance) {
56         return (newInstance != null &&
57                 oldInstance.getClass() == newInstance.getClass() && // Also ensures the subtype is correct.
58
Array.getLength(oldInstance) == Array.getLength(newInstance));
59         }
60
61     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
62         // System.out.println("instantiate: " + type + " " + oldInstance);
63
Class JavaDoc oldClass = oldInstance.getClass();
64         return new Expression JavaDoc(oldInstance, Array JavaDoc.class, "newInstance",
65                    new Object JavaDoc[]{oldClass.getComponentType(),
66                                 new Integer JavaDoc(Array.getLength(oldInstance))});
67         }
68
69     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
70         int n = Array.getLength(oldInstance);
71         for (int i = 0; i < n; i++) {
72             Object JavaDoc index = new Integer JavaDoc(i);
73             // Expression oldGetExp = new Expression(Array.class, "get", new Object[]{oldInstance, index});
74
// Expression newGetExp = new Expression(Array.class, "get", new Object[]{newInstance, index});
75
Expression JavaDoc oldGetExp = new Expression JavaDoc(oldInstance, "get", new Object JavaDoc[]{index});
76             Expression JavaDoc newGetExp = new Expression JavaDoc(newInstance, "get", new Object JavaDoc[]{index});
77             try {
78                 Object JavaDoc oldValue = oldGetExp.getValue();
79                 Object JavaDoc newValue = newGetExp.getValue();
80                 out.writeExpression(oldGetExp);
81                 if (!MetaData.equals(newValue, out.get(oldValue))) {
82                     // System.out.println("Not equal: " + newGetExp + " != " + actualGetExp);
83
// invokeStatement(Array.class, "set", new Object[]{oldInstance, index, oldValue}, out);
84
DefaultPersistenceDelegate.invokeStatement(oldInstance, "set", new Object JavaDoc[]{index, oldValue}, out);
85                 }
86             }
87             catch (Exception JavaDoc e) {
88                 // System.err.println("Warning:: failed to write: " + oldGetExp);
89
out.getExceptionListener().exceptionThrown(e);
90             }
91         }
92     }
93 }
94
95 class ProxyPersistenceDelegate extends PersistenceDelegate JavaDoc {
96     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
97         Class JavaDoc type = oldInstance.getClass();
98         java.lang.reflect.Proxy JavaDoc p = (java.lang.reflect.Proxy JavaDoc)oldInstance;
99         // This unappealing hack is not required but makes the
100
// representation of EventHandlers much more concise.
101
java.lang.reflect.InvocationHandler JavaDoc ih = java.lang.reflect.Proxy.getInvocationHandler(p);
102         if (ih instanceof EventHandler JavaDoc) {
103             EventHandler JavaDoc eh = (EventHandler JavaDoc)ih;
104             Vector JavaDoc args = new Vector JavaDoc();
105             args.add(type.getInterfaces()[0]);
106             args.add(eh.getTarget());
107             args.add(eh.getAction());
108             if (eh.getEventPropertyName() != null) {
109                 args.add(eh.getEventPropertyName());
110             }
111             if (eh.getListenerMethodName() != null) {
112                 args.setSize(4);
113                 args.add(eh.getListenerMethodName());
114             }
115             return new Expression JavaDoc(oldInstance,
116                                   EventHandler JavaDoc.class,
117                                   "create",
118                                   args.toArray());
119         }
120         return new Expression JavaDoc(oldInstance,
121                               java.lang.reflect.Proxy JavaDoc.class,
122                               "newProxyInstance",
123                               new Object JavaDoc[]{type.getClassLoader(),
124                                            type.getInterfaces(),
125                                            ih});
126     }
127 }
128
129 // Strings
130
class java_lang_String_PersistenceDelegate extends PersistenceDelegate JavaDoc {
131     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) { return null; }
132
133     public void writeObject(Object JavaDoc oldInstance, Encoder JavaDoc out) {
134         // System.out.println("NullPersistenceDelegate:writeObject " + oldInstance);
135
}
136 }
137
138 // Classes
139
class java_lang_Class_PersistenceDelegate extends PersistenceDelegate JavaDoc {
140     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
141         Class JavaDoc c = (Class JavaDoc)oldInstance;
142         // As of 1.3 it is not possible to call Class.forName("int"),
143
// so we have to generate different code for primitive types.
144
// This is needed for arrays whose subtype may be primitive.
145
if (c.isPrimitive()) {
146             Field JavaDoc field = null;
147         try {
148         field = ReflectionUtils.typeToClass(c).getDeclaredField("TYPE");
149         } catch (NoSuchFieldException JavaDoc ex) {
150                 System.err.println("Unknown primitive type: " + c);
151             }
152             return new Expression JavaDoc(oldInstance, field, "get", new Object JavaDoc[]{null});
153         }
154         else if (oldInstance == String JavaDoc.class) {
155             return new Expression JavaDoc(oldInstance, "", "getClass", new Object JavaDoc[]{});
156         }
157         else if (oldInstance == Class JavaDoc.class) {
158             return new Expression JavaDoc(oldInstance, String JavaDoc.class, "getClass", new Object JavaDoc[]{});
159         }
160         else {
161             return new Expression JavaDoc(oldInstance, Class JavaDoc.class, "forName", new Object JavaDoc[]{c.getName()});
162         }
163     }
164 }
165
166 // Fields
167
class java_lang_reflect_Field_PersistenceDelegate extends PersistenceDelegate JavaDoc {
168     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
169         Field JavaDoc f = (Field JavaDoc)oldInstance;
170         return new Expression JavaDoc(oldInstance,
171                 f.getDeclaringClass(),
172                 "getField",
173                 new Object JavaDoc[]{f.getName()});
174     }
175 }
176
177 // Methods
178
class java_lang_reflect_Method_PersistenceDelegate extends PersistenceDelegate JavaDoc {
179     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
180         Method JavaDoc m = (Method JavaDoc)oldInstance;
181         return new Expression JavaDoc(oldInstance,
182                 m.getDeclaringClass(),
183                 "getMethod",
184                 new Object JavaDoc[]{m.getName(), m.getParameterTypes()});
185     }
186 }
187
188 // Collections
189

190 /*
191 The Hashtable and AbstractMap classes have no common ancestor yet may
192 be handled with a single persistence delegate: one which uses the methods
193 of the Map insterface exclusively. Attatching the persistence delegates
194 to the interfaces themselves is fraught however since, in the case of
195 the Map, both the AbstractMap and HashMap classes are declared to
196 implement the Map interface, leaving the obvious implementation prone
197 to repeating their initialization. These issues and questions around
198 the ordering of delegates attached to interfaces have lead us to
199 ignore any delegates attached to interfaces and force all persistence
200 delegates to be registered with concrete classes.
201 */

202
203 // Collection
204
class java_util_Collection_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
205     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
206     java.util.Collection JavaDoc oldO = (java.util.Collection JavaDoc)oldInstance;
207     java.util.Collection JavaDoc newO = (java.util.Collection JavaDoc)newInstance;
208
209         if (newO.size() != 0) {
210             invokeStatement(oldInstance, "clear", new Object JavaDoc[]{}, out);
211         }
212         for (Iterator JavaDoc i = oldO.iterator(); i.hasNext();) {
213             invokeStatement(oldInstance, "add", new Object JavaDoc[]{i.next()}, out);
214         }
215     }
216 }
217
218 // List
219
class java_util_List_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
220     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
221         java.util.List JavaDoc oldO = (java.util.List JavaDoc)oldInstance;
222         java.util.List JavaDoc newO = (java.util.List JavaDoc)newInstance;
223         int oldSize = oldO.size();
224         int newSize = (newO == null) ? 0 : newO.size();
225         if (oldSize < newSize) {
226             invokeStatement(oldInstance, "clear", new Object JavaDoc[]{}, out);
227             newSize = 0;
228         }
229         for (int i = 0; i < newSize; i++) {
230             Object JavaDoc index = new Integer JavaDoc(i);
231
232             Expression JavaDoc oldGetExp = new Expression JavaDoc(oldInstance, "get", new Object JavaDoc[]{index});
233             Expression JavaDoc newGetExp = new Expression JavaDoc(newInstance, "get", new Object JavaDoc[]{index});
234             try {
235                 Object JavaDoc oldValue = oldGetExp.getValue();
236                 Object JavaDoc newValue = newGetExp.getValue();
237                 out.writeExpression(oldGetExp);
238                 if (!MetaData.equals(newValue, out.get(oldValue))) {
239                     invokeStatement(oldInstance, "set", new Object JavaDoc[]{index, oldValue}, out);
240                 }
241             }
242             catch (Exception JavaDoc e) {
243                 out.getExceptionListener().exceptionThrown(e);
244             }
245         }
246         for (int i = newSize; i < oldSize; i++) {
247             invokeStatement(oldInstance, "add", new Object JavaDoc[]{oldO.get(i)}, out);
248         }
249     }
250 }
251
252
253 // Map
254
class java_util_Map_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
255     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
256         // System.out.println("Initializing: " + newInstance);
257
java.util.Map JavaDoc oldMap = (java.util.Map JavaDoc)oldInstance;
258         java.util.Map JavaDoc newMap = (java.util.Map JavaDoc)newInstance;
259         // Remove the new elements.
260
// Do this first otherwise we undo the adding work.
261
if (newMap != null) {
262             java.util.Iterator JavaDoc newKeys = newMap.keySet().iterator();
263             while(newKeys.hasNext()) {
264                 Object JavaDoc newKey = newKeys.next();
265                // PENDING: This "key" is not in the right environment.
266
if (!oldMap.containsKey(newKey)) {
267                     invokeStatement(oldInstance, "remove", new Object JavaDoc[]{newKey}, out);
268                 }
269             }
270         }
271         // Add the new elements.
272
java.util.Iterator JavaDoc oldKeys = oldMap.keySet().iterator();
273         while(oldKeys.hasNext()) {
274             Object JavaDoc oldKey = oldKeys.next();
275
276             Expression JavaDoc oldGetExp = new Expression JavaDoc(oldInstance, "get", new Object JavaDoc[]{oldKey});
277             // Pending: should use newKey.
278
Expression JavaDoc newGetExp = new Expression JavaDoc(newInstance, "get", new Object JavaDoc[]{oldKey});
279             try {
280                 Object JavaDoc oldValue = oldGetExp.getValue();
281                 Object JavaDoc newValue = newGetExp.getValue();
282                 out.writeExpression(oldGetExp);
283                 if (!MetaData.equals(newValue, out.get(oldValue))) {
284                     invokeStatement(oldInstance, "put", new Object JavaDoc[]{oldKey, oldValue}, out);
285                 }
286             }
287             catch (Exception JavaDoc e) {
288                 out.getExceptionListener().exceptionThrown(e);
289             }
290         }
291     }
292 }
293
294 class java_util_AbstractCollection_PersistenceDelegate extends java_util_Collection_PersistenceDelegate {}
295 class java_util_AbstractList_PersistenceDelegate extends java_util_List_PersistenceDelegate {}
296 class java_util_AbstractMap_PersistenceDelegate extends java_util_Map_PersistenceDelegate {}
297 class java_util_Hashtable_PersistenceDelegate extends java_util_Map_PersistenceDelegate {}
298
299
300 // Beans
301
class java_beans_beancontext_BeanContextSupport_PersistenceDelegate extends java_util_Collection_PersistenceDelegate {}
302
303 // AWT
304

305 class StaticFieldsPersistenceDelegate extends PersistenceDelegate JavaDoc {
306     protected void installFields(Encoder JavaDoc out, Class JavaDoc<?> cls) {
307         Field JavaDoc fields[] = cls.getFields();
308         for(int i = 0; i < fields.length; i++) {
309             Field JavaDoc field = fields[i];
310             // Don't install primitives, their identity will not be preserved
311
// by wrapping.
312
if (Object JavaDoc.class.isAssignableFrom(field.getType())) {
313                 out.writeExpression(new Expression JavaDoc(field, "get", new Object JavaDoc[]{null}));
314             }
315         }
316     }
317
318     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
319         throw new RuntimeException JavaDoc("Unrecognized instance: " + oldInstance);
320     }
321     
322     public void writeObject(Object JavaDoc oldInstance, Encoder JavaDoc out) {
323         if (out.getAttribute(this) == null) {
324             out.setAttribute(this, Boolean.TRUE);
325             installFields(out, oldInstance.getClass());
326     }
327         super.writeObject(oldInstance, out);
328     }
329 }
330
331 // SystemColor
332
class java_awt_SystemColor_PersistenceDelegate extends StaticFieldsPersistenceDelegate {}
333
334 // TextAttribute
335
class java_awt_font_TextAttribute_PersistenceDelegate extends StaticFieldsPersistenceDelegate {}
336
337 // MenuShortcut
338
class java_awt_MenuShortcut_PersistenceDelegate extends PersistenceDelegate JavaDoc {
339     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
340         java.awt.MenuShortcut JavaDoc m = (java.awt.MenuShortcut JavaDoc)oldInstance;
341         return new Expression JavaDoc(oldInstance, m.getClass(), "new",
342                    new Object JavaDoc[]{new Integer JavaDoc(m.getKey()), Boolean.valueOf(m.usesShiftModifier())});
343     }
344 }
345
346 // Component
347
class java_awt_Component_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
348     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
349         super.initialize(type, oldInstance, newInstance, out);
350         java.awt.Component JavaDoc c = (java.awt.Component JavaDoc)oldInstance;
351         java.awt.Component JavaDoc c2 = (java.awt.Component JavaDoc)newInstance;
352         // The "background", "foreground" and "font" properties.
353
// The foreground and font properties of Windows change from
354
// null to defined values after the Windows are made visible -
355
// special case them for now.
356
if (!(oldInstance instanceof java.awt.Window JavaDoc)) {
357             String JavaDoc[] fieldNames = new String JavaDoc[]{"background", "foreground", "font"};
358             for(int i = 0; i < fieldNames.length; i++) {
359                 String JavaDoc name = fieldNames[i];
360                 Object JavaDoc oldValue = ReflectionUtils.getPrivateField(oldInstance, java.awt.Component JavaDoc.class, name, out.getExceptionListener());
361                 Object JavaDoc newValue = (newInstance == null) ? null : ReflectionUtils.getPrivateField(newInstance, java.awt.Component JavaDoc.class, name, out.getExceptionListener());
362                 if (oldValue != null && !oldValue.equals(newValue)) {
363                     invokeStatement(oldInstance, "set" + NameGenerator.capitalize(name), new Object JavaDoc[]{oldValue}, out);
364                 }
365             }
366         }
367
368         // Bounds
369
java.awt.Container JavaDoc p = c.getParent();
370         if (p == null || p.getLayout() == null && !(p instanceof javax.swing.JLayeredPane JavaDoc)) {
371             // Use the most concise construct.
372
boolean locationCorrect = c.getLocation().equals(c2.getLocation());
373             boolean sizeCorrect = c.getSize().equals(c2.getSize());
374             if (!locationCorrect && !sizeCorrect) {
375                 invokeStatement(oldInstance, "setBounds", new Object JavaDoc[]{c.getBounds()}, out);
376             }
377             else if (!locationCorrect) {
378                 invokeStatement(oldInstance, "setLocation", new Object JavaDoc[]{c.getLocation()}, out);
379             }
380             else if (!sizeCorrect) {
381                 invokeStatement(oldInstance, "setSize", new Object JavaDoc[]{c.getSize()}, out);
382             }
383         }
384     }
385 }
386
387 // Container
388
class java_awt_Container_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
389     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
390         super.initialize(type, oldInstance, newInstance, out);
391         // Ignore the children of a JScrollPane.
392
// Pending(milne) find a better way to do this.
393
if (oldInstance instanceof javax.swing.JScrollPane JavaDoc) {
394             return;
395         }
396         java.awt.Container JavaDoc oldC = (java.awt.Container JavaDoc)oldInstance;
397         java.awt.Component JavaDoc[] oldChildren = oldC.getComponents();
398         java.awt.Container JavaDoc newC = (java.awt.Container JavaDoc)newInstance;
399         java.awt.Component JavaDoc[] newChildren = (newC == null) ? new java.awt.Component JavaDoc[0] : newC.getComponents();
400         // Pending. Assume all the new children are unaltered.
401
for(int i = newChildren.length; i < oldChildren.length; i++) {
402             invokeStatement(oldInstance, "add", new Object JavaDoc[]{oldChildren[i]}, out);
403         }
404     }
405 }
406
407 // Choice
408
class java_awt_Choice_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
409     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
410         super.initialize(type, oldInstance, newInstance, out);
411         java.awt.Choice JavaDoc m = (java.awt.Choice JavaDoc)oldInstance;
412         java.awt.Choice JavaDoc n = (java.awt.Choice JavaDoc)newInstance;
413         for (int i = n.getItemCount(); i < m.getItemCount(); i++) {
414             invokeStatement(oldInstance, "add", new Object JavaDoc[]{m.getItem(i)}, out);
415         }
416     }
417 }
418
419 // Menu
420
class java_awt_Menu_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
421     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
422         super.initialize(type, oldInstance, newInstance, out);
423         java.awt.Menu JavaDoc m = (java.awt.Menu JavaDoc)oldInstance;
424         java.awt.Menu JavaDoc n = (java.awt.Menu JavaDoc)newInstance;
425         for (int i = n.getItemCount(); i < m.getItemCount(); i++) {
426             invokeStatement(oldInstance, "add", new Object JavaDoc[]{m.getItem(i)}, out);
427         }
428     }
429 }
430
431 // MenuBar
432
class java_awt_MenuBar_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
433     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
434         super.initialize(type, oldInstance, newInstance, out);
435         java.awt.MenuBar JavaDoc m = (java.awt.MenuBar JavaDoc)oldInstance;
436         java.awt.MenuBar JavaDoc n = (java.awt.MenuBar JavaDoc)newInstance;
437         for (int i = n.getMenuCount(); i < m.getMenuCount(); i++) {
438             invokeStatement(oldInstance, "add", new Object JavaDoc[]{m.getMenu(i)}, out);
439         }
440     }
441 }
442
443 // List
444
class java_awt_List_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
445     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
446         super.initialize(type, oldInstance, newInstance, out);
447         java.awt.List JavaDoc m = (java.awt.List JavaDoc)oldInstance;
448         java.awt.List JavaDoc n = (java.awt.List JavaDoc)newInstance;
449         for (int i = n.getItemCount(); i < m.getItemCount(); i++) {
450             invokeStatement(oldInstance, "add", new Object JavaDoc[]{m.getItem(i)}, out);
451         }
452     }
453 }
454     
455
456 // LayoutManagers
457

458 // BorderLayout
459
class java_awt_BorderLayout_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
460     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance,
461                   Object JavaDoc newInstance, Encoder JavaDoc out) {
462         super.initialize(type, oldInstance, newInstance, out);
463         String JavaDoc[] locations = {"north", "south", "east", "west", "center"};
464         String JavaDoc[] names = {java.awt.BorderLayout.NORTH, java.awt.BorderLayout.SOUTH,
465               java.awt.BorderLayout.EAST, java.awt.BorderLayout.WEST,
466               java.awt.BorderLayout.CENTER};
467         for(int i = 0; i < locations.length; i++) {
468             Object JavaDoc oldC = ReflectionUtils.getPrivateField(oldInstance,
469                               java.awt.BorderLayout JavaDoc.class,
470                               locations[i],
471                               out.getExceptionListener());
472             Object JavaDoc newC = ReflectionUtils.getPrivateField(newInstance,
473                               java.awt.BorderLayout JavaDoc.class,
474                               locations[i],
475                               out.getExceptionListener());
476             // Pending, assume any existing elements are OK.
477
if (oldC != null && newC == null) {
478                 invokeStatement(oldInstance, "addLayoutComponent",
479                 new Object JavaDoc[]{oldC, names[i]}, out);
480             }
481         }
482     }
483 }
484
485 // CardLayout
486
class java_awt_CardLayout_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
487     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance,
488                   Object JavaDoc newInstance, Encoder JavaDoc out) {
489         super.initialize(type, oldInstance, newInstance, out);
490         Hashtable JavaDoc tab = (Hashtable JavaDoc)ReflectionUtils.getPrivateField(oldInstance,
491                                    java.awt.CardLayout JavaDoc.class,
492                                    "tab",
493                                    out.getExceptionListener());
494         if (tab != null) {
495             for(Enumeration JavaDoc e = tab.keys(); e.hasMoreElements();) {
496                 Object JavaDoc child = e.nextElement();
497                 invokeStatement(oldInstance, "addLayoutComponent",
498                                 new Object JavaDoc[]{child, (String JavaDoc)tab.get(child)}, out);
499             }
500         }
501     }
502 }
503
504 // GridBagLayout
505
class java_awt_GridBagLayout_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
506     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance,
507                   Object JavaDoc newInstance, Encoder JavaDoc out) {
508         super.initialize(type, oldInstance, newInstance, out);
509         Hashtable JavaDoc comptable = (Hashtable JavaDoc)ReflectionUtils.getPrivateField(oldInstance,
510                          java.awt.GridBagLayout JavaDoc.class,
511                          "comptable",
512                          out.getExceptionListener());
513         if (comptable != null) {
514             for(Enumeration JavaDoc e = comptable.keys(); e.hasMoreElements();) {
515                 Object JavaDoc child = e.nextElement();
516                 invokeStatement(oldInstance, "addLayoutComponent",
517                                 new Object JavaDoc[]{child, comptable.get(child)}, out);
518             }
519         }
520     }
521 }
522
523 // Swing
524

525 // JFrame (If we do this for Window instead of JFrame, the setVisible call
526
// will be issued before we have added all the children to the JFrame and
527
// will appear blank).
528
class javax_swing_JFrame_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
529     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
530         super.initialize(type, oldInstance, newInstance, out);
531         java.awt.Window JavaDoc oldC = (java.awt.Window JavaDoc)oldInstance;
532         java.awt.Window JavaDoc newC = (java.awt.Window JavaDoc)newInstance;
533         boolean oldV = oldC.isVisible();
534         boolean newV = newC.isVisible();
535         if (newV != oldV) {
536             // false means: don't execute this statement at write time.
537
boolean executeStatements = out.executeStatements;
538             out.executeStatements = false;
539             invokeStatement(oldInstance, "setVisible", new Object JavaDoc[]{Boolean.valueOf(oldV)}, out);
540             out.executeStatements = executeStatements;
541         }
542     }
543 }
544
545 // Models
546

547 // DefaultListModel
548
class javax_swing_DefaultListModel_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
549     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
550         // Note, the "size" property will be set here.
551
super.initialize(type, oldInstance, newInstance, out);
552         javax.swing.DefaultListModel JavaDoc m = (javax.swing.DefaultListModel JavaDoc)oldInstance;
553         javax.swing.DefaultListModel JavaDoc n = (javax.swing.DefaultListModel JavaDoc)newInstance;
554         for (int i = n.getSize(); i < m.getSize(); i++) {
555             invokeStatement(oldInstance, "add", // Can also use "addElement".
556
new Object JavaDoc[]{m.getElementAt(i)}, out);
557         }
558     }
559 }
560
561 // DefaultComboBoxModel
562
class javax_swing_DefaultComboBoxModel_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
563     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
564         super.initialize(type, oldInstance, newInstance, out);
565         javax.swing.DefaultComboBoxModel JavaDoc m = (javax.swing.DefaultComboBoxModel JavaDoc)oldInstance;
566         for (int i = 0; i < m.getSize(); i++) {
567             invokeStatement(oldInstance, "addElement", new Object JavaDoc[]{m.getElementAt(i)}, out);
568         }
569     }
570 }
571
572
573 // DefaultMutableTreeNode
574
class javax_swing_tree_DefaultMutableTreeNode_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
575     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc
576                   newInstance, Encoder JavaDoc out) {
577         super.initialize(type, oldInstance, newInstance, out);
578         javax.swing.tree.DefaultMutableTreeNode JavaDoc m =
579         (javax.swing.tree.DefaultMutableTreeNode JavaDoc)oldInstance;
580         javax.swing.tree.DefaultMutableTreeNode JavaDoc n =
581         (javax.swing.tree.DefaultMutableTreeNode JavaDoc)newInstance;
582         for (int i = n.getChildCount(); i < m.getChildCount(); i++) {
583             invokeStatement(oldInstance, "add", new
584         Object JavaDoc[]{m.getChildAt(i)}, out);
585         }
586     }
587 }
588
589 // ToolTipManager
590
class javax_swing_ToolTipManager_PersistenceDelegate extends PersistenceDelegate JavaDoc {
591     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
592         return new Expression JavaDoc(oldInstance, javax.swing.ToolTipManager JavaDoc.class,
593                               "sharedInstance", new Object JavaDoc[]{});
594     }
595 }
596
597 // JTabbedPane
598
class javax_swing_JTabbedPane_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
599     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
600         super.initialize(type, oldInstance, newInstance, out);
601         javax.swing.JTabbedPane JavaDoc p = (javax.swing.JTabbedPane JavaDoc)oldInstance;
602         for (int i = 0; i < p.getTabCount(); i++) {
603             invokeStatement(oldInstance, "addTab",
604                                           new Object JavaDoc[]{
605                                               p.getTitleAt(i),
606                                               p.getIconAt(i),
607                                               p.getComponentAt(i)}, out);
608         }
609     }
610 }
611
612 // Box
613
class javax_swing_Box_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
614     protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
615     javax.swing.BoxLayout JavaDoc lm = (javax.swing.BoxLayout JavaDoc)((javax.swing.Box JavaDoc)oldInstance).getLayout();
616     
617     Object JavaDoc value = ReflectionUtils.getPrivateField(lm, javax.swing.BoxLayout JavaDoc.class, "axis",
618                        out.getExceptionListener());
619     String JavaDoc method = ((Integer JavaDoc)value).intValue() == javax.swing.BoxLayout.X_AXIS ?
620         "createHorizontalBox" : "createVerticalBox";
621     return new Expression JavaDoc(oldInstance, oldInstance.getClass(), method, new Object JavaDoc[0]);
622     }
623 }
624
625 // JMenu
626
// Note that we do not need to state the initialiser for
627
// JMenuItems since the getComponents() method defined in
628
// Container will return all of the sub menu items that
629
// need to be added to the menu item.
630
// Not so for JMenu apparently.
631
class javax_swing_JMenu_PersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
632     protected void initialize(Class JavaDoc<?> type, Object JavaDoc oldInstance, Object JavaDoc newInstance, Encoder JavaDoc out) {
633         super.initialize(type, oldInstance, newInstance, out);
634         javax.swing.JMenu JavaDoc m = (javax.swing.JMenu JavaDoc)oldInstance;
635         java.awt.Component JavaDoc[] c = m.getMenuComponents();
636         for (int i = 0; i < c.length; i++) {
637             invokeStatement(oldInstance, "add", new Object JavaDoc[]{c[i]}, out);
638         }
639     }
640 }
641
642 /* XXX - doens't seem to work. Debug later.
643 class javax_swing_JMenu_PersistenceDelegate extends DefaultPersistenceDelegate {
644     protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
645         super.initialize(type, oldInstance, newInstance, out);
646         javax.swing.JMenu m = (javax.swing.JMenu)oldInstance;
647         javax.swing.JMenu n = (javax.swing.JMenu)newInstance;
648         for (int i = n.getItemCount(); i < m.getItemCount(); i++) {
649             invokeStatement(oldInstance, "add", new Object[]{m.getItem(i)}, out);
650         }
651     }
652 }
653 */

654
655 class MetaData {
656     private static Hashtable JavaDoc internalPersistenceDelegates = new Hashtable JavaDoc();
657     private static Hashtable JavaDoc transientProperties = new Hashtable JavaDoc();
658
659     private static PersistenceDelegate JavaDoc nullPersistenceDelegate = new NullPersistenceDelegate();
660     private static PersistenceDelegate JavaDoc primitivePersistenceDelegate = new PrimitivePersistenceDelegate();
661     private static PersistenceDelegate JavaDoc defaultPersistenceDelegate = new DefaultPersistenceDelegate JavaDoc();
662     private static PersistenceDelegate JavaDoc arrayPersistenceDelegate;
663     private static PersistenceDelegate JavaDoc proxyPersistenceDelegate;
664
665     static {
666
667 // Constructors.
668

669   // util
670

671         registerConstructor("java.util.Date", new String JavaDoc[]{"time"});
672
673   // beans
674

675         registerConstructor("java.beans.Statement", new String JavaDoc[]{"target", "methodName", "arguments"});
676         registerConstructor("java.beans.Expression", new String JavaDoc[]{"target", "methodName", "arguments"});
677         registerConstructor("java.beans.EventHandler", new String JavaDoc[]{"target", "action", "eventPropertyName", "listenerMethodName"});
678
679   // awt
680

681         registerConstructor("java.awt.Point", new String JavaDoc[]{"x", "y"});
682         registerConstructor("java.awt.Dimension", new String JavaDoc[]{"width", "height"});
683         registerConstructor("java.awt.Rectangle", new String JavaDoc[]{"x", "y", "width", "height"});
684
685         registerConstructor("java.awt.Insets", new String JavaDoc[]{"top", "left", "bottom", "right"});
686         registerConstructor("java.awt.Color", new String JavaDoc[]{"red", "green", "blue", "alpha"});
687         registerConstructor("java.awt.Font", new String JavaDoc[]{"name", "style", "size"});
688         registerConstructor("java.awt.Cursor", new String JavaDoc[]{"type"});
689         registerConstructor("java.awt.GridBagConstraints",
690                             new String JavaDoc[]{"gridx", "gridy", "gridwidth", "gridheight",
691                                          "weightx", "weighty",
692                                          "anchor", "fill", "insets",
693                                          "ipadx", "ipady"});
694         registerConstructor("java.awt.ScrollPane", new String JavaDoc[]{"scrollbarDisplayPolicy"});
695
696   // swing
697

698         registerConstructor("javax.swing.plaf.FontUIResource", new String JavaDoc[]{"name", "style", "size"});
699         registerConstructor("javax.swing.plaf.ColorUIResource", new String JavaDoc[]{"red", "green", "blue"});
700
701         registerConstructor("javax.swing.tree.DefaultTreeModel", new String JavaDoc[]{"root"});
702         registerConstructor("javax.swing.JTree", new String JavaDoc[]{"model"});
703         registerConstructor("javax.swing.tree.TreePath", new String JavaDoc[]{"path"});
704
705         registerConstructor("javax.swing.OverlayLayout", new String JavaDoc[]{"target"});
706         registerConstructor("javax.swing.BoxLayout", new String JavaDoc[]{"target", "axis"});
707         registerConstructor("javax.swing.Box$Filler", new String JavaDoc[]{"minimumSize", "preferredSize",
708                                    "maximumSize"});
709         registerConstructor("javax.swing.DefaultCellEditor", new String JavaDoc[]{"component"});
710
711         /*
712         This is required because the JSplitPane reveals a private layout class
713         called BasicSplitPaneUI$BasicVerticalLayoutManager which changes with
714         the orientation. To avoid the necessity for instantiating it we cause
715         the orientation attribute to get set before the layout manager - that
716         way the layout manager will be changed as a side effect. Unfortunately,
717         the layout property belongs to the superclass and therefore precedes
718         the orientation property. PENDING - we need to allow this kind of
719         modification. For now, put the property in the constructor.
720         */

721         registerConstructor("javax.swing.JSplitPane", new String JavaDoc[]{"orientation"});
722         // Try to synthesize the ImageIcon from its description.
723
registerConstructor("javax.swing.ImageIcon", new String JavaDoc[]{"description"});
724         // JButton's "label" and "actionCommand" properties are related,
725
// use the label as a constructor argument to ensure that it is set first.
726
// This remove the benign, but unnecessary, manipulation of actionCommand
727
// property in the common case.
728
registerConstructor("javax.swing.JButton", new String JavaDoc[]{"label"});
729
730         // borders
731

732         registerConstructor("javax.swing.border.BevelBorder", new String JavaDoc[]{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"});
733         registerConstructor("javax.swing.plaf.BorderUIResource$BevelBorderUIResource", new String JavaDoc[]{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"});
734         registerConstructor("javax.swing.border.CompoundBorder", new String JavaDoc[]{"outsideBorder", "insideBorder"});
735         registerConstructor("javax.swing.plaf.BorderUIResource$CompoundBorderUIResource", new String JavaDoc[]{"outsideBorder", "insideBorder"});
736         registerConstructor("javax.swing.border.EmptyBorder", new String JavaDoc[]{"top", "left", "bottom", "right"});
737         registerConstructor("javax.swing.plaf.BorderUIResource$EmptyBorderUIResource", new String JavaDoc[]{"top", "left", "bottom", "right"});
738         registerConstructor("javax.swing.border.EtchedBorder", new String JavaDoc[]{"etchType", "highlight", "shadow"});
739         registerConstructor("javax.swing.plaf.BorderUIResource$EtchedBorderUIResource", new String JavaDoc[]{"etchType", "highlight", "shadow"});
740         registerConstructor("javax.swing.border.LineBorder", new String JavaDoc[]{"lineColor", "thickness"});
741         registerConstructor("javax.swing.plaf.BorderUIResource$LineBorderUIResource", new String JavaDoc[]{"lineColor", "thickness"});
742         // Note this should check to see which of "color" and "tileIcon" is non-null.
743
registerConstructor("javax.swing.border.MatteBorder", new String JavaDoc[]{"top", "left", "bottom", "right", "tileIcon"});
744         registerConstructor("javax.swing.plaf.BorderUIResource$MatteBorderUIResource", new String JavaDoc[]{"top", "left", "bottom", "right", "tileIcon"});
745         registerConstructor("javax.swing.border.SoftBevelBorder", new String JavaDoc[]{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"});
746         // registerConstructorWithBadEqual("javax.swing.plaf.BorderUIResource$SoftBevelBorderUIResource", new String[]{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"});
747
registerConstructor("javax.swing.border.TitledBorder", new String JavaDoc[]{"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"});
748         registerConstructor("javax.swing.plaf.BorderUIResource$TitledBorderUIResource", new String JavaDoc[]{"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"});
749
750 // Transient properties
751

752   // awt
753

754     // Infinite graphs.
755
removeProperty("java.awt.geom.RectangularShape", "frame");
756         // removeProperty("java.awt.Rectangle2D", "frame");
757
// removeProperty("java.awt.Rectangle", "frame");
758

759         removeProperty("java.awt.Rectangle", "bounds");
760         removeProperty("java.awt.Dimension", "size");
761         removeProperty("java.awt.Point", "location");
762
763         // The color and font properties in Component need special treatment, see above.
764
removeProperty("java.awt.Component", "foreground");
765         removeProperty("java.awt.Component", "background");
766         removeProperty("java.awt.Component", "font");
767
768         // The visible property of Component needs special treatment because of Windows.
769
removeProperty("java.awt.Component", "visible");
770
771         // This property throws an exception if accessed when there is no child.
772
removeProperty("java.awt.ScrollPane", "scrollPosition");
773         
774     // 4917458 this should be removed for XAWT since it may throw
775
// an unsupported exception if there isn't any input methods.
776
// This shouldn't be a problem since these are added behind
777
// the scenes automatically.
778
removeProperty("java.awt.im.InputContext", "compositionEnabled");
779
780   // swing
781

782         // The size properties in JComponent need special treatment, see above.
783
removeProperty("javax.swing.JComponent", "minimumSize");
784         removeProperty("javax.swing.JComponent", "preferredSize");
785         removeProperty("javax.swing.JComponent", "maximumSize");
786
787         // These properties have platform specific implementations
788
// and should not appear in archives.
789
removeProperty("javax.swing.ImageIcon", "image");
790         removeProperty("javax.swing.ImageIcon", "imageObserver");
791
792         // This property throws an exception when set in JMenu.
793
// PENDING: Note we must delete the property from
794
// the superclass even though the superclass's
795
// implementation does not throw an error.
796
// This needs some more thought.
797
removeProperty("javax.swing.JMenu", "accelerator");
798         removeProperty("javax.swing.JMenuItem", "accelerator");
799         // This property unconditionally throws a "not implemented" exception.
800
removeProperty("javax.swing.JMenuBar", "helpMenu");
801
802         // The scrollBars in a JScrollPane are dynamic and should not
803
// be archived. The row and columns headers are changed by
804
// components like JTable on "addNotify".
805
removeProperty("javax.swing.JScrollPane", "verticalScrollBar");
806         removeProperty("javax.swing.JScrollPane", "horizontalScrollBar");
807         removeProperty("javax.swing.JScrollPane", "rowHeader");
808         removeProperty("javax.swing.JScrollPane", "columnHeader");
809         
810         removeProperty("javax.swing.JViewport", "extentSize");
811
812         // Renderers need special treatment, since their properties
813
// change during rendering.
814
removeProperty("javax.swing.table.JTableHeader", "defaultRenderer");
815         removeProperty("javax.swing.JList", "cellRenderer");
816
817         removeProperty("javax.swing.JList", "selectedIndices");
818
819         // The lead and anchor selection indexes are best ignored.
820
// Selection is rarely something that should persist from
821
// development to deployment.
822
removeProperty("javax.swing.DefaultListSelectionModel", "leadSelectionIndex");
823         removeProperty("javax.swing.DefaultListSelectionModel", "anchorSelectionIndex");
824
825         // The selection must come after the text itself.
826
removeProperty("javax.swing.JComboBox", "selectedIndex");
827
828         // All selection information should come after the JTabbedPane is built
829
removeProperty("javax.swing.JTabbedPane", "selectedIndex");
830         removeProperty("javax.swing.JTabbedPane", "selectedComponent");
831
832         // PENDING: The "disabledIcon" property is often computed from the icon property.
833
removeProperty("javax.swing.AbstractButton", "disabledIcon");
834     removeProperty("javax.swing.JLabel", "disabledIcon");
835
836         // The caret property throws errors when it it set beyond
837
// the extent of the text. We could just set it after the
838
// text, but this is probably not something we want to archive anyway.
839
removeProperty("javax.swing.text.JTextComponent", "caret");
840         removeProperty("javax.swing.text.JTextComponent", "caretPosition");
841         // The selectionStart must come after the text itself.
842
removeProperty("javax.swing.text.JTextComponent", "selectionStart");
843         removeProperty("javax.swing.text.JTextComponent", "selectionEnd");
844     }
845
846     /*pp*/ static boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
847         return (o1 == null) ? (o2 == null) : o1.equals(o2);
848     }
849
850
851
852     // Entry points for Encoder.
853

854     public synchronized static void setPersistenceDelegate(Class JavaDoc type,
855                      PersistenceDelegate JavaDoc persistenceDelegate) {
856     setBeanAttribute(type, "persistenceDelegate", persistenceDelegate);
857     }
858
859     public synchronized static PersistenceDelegate JavaDoc getPersistenceDelegate(Class JavaDoc type) {
860         if (type == null) {
861             return nullPersistenceDelegate;
862         }
863         if (ReflectionUtils.isPrimitive(type)) {
864             return primitivePersistenceDelegate;
865         }
866         // The persistence delegate for arrays is non-trivial; instantiate it lazily.
867
if (type.isArray()) {
868             if (arrayPersistenceDelegate == null) {
869                 arrayPersistenceDelegate = new ArrayPersistenceDelegate();
870             }
871             return arrayPersistenceDelegate;
872         }
873         // Handle proxies lazily for backward compatibility with 1.2.
874
try {
875             if (java.lang.reflect.Proxy.isProxyClass(type)) {
876                 if (proxyPersistenceDelegate == null) {
877                     proxyPersistenceDelegate = new ProxyPersistenceDelegate();
878                 }
879                 return proxyPersistenceDelegate;
880             }
881         }
882         catch(Exception JavaDoc e) {}
883         // else if (type.getDeclaringClass() != null) {
884
// return new DefaultPersistenceDelegate(new String[]{"this$0"});
885
// }
886

887     String JavaDoc typeName = type.getName();
888
889     // Check to see if there are properties that have been lazily registered for removal.
890
if (getBeanAttribute(type, "transient_init") == null) {
891         Vector JavaDoc tp = (Vector JavaDoc)transientProperties.get(typeName);
892         if (tp != null) {
893         for(int i = 0; i < tp.size(); i++) {
894             setPropertyAttribute(type, (String JavaDoc)tp.get(i), "transient", Boolean.TRUE);
895         }
896         }
897         setBeanAttribute(type, "transient_init", Boolean.TRUE);
898     }
899
900         PersistenceDelegate JavaDoc pd = (PersistenceDelegate JavaDoc)getBeanAttribute(type, "persistenceDelegate");
901         if (pd == null) {
902             pd = (PersistenceDelegate JavaDoc)internalPersistenceDelegates.get(typeName);
903             if (pd != null) {
904                 return pd;
905             }
906             internalPersistenceDelegates.put(typeName, defaultPersistenceDelegate);
907             try {
908                 String JavaDoc name = type.getName();
909                 Class JavaDoc c = Class.forName("java.beans." + name.replace('.', '_')
910                     + "_PersistenceDelegate");
911                 pd = (PersistenceDelegate JavaDoc)c.newInstance();
912                 internalPersistenceDelegates.put(typeName, pd);
913             }
914             catch (ClassNotFoundException JavaDoc e) {}
915             catch (Exception JavaDoc e) {
916                 System.err.println("Internal error: " + e);
917             }
918         }
919
920         return (pd != null) ? pd : defaultPersistenceDelegate;
921     }
922
923     // Wrapper for Introspector.getBeanInfo to handle exception handling.
924
// Note: this relys on new 1.4 Introspector semantics which cache the BeanInfos
925
public static BeanInfo JavaDoc getBeanInfo(Class JavaDoc type) {
926     BeanInfo JavaDoc info = null;
927     try {
928         info = Introspector.getBeanInfo(type);
929     } catch (Throwable JavaDoc e) {
930         e.printStackTrace();
931     }
932
933     return info;
934     }
935
936     private static PropertyDescriptor JavaDoc getPropertyDescriptor(Class JavaDoc type, String JavaDoc propertyName) {
937         BeanInfo JavaDoc info = getBeanInfo(type);
938         PropertyDescriptor JavaDoc[] propertyDescriptors = info.getPropertyDescriptors();
939         // System.out.println("Searching for: " + propertyName + " in " + type);
940
for(int i = 0; i < propertyDescriptors.length; i++) {
941             PropertyDescriptor JavaDoc pd = propertyDescriptors[i];
942             if (propertyName.equals(pd.getName())) {
943                 return pd;
944             }
945         }
946         return null;
947     }
948
949     private static void setPropertyAttribute(Class JavaDoc type, String JavaDoc property, String JavaDoc attribute, Object JavaDoc value) {
950         PropertyDescriptor JavaDoc pd = getPropertyDescriptor(type, property);
951         if (pd == null) {
952             System.err.println("Warning: property " + property + " is not defined on " + type);
953             return;
954         }
955         pd.setValue(attribute, value);
956     }
957
958     private static void setBeanAttribute(Class JavaDoc type, String JavaDoc attribute, Object JavaDoc value) {
959         getBeanInfo(type).getBeanDescriptor().setValue(attribute, value);
960     }
961
962     private static Object JavaDoc getBeanAttribute(Class JavaDoc type, String JavaDoc attribute) {
963     return getBeanInfo(type).getBeanDescriptor().getValue(attribute);
964     }
965
966     // MetaData registration
967

968     private synchronized static void registerConstructor(String JavaDoc typeName,
969                              String JavaDoc[] constructor) {
970         internalPersistenceDelegates.put(typeName,
971                      new DefaultPersistenceDelegate JavaDoc(constructor));
972     }
973
974     private static void removeProperty(String JavaDoc typeName, String JavaDoc property) {
975         Vector JavaDoc tp = (Vector JavaDoc)transientProperties.get(typeName);
976         if (tp == null) {
977             tp = new Vector JavaDoc();
978             transientProperties.put(typeName, tp);
979         }
980         tp.add(property);
981     }
982 }
983
984
Popular Tags