private static void setPersistenceDelegates(XMLEncoder encoder, Class<?>[] types) { if (types != null && types.length > 0) { PersistenceDelegate persistenceDelegate = new EnumPersistenceDelegate(); for (int i = 0; i < types.length; i++) { encoder.setPersistenceDelegate(types[i], persistenceDelegate); } } // Handle "BiGDecimal" manually (has no default constructor) encoder.setPersistenceDelegate(BigDecimal.class, new BigDecimalPersistenceDelegate()); encoder.setPersistenceDelegate(byte[].class, new ByteArrayPersistenceDelegate()); encoder.setPersistenceDelegate(UUID.class, new UUIDDelegate()); }
public static void main(String[] args) { XMLEncoder encoder = new XMLEncoder(System.out); encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate()); // WARNING: This can eat up a lot of memory Object[] obs = new Object[10000]; while (obs != null) { try { obs = new Object[obs.length + obs.length / 3]; } catch (OutOfMemoryError error) { obs = null; } } PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class); if (!(pd instanceof MyPersistenceDelegate)) throw new Error("persistence delegate has been lost"); }
private void recordCall(Object param) { StackTraceElement[] eles = (new Throwable()).getStackTrace(); int i = 0; // skip Throwable.init() while (eles[i].getClassName().equals("java.lang.Throwable")) { i++; } // skip calls from MockEncoder while (eles[i].getClassName().equals(MockEncoder.class.getName())) { i++; } // skip calls from DefaultPersistenceDelegate & PersistenceDelegate while (eles[i].getClassName().equals( DefaultPersistenceDelegate.class.getName()) || eles[i].getClassName().equals( PersistenceDelegate.class.getName())) { i++; } if (i > 2 && eles[++i].getClassName().equals( DefaultPersistenceDelegateTest.class.getName())) { CallVerificationStack.getInstance().push(param); } }
public void testGetPersistenceDelegate_Default() { Encoder enc = new Encoder(); Encoder enc2 = new Encoder(); PersistenceDelegate pd1 = enc.getPersistenceDelegate(SampleBean.class); assertTrue(pd1 instanceof DefaultPersistenceDelegate); PersistenceDelegate pd2 = enc.getPersistenceDelegate(SampleBean.class); assertTrue(pd2 instanceof DefaultPersistenceDelegate); PersistenceDelegate pd3 = enc2 .getPersistenceDelegate(MockBean4Codec.class); assertTrue(pd3 instanceof DefaultPersistenceDelegate); assertSame(pd1, pd2); assertSame(pd1, pd3); }
public void register(Class<?> type, PersistenceDelegate delegate) { synchronized (this.registry) { if (delegate != null) { this.registry.put(type, delegate); } else { this.registry.remove(type); } } }
@Override public PersistenceDelegate find(Class<?> type) { PersistenceDelegate delegate; synchronized (this.registry) { delegate = this.registry.get(type); } return (delegate != null) ? delegate : super.find(type); }
private static void test(Class<?> type, Class<? extends PersistenceDelegate> expected) { PersistenceDelegate actual = ENCODER.getPersistenceDelegate(type); if ((actual == null) && (expected != null)) { throw new Error("expected delegate is not found"); } if ((actual != null) && !actual.getClass().equals(expected)) { throw new Error("found unexpected delegate"); } }
protected void initialize(XMLEncoder encoder) { encoder.setPersistenceDelegate(Container.class, new PersistenceDelegate() { protected Expression instantiate(Object oldInstance, Encoder out) { Container container = (Container) oldInstance; Component component = container.getComponent(); return new Expression(container, component, "create", new Object[] {component}); } }); }
private static void test(Class<?> type, PersistenceDelegate pd) { Encoder encoder1 = new Encoder(); Encoder encoder2 = new XMLEncoder(System.out); PersistenceDelegate pd1 = encoder1.getPersistenceDelegate(type); PersistenceDelegate pd2 = encoder2.getPersistenceDelegate(type); encoder1.setPersistenceDelegate(type, pd); if (pd1 == encoder1.getPersistenceDelegate(type)) throw new Error("first persistence delegate is not changed"); if (pd2 != encoder2.getPersistenceDelegate(type)) throw new Error("second persistence delegate is changed"); }