1 7 package java.beans; 8 9 import java.lang.reflect.Array ; 10 import java.lang.reflect.Field ; 11 import java.lang.reflect.Method ; 12 13 import java.util.Vector ; 14 import java.util.Hashtable ; 15 import java.util.Iterator ; 16 import java.util.Enumeration ; 17 18 30 31 class NullPersistenceDelegate extends PersistenceDelegate { 32 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 35 } 36 protected Expression instantiate(Object oldInstance, Encoder out) { return null; } 37 38 public void writeObject(Object oldInstance, Encoder out) { 39 } 41 } 42 43 class PrimitivePersistenceDelegate extends PersistenceDelegate { 44 protected boolean mutatesTo(Object oldInstance, Object newInstance) { 45 return oldInstance.equals(newInstance); 46 } 47 48 protected Expression instantiate(Object oldInstance, Encoder out) { 49 return new Expression (oldInstance, oldInstance.getClass(), 50 "new", new Object []{oldInstance.toString()}); 51 } 52 } 53 54 class ArrayPersistenceDelegate extends PersistenceDelegate { 55 protected boolean mutatesTo(Object oldInstance, Object newInstance) { 56 return (newInstance != null && 57 oldInstance.getClass() == newInstance.getClass() && Array.getLength(oldInstance) == Array.getLength(newInstance)); 59 } 60 61 protected Expression instantiate(Object oldInstance, Encoder out) { 62 Class oldClass = oldInstance.getClass(); 64 return new Expression (oldInstance, Array .class, "newInstance", 65 new Object []{oldClass.getComponentType(), 66 new Integer (Array.getLength(oldInstance))}); 67 } 68 69 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 70 int n = Array.getLength(oldInstance); 71 for (int i = 0; i < n; i++) { 72 Object index = new Integer (i); 73 Expression oldGetExp = new Expression (oldInstance, "get", new Object []{index}); 76 Expression newGetExp = new Expression (newInstance, "get", new Object []{index}); 77 try { 78 Object oldValue = oldGetExp.getValue(); 79 Object newValue = newGetExp.getValue(); 80 out.writeExpression(oldGetExp); 81 if (!MetaData.equals(newValue, out.get(oldValue))) { 82 DefaultPersistenceDelegate.invokeStatement(oldInstance, "set", new Object []{index, oldValue}, out); 85 } 86 } 87 catch (Exception e) { 88 out.getExceptionListener().exceptionThrown(e); 90 } 91 } 92 } 93 } 94 95 class ProxyPersistenceDelegate extends PersistenceDelegate { 96 protected Expression instantiate(Object oldInstance, Encoder out) { 97 Class type = oldInstance.getClass(); 98 java.lang.reflect.Proxy p = (java.lang.reflect.Proxy )oldInstance; 99 java.lang.reflect.InvocationHandler ih = java.lang.reflect.Proxy.getInvocationHandler(p); 102 if (ih instanceof EventHandler ) { 103 EventHandler eh = (EventHandler )ih; 104 Vector args = new Vector (); 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 (oldInstance, 116 EventHandler .class, 117 "create", 118 args.toArray()); 119 } 120 return new Expression (oldInstance, 121 java.lang.reflect.Proxy .class, 122 "newProxyInstance", 123 new Object []{type.getClassLoader(), 124 type.getInterfaces(), 125 ih}); 126 } 127 } 128 129 class java_lang_String_PersistenceDelegate extends PersistenceDelegate { 131 protected Expression instantiate(Object oldInstance, Encoder out) { return null; } 132 133 public void writeObject(Object oldInstance, Encoder out) { 134 } 136 } 137 138 class java_lang_Class_PersistenceDelegate extends PersistenceDelegate { 140 protected Expression instantiate(Object oldInstance, Encoder out) { 141 Class c = (Class )oldInstance; 142 if (c.isPrimitive()) { 146 Field field = null; 147 try { 148 field = ReflectionUtils.typeToClass(c).getDeclaredField("TYPE"); 149 } catch (NoSuchFieldException ex) { 150 System.err.println("Unknown primitive type: " + c); 151 } 152 return new Expression (oldInstance, field, "get", new Object []{null}); 153 } 154 else if (oldInstance == String .class) { 155 return new Expression (oldInstance, "", "getClass", new Object []{}); 156 } 157 else if (oldInstance == Class .class) { 158 return new Expression (oldInstance, String .class, "getClass", new Object []{}); 159 } 160 else { 161 return new Expression (oldInstance, Class .class, "forName", new Object []{c.getName()}); 162 } 163 } 164 } 165 166 class java_lang_reflect_Field_PersistenceDelegate extends PersistenceDelegate { 168 protected Expression instantiate(Object oldInstance, Encoder out) { 169 Field f = (Field )oldInstance; 170 return new Expression (oldInstance, 171 f.getDeclaringClass(), 172 "getField", 173 new Object []{f.getName()}); 174 } 175 } 176 177 class java_lang_reflect_Method_PersistenceDelegate extends PersistenceDelegate { 179 protected Expression instantiate(Object oldInstance, Encoder out) { 180 Method m = (Method )oldInstance; 181 return new Expression (oldInstance, 182 m.getDeclaringClass(), 183 "getMethod", 184 new Object []{m.getName(), m.getParameterTypes()}); 185 } 186 } 187 188 190 202 203 class java_util_Collection_PersistenceDelegate extends DefaultPersistenceDelegate { 205 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 206 java.util.Collection oldO = (java.util.Collection )oldInstance; 207 java.util.Collection newO = (java.util.Collection )newInstance; 208 209 if (newO.size() != 0) { 210 invokeStatement(oldInstance, "clear", new Object []{}, out); 211 } 212 for (Iterator i = oldO.iterator(); i.hasNext();) { 213 invokeStatement(oldInstance, "add", new Object []{i.next()}, out); 214 } 215 } 216 } 217 218 class java_util_List_PersistenceDelegate extends DefaultPersistenceDelegate { 220 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 221 java.util.List oldO = (java.util.List )oldInstance; 222 java.util.List newO = (java.util.List )newInstance; 223 int oldSize = oldO.size(); 224 int newSize = (newO == null) ? 0 : newO.size(); 225 if (oldSize < newSize) { 226 invokeStatement(oldInstance, "clear", new Object []{}, out); 227 newSize = 0; 228 } 229 for (int i = 0; i < newSize; i++) { 230 Object index = new Integer (i); 231 232 Expression oldGetExp = new Expression (oldInstance, "get", new Object []{index}); 233 Expression newGetExp = new Expression (newInstance, "get", new Object []{index}); 234 try { 235 Object oldValue = oldGetExp.getValue(); 236 Object newValue = newGetExp.getValue(); 237 out.writeExpression(oldGetExp); 238 if (!MetaData.equals(newValue, out.get(oldValue))) { 239 invokeStatement(oldInstance, "set", new Object []{index, oldValue}, out); 240 } 241 } 242 catch (Exception e) { 243 out.getExceptionListener().exceptionThrown(e); 244 } 245 } 246 for (int i = newSize; i < oldSize; i++) { 247 invokeStatement(oldInstance, "add", new Object []{oldO.get(i)}, out); 248 } 249 } 250 } 251 252 253 class java_util_Map_PersistenceDelegate extends DefaultPersistenceDelegate { 255 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 256 java.util.Map oldMap = (java.util.Map )oldInstance; 258 java.util.Map newMap = (java.util.Map )newInstance; 259 if (newMap != null) { 262 java.util.Iterator newKeys = newMap.keySet().iterator(); 263 while(newKeys.hasNext()) { 264 Object newKey = newKeys.next(); 265 if (!oldMap.containsKey(newKey)) { 267 invokeStatement(oldInstance, "remove", new Object []{newKey}, out); 268 } 269 } 270 } 271 java.util.Iterator oldKeys = oldMap.keySet().iterator(); 273 while(oldKeys.hasNext()) { 274 Object oldKey = oldKeys.next(); 275 276 Expression oldGetExp = new Expression (oldInstance, "get", new Object []{oldKey}); 277 Expression newGetExp = new Expression (newInstance, "get", new Object []{oldKey}); 279 try { 280 Object oldValue = oldGetExp.getValue(); 281 Object newValue = newGetExp.getValue(); 282 out.writeExpression(oldGetExp); 283 if (!MetaData.equals(newValue, out.get(oldValue))) { 284 invokeStatement(oldInstance, "put", new Object []{oldKey, oldValue}, out); 285 } 286 } 287 catch (Exception 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 class java_beans_beancontext_BeanContextSupport_PersistenceDelegate extends java_util_Collection_PersistenceDelegate {} 302 303 305 class StaticFieldsPersistenceDelegate extends PersistenceDelegate { 306 protected void installFields(Encoder out, Class <?> cls) { 307 Field fields[] = cls.getFields(); 308 for(int i = 0; i < fields.length; i++) { 309 Field field = fields[i]; 310 if (Object .class.isAssignableFrom(field.getType())) { 313 out.writeExpression(new Expression (field, "get", new Object []{null})); 314 } 315 } 316 } 317 318 protected Expression instantiate(Object oldInstance, Encoder out) { 319 throw new RuntimeException ("Unrecognized instance: " + oldInstance); 320 } 321 322 public void writeObject(Object oldInstance, Encoder 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 class java_awt_SystemColor_PersistenceDelegate extends StaticFieldsPersistenceDelegate {} 333 334 class java_awt_font_TextAttribute_PersistenceDelegate extends StaticFieldsPersistenceDelegate {} 336 337 class java_awt_MenuShortcut_PersistenceDelegate extends PersistenceDelegate { 339 protected Expression instantiate(Object oldInstance, Encoder out) { 340 java.awt.MenuShortcut m = (java.awt.MenuShortcut )oldInstance; 341 return new Expression (oldInstance, m.getClass(), "new", 342 new Object []{new Integer (m.getKey()), Boolean.valueOf(m.usesShiftModifier())}); 343 } 344 } 345 346 class java_awt_Component_PersistenceDelegate extends DefaultPersistenceDelegate { 348 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 349 super.initialize(type, oldInstance, newInstance, out); 350 java.awt.Component c = (java.awt.Component )oldInstance; 351 java.awt.Component c2 = (java.awt.Component )newInstance; 352 if (!(oldInstance instanceof java.awt.Window )) { 357 String [] fieldNames = new String []{"background", "foreground", "font"}; 358 for(int i = 0; i < fieldNames.length; i++) { 359 String name = fieldNames[i]; 360 Object oldValue = ReflectionUtils.getPrivateField(oldInstance, java.awt.Component .class, name, out.getExceptionListener()); 361 Object newValue = (newInstance == null) ? null : ReflectionUtils.getPrivateField(newInstance, java.awt.Component .class, name, out.getExceptionListener()); 362 if (oldValue != null && !oldValue.equals(newValue)) { 363 invokeStatement(oldInstance, "set" + NameGenerator.capitalize(name), new Object []{oldValue}, out); 364 } 365 } 366 } 367 368 java.awt.Container p = c.getParent(); 370 if (p == null || p.getLayout() == null && !(p instanceof javax.swing.JLayeredPane )) { 371 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 []{c.getBounds()}, out); 376 } 377 else if (!locationCorrect) { 378 invokeStatement(oldInstance, "setLocation", new Object []{c.getLocation()}, out); 379 } 380 else if (!sizeCorrect) { 381 invokeStatement(oldInstance, "setSize", new Object []{c.getSize()}, out); 382 } 383 } 384 } 385 } 386 387 class java_awt_Container_PersistenceDelegate extends DefaultPersistenceDelegate { 389 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 390 super.initialize(type, oldInstance, newInstance, out); 391 if (oldInstance instanceof javax.swing.JScrollPane ) { 394 return; 395 } 396 java.awt.Container oldC = (java.awt.Container )oldInstance; 397 java.awt.Component [] oldChildren = oldC.getComponents(); 398 java.awt.Container newC = (java.awt.Container )newInstance; 399 java.awt.Component [] newChildren = (newC == null) ? new java.awt.Component [0] : newC.getComponents(); 400 for(int i = newChildren.length; i < oldChildren.length; i++) { 402 invokeStatement(oldInstance, "add", new Object []{oldChildren[i]}, out); 403 } 404 } 405 } 406 407 class java_awt_Choice_PersistenceDelegate extends DefaultPersistenceDelegate { 409 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 410 super.initialize(type, oldInstance, newInstance, out); 411 java.awt.Choice m = (java.awt.Choice )oldInstance; 412 java.awt.Choice n = (java.awt.Choice )newInstance; 413 for (int i = n.getItemCount(); i < m.getItemCount(); i++) { 414 invokeStatement(oldInstance, "add", new Object []{m.getItem(i)}, out); 415 } 416 } 417 } 418 419 class java_awt_Menu_PersistenceDelegate extends DefaultPersistenceDelegate { 421 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 422 super.initialize(type, oldInstance, newInstance, out); 423 java.awt.Menu m = (java.awt.Menu )oldInstance; 424 java.awt.Menu n = (java.awt.Menu )newInstance; 425 for (int i = n.getItemCount(); i < m.getItemCount(); i++) { 426 invokeStatement(oldInstance, "add", new Object []{m.getItem(i)}, out); 427 } 428 } 429 } 430 431 class java_awt_MenuBar_PersistenceDelegate extends DefaultPersistenceDelegate { 433 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 434 super.initialize(type, oldInstance, newInstance, out); 435 java.awt.MenuBar m = (java.awt.MenuBar )oldInstance; 436 java.awt.MenuBar n = (java.awt.MenuBar )newInstance; 437 for (int i = n.getMenuCount(); i < m.getMenuCount(); i++) { 438 invokeStatement(oldInstance, "add", new Object []{m.getMenu(i)}, out); 439 } 440 } 441 } 442 443 class java_awt_List_PersistenceDelegate extends DefaultPersistenceDelegate { 445 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 446 super.initialize(type, oldInstance, newInstance, out); 447 java.awt.List m = (java.awt.List )oldInstance; 448 java.awt.List n = (java.awt.List )newInstance; 449 for (int i = n.getItemCount(); i < m.getItemCount(); i++) { 450 invokeStatement(oldInstance, "add", new Object []{m.getItem(i)}, out); 451 } 452 } 453 } 454 455 456 458 class java_awt_BorderLayout_PersistenceDelegate extends DefaultPersistenceDelegate { 460 protected void initialize(Class <?> type, Object oldInstance, 461 Object newInstance, Encoder out) { 462 super.initialize(type, oldInstance, newInstance, out); 463 String [] locations = {"north", "south", "east", "west", "center"}; 464 String [] 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 oldC = ReflectionUtils.getPrivateField(oldInstance, 469 java.awt.BorderLayout .class, 470 locations[i], 471 out.getExceptionListener()); 472 Object newC = ReflectionUtils.getPrivateField(newInstance, 473 java.awt.BorderLayout .class, 474 locations[i], 475 out.getExceptionListener()); 476 if (oldC != null && newC == null) { 478 invokeStatement(oldInstance, "addLayoutComponent", 479 new Object []{oldC, names[i]}, out); 480 } 481 } 482 } 483 } 484 485 class java_awt_CardLayout_PersistenceDelegate extends DefaultPersistenceDelegate { 487 protected void initialize(Class <?> type, Object oldInstance, 488 Object newInstance, Encoder out) { 489 super.initialize(type, oldInstance, newInstance, out); 490 Hashtable tab = (Hashtable )ReflectionUtils.getPrivateField(oldInstance, 491 java.awt.CardLayout .class, 492 "tab", 493 out.getExceptionListener()); 494 if (tab != null) { 495 for(Enumeration e = tab.keys(); e.hasMoreElements();) { 496 Object child = e.nextElement(); 497 invokeStatement(oldInstance, "addLayoutComponent", 498 new Object []{child, (String )tab.get(child)}, out); 499 } 500 } 501 } 502 } 503 504 class java_awt_GridBagLayout_PersistenceDelegate extends DefaultPersistenceDelegate { 506 protected void initialize(Class <?> type, Object oldInstance, 507 Object newInstance, Encoder out) { 508 super.initialize(type, oldInstance, newInstance, out); 509 Hashtable comptable = (Hashtable )ReflectionUtils.getPrivateField(oldInstance, 510 java.awt.GridBagLayout .class, 511 "comptable", 512 out.getExceptionListener()); 513 if (comptable != null) { 514 for(Enumeration e = comptable.keys(); e.hasMoreElements();) { 515 Object child = e.nextElement(); 516 invokeStatement(oldInstance, "addLayoutComponent", 517 new Object []{child, comptable.get(child)}, out); 518 } 519 } 520 } 521 } 522 523 525 class javax_swing_JFrame_PersistenceDelegate extends DefaultPersistenceDelegate { 529 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 530 super.initialize(type, oldInstance, newInstance, out); 531 java.awt.Window oldC = (java.awt.Window )oldInstance; 532 java.awt.Window newC = (java.awt.Window )newInstance; 533 boolean oldV = oldC.isVisible(); 534 boolean newV = newC.isVisible(); 535 if (newV != oldV) { 536 boolean executeStatements = out.executeStatements; 538 out.executeStatements = false; 539 invokeStatement(oldInstance, "setVisible", new Object []{Boolean.valueOf(oldV)}, out); 540 out.executeStatements = executeStatements; 541 } 542 } 543 } 544 545 547 class javax_swing_DefaultListModel_PersistenceDelegate extends DefaultPersistenceDelegate { 549 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 550 super.initialize(type, oldInstance, newInstance, out); 552 javax.swing.DefaultListModel m = (javax.swing.DefaultListModel )oldInstance; 553 javax.swing.DefaultListModel n = (javax.swing.DefaultListModel )newInstance; 554 for (int i = n.getSize(); i < m.getSize(); i++) { 555 invokeStatement(oldInstance, "add", new Object []{m.getElementAt(i)}, out); 557 } 558 } 559 } 560 561 class javax_swing_DefaultComboBoxModel_PersistenceDelegate extends DefaultPersistenceDelegate { 563 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 564 super.initialize(type, oldInstance, newInstance, out); 565 javax.swing.DefaultComboBoxModel m = (javax.swing.DefaultComboBoxModel )oldInstance; 566 for (int i = 0; i < m.getSize(); i++) { 567 invokeStatement(oldInstance, "addElement", new Object []{m.getElementAt(i)}, out); 568 } 569 } 570 } 571 572 573 class javax_swing_tree_DefaultMutableTreeNode_PersistenceDelegate extends DefaultPersistenceDelegate { 575 protected void initialize(Class <?> type, Object oldInstance, Object 576 newInstance, Encoder out) { 577 super.initialize(type, oldInstance, newInstance, out); 578 javax.swing.tree.DefaultMutableTreeNode m = 579 (javax.swing.tree.DefaultMutableTreeNode )oldInstance; 580 javax.swing.tree.DefaultMutableTreeNode n = 581 (javax.swing.tree.DefaultMutableTreeNode )newInstance; 582 for (int i = n.getChildCount(); i < m.getChildCount(); i++) { 583 invokeStatement(oldInstance, "add", new 584 Object []{m.getChildAt(i)}, out); 585 } 586 } 587 } 588 589 class javax_swing_ToolTipManager_PersistenceDelegate extends PersistenceDelegate { 591 protected Expression instantiate(Object oldInstance, Encoder out) { 592 return new Expression (oldInstance, javax.swing.ToolTipManager .class, 593 "sharedInstance", new Object []{}); 594 } 595 } 596 597 class javax_swing_JTabbedPane_PersistenceDelegate extends DefaultPersistenceDelegate { 599 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 600 super.initialize(type, oldInstance, newInstance, out); 601 javax.swing.JTabbedPane p = (javax.swing.JTabbedPane )oldInstance; 602 for (int i = 0; i < p.getTabCount(); i++) { 603 invokeStatement(oldInstance, "addTab", 604 new Object []{ 605 p.getTitleAt(i), 606 p.getIconAt(i), 607 p.getComponentAt(i)}, out); 608 } 609 } 610 } 611 612 class javax_swing_Box_PersistenceDelegate extends DefaultPersistenceDelegate { 614 protected Expression instantiate(Object oldInstance, Encoder out) { 615 javax.swing.BoxLayout lm = (javax.swing.BoxLayout )((javax.swing.Box )oldInstance).getLayout(); 616 617 Object value = ReflectionUtils.getPrivateField(lm, javax.swing.BoxLayout .class, "axis", 618 out.getExceptionListener()); 619 String method = ((Integer )value).intValue() == javax.swing.BoxLayout.X_AXIS ? 620 "createHorizontalBox" : "createVerticalBox"; 621 return new Expression (oldInstance, oldInstance.getClass(), method, new Object [0]); 622 } 623 } 624 625 class javax_swing_JMenu_PersistenceDelegate extends DefaultPersistenceDelegate { 632 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) { 633 super.initialize(type, oldInstance, newInstance, out); 634 javax.swing.JMenu m = (javax.swing.JMenu )oldInstance; 635 java.awt.Component [] c = m.getMenuComponents(); 636 for (int i = 0; i < c.length; i++) { 637 invokeStatement(oldInstance, "add", new Object []{c[i]}, out); 638 } 639 } 640 } 641 642 654 655 class MetaData { 656 private static Hashtable internalPersistenceDelegates = new Hashtable (); 657 private static Hashtable transientProperties = new Hashtable (); 658 659 private static PersistenceDelegate nullPersistenceDelegate = new NullPersistenceDelegate(); 660 private static PersistenceDelegate primitivePersistenceDelegate = new PrimitivePersistenceDelegate(); 661 private static PersistenceDelegate defaultPersistenceDelegate = new DefaultPersistenceDelegate (); 662 private static PersistenceDelegate arrayPersistenceDelegate; 663 private static PersistenceDelegate proxyPersistenceDelegate; 664 665 static { 666 667 669 671 registerConstructor("java.util.Date", new String []{"time"}); 672 673 675 registerConstructor("java.beans.Statement", new String []{"target", "methodName", "arguments"}); 676 registerConstructor("java.beans.Expression", new String []{"target", "methodName", "arguments"}); 677 registerConstructor("java.beans.EventHandler", new String []{"target", "action", "eventPropertyName", "listenerMethodName"}); 678 679 681 registerConstructor("java.awt.Point", new String []{"x", "y"}); 682 registerConstructor("java.awt.Dimension", new String []{"width", "height"}); 683 registerConstructor("java.awt.Rectangle", new String []{"x", "y", "width", "height"}); 684 685 registerConstructor("java.awt.Insets", new String []{"top", "left", "bottom", "right"}); 686 registerConstructor("java.awt.Color", new String []{"red", "green", "blue", "alpha"}); 687 registerConstructor("java.awt.Font", new String []{"name", "style", "size"}); 688 registerConstructor("java.awt.Cursor", new String []{"type"}); 689 registerConstructor("java.awt.GridBagConstraints", 690 new String []{"gridx", "gridy", "gridwidth", "gridheight", 691 "weightx", "weighty", 692 "anchor", "fill", "insets", 693 "ipadx", "ipady"}); 694 registerConstructor("java.awt.ScrollPane", new String []{"scrollbarDisplayPolicy"}); 695 696 698 registerConstructor("javax.swing.plaf.FontUIResource", new String []{"name", "style", "size"}); 699 registerConstructor("javax.swing.plaf.ColorUIResource", new String []{"red", "green", "blue"}); 700 701 registerConstructor("javax.swing.tree.DefaultTreeModel", new String []{"root"}); 702 registerConstructor("javax.swing.JTree", new String []{"model"}); 703 registerConstructor("javax.swing.tree.TreePath", new String []{"path"}); 704 705 registerConstructor("javax.swing.OverlayLayout", new String []{"target"}); 706 registerConstructor("javax.swing.BoxLayout", new String []{"target", "axis"}); 707 registerConstructor("javax.swing.Box$Filler", new String []{"minimumSize", "preferredSize", 708 "maximumSize"}); 709 registerConstructor("javax.swing.DefaultCellEditor", new String []{"component"}); 710 711 721 registerConstructor("javax.swing.JSplitPane", new String []{"orientation"}); 722 registerConstructor("javax.swing.ImageIcon", new String []{"description"}); 724 registerConstructor("javax.swing.JButton", new String []{"label"}); 729 730 732 registerConstructor("javax.swing.border.BevelBorder", new String []{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"}); 733 registerConstructor("javax.swing.plaf.BorderUIResource$BevelBorderUIResource", new String []{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"}); 734 registerConstructor("javax.swing.border.CompoundBorder", new String []{"outsideBorder", "insideBorder"}); 735 registerConstructor("javax.swing.plaf.BorderUIResource$CompoundBorderUIResource", new String []{"outsideBorder", "insideBorder"}); 736 registerConstructor("javax.swing.border.EmptyBorder", new String []{"top", "left", "bottom", "right"}); 737 registerConstructor("javax.swing.plaf.BorderUIResource$EmptyBorderUIResource", new String []{"top", "left", "bottom", "right"}); 738 registerConstructor("javax.swing.border.EtchedBorder", new String []{"etchType", "highlight", "shadow"}); 739 registerConstructor("javax.swing.plaf.BorderUIResource$EtchedBorderUIResource", new String []{"etchType", "highlight", "shadow"}); 740 registerConstructor("javax.swing.border.LineBorder", new String []{"lineColor", "thickness"}); 741 registerConstructor("javax.swing.plaf.BorderUIResource$LineBorderUIResource", new String []{"lineColor", "thickness"}); 742 registerConstructor("javax.swing.border.MatteBorder", new String []{"top", "left", "bottom", "right", "tileIcon"}); 744 registerConstructor("javax.swing.plaf.BorderUIResource$MatteBorderUIResource", new String []{"top", "left", "bottom", "right", "tileIcon"}); 745 registerConstructor("javax.swing.border.SoftBevelBorder", new String []{"bevelType", "highlightOuter", "highlightInner", "shadowOuter", "shadowInner"}); 746 registerConstructor("javax.swing.border.TitledBorder", new String []{"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"}); 748 registerConstructor("javax.swing.plaf.BorderUIResource$TitledBorderUIResource", new String []{"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"}); 749 750 752 754 removeProperty("java.awt.geom.RectangularShape", "frame"); 756 759 removeProperty("java.awt.Rectangle", "bounds"); 760 removeProperty("java.awt.Dimension", "size"); 761 removeProperty("java.awt.Point", "location"); 762 763 removeProperty("java.awt.Component", "foreground"); 765 removeProperty("java.awt.Component", "background"); 766 removeProperty("java.awt.Component", "font"); 767 768 removeProperty("java.awt.Component", "visible"); 770 771 removeProperty("java.awt.ScrollPane", "scrollPosition"); 773 774 removeProperty("java.awt.im.InputContext", "compositionEnabled"); 779 780 782 removeProperty("javax.swing.JComponent", "minimumSize"); 784 removeProperty("javax.swing.JComponent", "preferredSize"); 785 removeProperty("javax.swing.JComponent", "maximumSize"); 786 787 removeProperty("javax.swing.ImageIcon", "image"); 790 removeProperty("javax.swing.ImageIcon", "imageObserver"); 791 792 removeProperty("javax.swing.JMenu", "accelerator"); 798 removeProperty("javax.swing.JMenuItem", "accelerator"); 799 removeProperty("javax.swing.JMenuBar", "helpMenu"); 801 802 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 removeProperty("javax.swing.table.JTableHeader", "defaultRenderer"); 815 removeProperty("javax.swing.JList", "cellRenderer"); 816 817 removeProperty("javax.swing.JList", "selectedIndices"); 818 819 removeProperty("javax.swing.DefaultListSelectionModel", "leadSelectionIndex"); 823 removeProperty("javax.swing.DefaultListSelectionModel", "anchorSelectionIndex"); 824 825 removeProperty("javax.swing.JComboBox", "selectedIndex"); 827 828 removeProperty("javax.swing.JTabbedPane", "selectedIndex"); 830 removeProperty("javax.swing.JTabbedPane", "selectedComponent"); 831 832 removeProperty("javax.swing.AbstractButton", "disabledIcon"); 834 removeProperty("javax.swing.JLabel", "disabledIcon"); 835 836 removeProperty("javax.swing.text.JTextComponent", "caret"); 840 removeProperty("javax.swing.text.JTextComponent", "caretPosition"); 841 removeProperty("javax.swing.text.JTextComponent", "selectionStart"); 843 removeProperty("javax.swing.text.JTextComponent", "selectionEnd"); 844 } 845 846 static boolean equals(Object o1, Object o2) { 847 return (o1 == null) ? (o2 == null) : o1.equals(o2); 848 } 849 850 851 852 854 public synchronized static void setPersistenceDelegate(Class type, 855 PersistenceDelegate persistenceDelegate) { 856 setBeanAttribute(type, "persistenceDelegate", persistenceDelegate); 857 } 858 859 public synchronized static PersistenceDelegate getPersistenceDelegate(Class type) { 860 if (type == null) { 861 return nullPersistenceDelegate; 862 } 863 if (ReflectionUtils.isPrimitive(type)) { 864 return primitivePersistenceDelegate; 865 } 866 if (type.isArray()) { 868 if (arrayPersistenceDelegate == null) { 869 arrayPersistenceDelegate = new ArrayPersistenceDelegate(); 870 } 871 return arrayPersistenceDelegate; 872 } 873 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 e) {} 883 887 String typeName = type.getName(); 888 889 if (getBeanAttribute(type, "transient_init") == null) { 891 Vector tp = (Vector )transientProperties.get(typeName); 892 if (tp != null) { 893 for(int i = 0; i < tp.size(); i++) { 894 setPropertyAttribute(type, (String )tp.get(i), "transient", Boolean.TRUE); 895 } 896 } 897 setBeanAttribute(type, "transient_init", Boolean.TRUE); 898 } 899 900 PersistenceDelegate pd = (PersistenceDelegate )getBeanAttribute(type, "persistenceDelegate"); 901 if (pd == null) { 902 pd = (PersistenceDelegate )internalPersistenceDelegates.get(typeName); 903 if (pd != null) { 904 return pd; 905 } 906 internalPersistenceDelegates.put(typeName, defaultPersistenceDelegate); 907 try { 908 String name = type.getName(); 909 Class c = Class.forName("java.beans." + name.replace('.', '_') 910 + "_PersistenceDelegate"); 911 pd = (PersistenceDelegate )c.newInstance(); 912 internalPersistenceDelegates.put(typeName, pd); 913 } 914 catch (ClassNotFoundException e) {} 915 catch (Exception e) { 916 System.err.println("Internal error: " + e); 917 } 918 } 919 920 return (pd != null) ? pd : defaultPersistenceDelegate; 921 } 922 923 public static BeanInfo getBeanInfo(Class type) { 926 BeanInfo info = null; 927 try { 928 info = Introspector.getBeanInfo(type); 929 } catch (Throwable e) { 930 e.printStackTrace(); 931 } 932 933 return info; 934 } 935 936 private static PropertyDescriptor getPropertyDescriptor(Class type, String propertyName) { 937 BeanInfo info = getBeanInfo(type); 938 PropertyDescriptor [] propertyDescriptors = info.getPropertyDescriptors(); 939 for(int i = 0; i < propertyDescriptors.length; i++) { 941 PropertyDescriptor 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 type, String property, String attribute, Object value) { 950 PropertyDescriptor 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 type, String attribute, Object value) { 959 getBeanInfo(type).getBeanDescriptor().setValue(attribute, value); 960 } 961 962 private static Object getBeanAttribute(Class type, String attribute) { 963 return getBeanInfo(type).getBeanDescriptor().getValue(attribute); 964 } 965 966 968 private synchronized static void registerConstructor(String typeName, 969 String [] constructor) { 970 internalPersistenceDelegates.put(typeName, 971 new DefaultPersistenceDelegate (constructor)); 972 } 973 974 private static void removeProperty(String typeName, String property) { 975 Vector tp = (Vector )transientProperties.get(typeName); 976 if (tp == null) { 977 tp = new Vector (); 978 transientProperties.put(typeName, tp); 979 } 980 tp.add(property); 981 } 982 } 983 984 | Popular Tags |