493 static final class SynchronizedSortedSet_PersistenceDelegate extends java_util_Collections { 494 protected Expression instantiate(Object oldInstance, Encoder out) { 495 SortedSet<?> set = new TreeSet<>((SortedSet<?>) oldInstance); 496 return new Expression(oldInstance, Collections.class, "synchronizedSortedSet", new Object[]{set}); 497 } 498 } 499 500 static final class SynchronizedMap_PersistenceDelegate extends java_util_Collections { 501 protected Expression instantiate(Object oldInstance, Encoder out) { 502 Map<?,?> map = new HashMap<>((Map<?,?>) oldInstance); 503 return new Expression(oldInstance, Collections.class, "synchronizedMap", new Object[]{map}); 504 } 505 } 506 507 static final class SynchronizedSortedMap_PersistenceDelegate extends java_util_Collections { 508 protected Expression instantiate(Object oldInstance, Encoder out) { 509 SortedMap<?,?> map = new TreeMap<>((SortedMap<?,?>) oldInstance); 510 return new Expression(oldInstance, Collections.class, "synchronizedSortedMap", new Object[]{map}); 511 } 512 } 513 514 static final class CheckedCollection_PersistenceDelegate extends java_util_Collections { 515 protected Expression instantiate(Object oldInstance, Encoder out) { 516 Object type = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedCollection.type"); 517 List<?> list = new ArrayList<>((Collection<?>) oldInstance); 518 return new Expression(oldInstance, Collections.class, "checkedCollection", new Object[]{list, type}); 519 } 520 } 521 522 static final class CheckedList_PersistenceDelegate extends java_util_Collections { 523 protected Expression instantiate(Object oldInstance, Encoder out) { 524 Object type = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedCollection.type"); 525 List<?> list = new LinkedList<>((Collection<?>) oldInstance); 526 return new Expression(oldInstance, Collections.class, "checkedList", new Object[]{list, type}); 527 } 528 } 529 530 static final class CheckedRandomAccessList_PersistenceDelegate extends java_util_Collections { 531 protected Expression instantiate(Object oldInstance, Encoder out) { 532 Object type = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedCollection.type"); 533 List<?> list = new ArrayList<>((Collection<?>) oldInstance); 534 return new Expression(oldInstance, Collections.class, "checkedList", new Object[]{list, type}); 535 } 536 } 537 538 static final class CheckedSet_PersistenceDelegate extends java_util_Collections { 539 protected Expression instantiate(Object oldInstance, Encoder out) { 540 Object type = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedCollection.type"); 541 Set<?> set = new HashSet<>((Set<?>) oldInstance); 542 return new Expression(oldInstance, Collections.class, "checkedSet", new Object[]{set, type}); 543 } 544 } 545 546 static final class CheckedSortedSet_PersistenceDelegate extends java_util_Collections { 547 protected Expression instantiate(Object oldInstance, Encoder out) { 548 Object type = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedCollection.type"); 549 SortedSet<?> set = new TreeSet<>((SortedSet<?>) oldInstance); 550 return new Expression(oldInstance, Collections.class, "checkedSortedSet", new Object[]{set, type}); 551 } 552 } 553 554 static final class CheckedMap_PersistenceDelegate extends java_util_Collections { 555 protected Expression instantiate(Object oldInstance, Encoder out) { 556 Object keyType = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedMap.keyType"); 557 Object valueType = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedMap.valueType"); 558 Map<?,?> map = new HashMap<>((Map<?,?>) oldInstance); 559 return new Expression(oldInstance, Collections.class, "checkedMap", new Object[]{map, keyType, valueType}); 560 } 561 } 562 563 static final class CheckedSortedMap_PersistenceDelegate extends java_util_Collections { 564 protected Expression instantiate(Object oldInstance, Encoder out) { 565 Object keyType = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedMap.keyType"); 566 Object valueType = MetaData.getPrivateFieldValue(oldInstance, "java.util.Collections$CheckedMap.valueType"); 567 SortedMap<?,?> map = new TreeMap<>((SortedMap<?,?>) oldInstance); 568 return new Expression(oldInstance, Collections.class, "checkedSortedMap", new Object[]{map, keyType, valueType}); 569 } 570 } 571 } 572 573 /** 574 * The persistence delegate for {@code java.util.EnumMap} classes. 575 * 576 * @author Sergey A. Malenkov 577 */ 578 static final class java_util_EnumMap_PersistenceDelegate extends PersistenceDelegate { 579 protected boolean mutatesTo(Object oldInstance, Object newInstance) { 580 return super.mutatesTo(oldInstance, newInstance) && (getType(oldInstance) == getType(newInstance)); 581 } 582 583 protected Expression instantiate(Object oldInstance, Encoder out) { 584 return new Expression(oldInstance, EnumMap.class, "new", new Object[] {getType(oldInstance)}); 585 } 586 587 private static Object getType(Object instance) { 588 return MetaData.getPrivateFieldValue(instance, "java.util.EnumMap.keyType"); 589 } 590 } 591 592 /** 593 * The persistence delegate for {@code java.util.EnumSet} classes. 594 * 595 * @author Sergey A. Malenkov 596 */ 597 static final class java_util_EnumSet_PersistenceDelegate extends PersistenceDelegate { 598 protected boolean mutatesTo(Object oldInstance, Object newInstance) { 599 return super.mutatesTo(oldInstance, newInstance) && (getType(oldInstance) == getType(newInstance)); 600 } 601 602 protected Expression instantiate(Object oldInstance, Encoder out) { 603 return new Expression(oldInstance, EnumSet.class, "noneOf", new Object[] {getType(oldInstance)}); 604 } 605 606 private static Object getType(Object instance) { 607 return MetaData.getPrivateFieldValue(instance, "java.util.EnumSet.elementType"); 608 } 609 } 610 611 // Collection 612 static class java_util_Collection_PersistenceDelegate extends DefaultPersistenceDelegate { 613 protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { 614 java.util.Collection<?> oldO = (java.util.Collection)oldInstance; 615 java.util.Collection<?> newO = (java.util.Collection)newInstance; 616 617 if (newO.size() != 0) { 618 invokeStatement(oldInstance, "clear", new Object[]{}, out); 619 } 620 for (Iterator<?> i = oldO.iterator(); i.hasNext();) { 621 invokeStatement(oldInstance, "add", new Object[]{i.next()}, out); 622 } 623 } 624 } 625 626 // List 627 static class java_util_List_PersistenceDelegate extends DefaultPersistenceDelegate { 628 protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { 1296 1297 static { 1298 1299 internalPersistenceDelegates.put("java.net.URI", 1300 new PrimitivePersistenceDelegate()); 1301 1302 // it is possible because MatteBorder is assignable from MatteBorderUIResource 1303 internalPersistenceDelegates.put("javax.swing.plaf.BorderUIResource$MatteBorderUIResource", 1304 new javax_swing_border_MatteBorder_PersistenceDelegate()); 1305 1306 // it is possible because FontUIResource is supported by java_awt_Font_PersistenceDelegate 1307 internalPersistenceDelegates.put("javax.swing.plaf.FontUIResource", 1308 new java_awt_Font_PersistenceDelegate()); 1309 1310 // it is possible because KeyStroke is supported by java_awt_AWTKeyStroke_PersistenceDelegate 1311 internalPersistenceDelegates.put("javax.swing.KeyStroke", 1312 new java_awt_AWTKeyStroke_PersistenceDelegate()); 1313 1314 internalPersistenceDelegates.put("java.sql.Date", new java_util_Date_PersistenceDelegate()); 1315 internalPersistenceDelegates.put("java.sql.Time", new java_util_Date_PersistenceDelegate()); 1316 1317 internalPersistenceDelegates.put("java.util.JumboEnumSet", new java_util_EnumSet_PersistenceDelegate()); 1318 internalPersistenceDelegates.put("java.util.RegularEnumSet", new java_util_EnumSet_PersistenceDelegate()); 1319 } 1320 1321 @SuppressWarnings("rawtypes") 1322 public static synchronized PersistenceDelegate getPersistenceDelegate(Class type) { 1323 if (type == null) { 1324 return nullPersistenceDelegate; 1325 } 1326 if (Enum.class.isAssignableFrom(type)) { 1327 return enumPersistenceDelegate; 1328 } 1329 if (null != XMLEncoder.primitiveTypeFor(type)) { 1330 return primitivePersistenceDelegate; 1331 } 1332 // The persistence delegate for arrays is non-trivial; instantiate it lazily. 1333 if (type.isArray()) { 1334 if (arrayPersistenceDelegate == null) { 1335 arrayPersistenceDelegate = new ArrayPersistenceDelegate(); 1336 } 1337 return arrayPersistenceDelegate; 1338 } | 493 static final class SynchronizedSortedSet_PersistenceDelegate extends java_util_Collections { 494 protected Expression instantiate(Object oldInstance, Encoder out) { 495 SortedSet<?> set = new TreeSet<>((SortedSet<?>) oldInstance); 496 return new Expression(oldInstance, Collections.class, "synchronizedSortedSet", new Object[]{set}); 497 } 498 } 499 500 static final class SynchronizedMap_PersistenceDelegate extends java_util_Collections { 501 protected Expression instantiate(Object oldInstance, Encoder out) { 502 Map<?,?> map = new HashMap<>((Map<?,?>) oldInstance); 503 return new Expression(oldInstance, Collections.class, "synchronizedMap", new Object[]{map}); 504 } 505 } 506 507 static final class SynchronizedSortedMap_PersistenceDelegate extends java_util_Collections { 508 protected Expression instantiate(Object oldInstance, Encoder out) { 509 SortedMap<?,?> map = new TreeMap<>((SortedMap<?,?>) oldInstance); 510 return new Expression(oldInstance, Collections.class, "synchronizedSortedMap", new Object[]{map}); 511 } 512 } 513 } 514 515 // Collection 516 static class java_util_Collection_PersistenceDelegate extends DefaultPersistenceDelegate { 517 protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { 518 java.util.Collection<?> oldO = (java.util.Collection)oldInstance; 519 java.util.Collection<?> newO = (java.util.Collection)newInstance; 520 521 if (newO.size() != 0) { 522 invokeStatement(oldInstance, "clear", new Object[]{}, out); 523 } 524 for (Iterator<?> i = oldO.iterator(); i.hasNext();) { 525 invokeStatement(oldInstance, "add", new Object[]{i.next()}, out); 526 } 527 } 528 } 529 530 // List 531 static class java_util_List_PersistenceDelegate extends DefaultPersistenceDelegate { 532 protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { 1200 1201 static { 1202 1203 internalPersistenceDelegates.put("java.net.URI", 1204 new PrimitivePersistenceDelegate()); 1205 1206 // it is possible because MatteBorder is assignable from MatteBorderUIResource 1207 internalPersistenceDelegates.put("javax.swing.plaf.BorderUIResource$MatteBorderUIResource", 1208 new javax_swing_border_MatteBorder_PersistenceDelegate()); 1209 1210 // it is possible because FontUIResource is supported by java_awt_Font_PersistenceDelegate 1211 internalPersistenceDelegates.put("javax.swing.plaf.FontUIResource", 1212 new java_awt_Font_PersistenceDelegate()); 1213 1214 // it is possible because KeyStroke is supported by java_awt_AWTKeyStroke_PersistenceDelegate 1215 internalPersistenceDelegates.put("javax.swing.KeyStroke", 1216 new java_awt_AWTKeyStroke_PersistenceDelegate()); 1217 1218 internalPersistenceDelegates.put("java.sql.Date", new java_util_Date_PersistenceDelegate()); 1219 internalPersistenceDelegates.put("java.sql.Time", new java_util_Date_PersistenceDelegate()); 1220 } 1221 1222 @SuppressWarnings("rawtypes") 1223 public static synchronized PersistenceDelegate getPersistenceDelegate(Class type) { 1224 if (type == null) { 1225 return nullPersistenceDelegate; 1226 } 1227 if (Enum.class.isAssignableFrom(type)) { 1228 return enumPersistenceDelegate; 1229 } 1230 if (null != XMLEncoder.primitiveTypeFor(type)) { 1231 return primitivePersistenceDelegate; 1232 } 1233 // The persistence delegate for arrays is non-trivial; instantiate it lazily. 1234 if (type.isArray()) { 1235 if (arrayPersistenceDelegate == null) { 1236 arrayPersistenceDelegate = new ArrayPersistenceDelegate(); 1237 } 1238 return arrayPersistenceDelegate; 1239 } |