protected Expression instantiate(Object oldInstance, Encoder out) { Class oldClass = (Class) oldInstance; // Due to the special handling of String instances in the Encoder // this Expression does not lead to further class resolutions. if (oldClass == String.class) return new Expression(oldClass, "", "getClass", null); // This Expression will lead to the class resolution of String.class. if (oldClass == Class.class) return new Expression(oldClass, String.class, "getClass", null); // This Expression will lead to the class resolution of Class.class. return new Expression(oldClass, Class.class, "forName", new Object[] { oldClass.getName() }); }
protected Expression instantiate(Object oldInstance, Encoder out) { Class type = oldInstance.getClass().getComponentType(); // oldInstance is expected to be an array, then // getClass().getComponentType() should lead // to its component type. assert (type != null); // Not handling primitive types in a special way here // causes that Class.forName("int") is built as an Expression // later which would cause an exception if executed. A special // handling to avoid the execution for primitive types can be // java.beans.Encoder.writeExpression() . return new Expression( oldInstance, Array.class, "newInstance", new Object[] { type, new Integer(Array.getLength(oldInstance)) }); }
@Override protected Expression instantiate( Object oldInstance, Encoder out ) { Class type = oldInstance.getClass(); if ( !Modifier.isPublic( type.getModifiers() ) ) throw new IllegalArgumentException( "Could not instantiate instance of non-public class: " + oldInstance ); for ( Field field : type.getFields() ) { int mod = field.getModifiers(); if ( Modifier.isPublic( mod ) && Modifier.isStatic( mod ) && Modifier.isFinal( mod ) && ( type == field.getDeclaringClass() ) ) { try { if ( oldInstance == field.get( null ) ) return new Expression( oldInstance, field, "get", new Object[]{null} ); } catch ( IllegalAccessException exception ) { throw new IllegalArgumentException( "Could not get value of the field: " + field, exception ); } } } throw new IllegalArgumentException( "Could not instantiate value: " + oldInstance ); }
@Override protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) { // p("image painter delegate called"); super.initialize(type, oldInstance, newInstance, out); //p("old instance = " + oldInstance); //p("owner = " + ((XMLEncoder)out).getOwner()); PersistenceOwner owner = (PersistenceOwner)((XMLEncoder)out).getOwner(); ImagePainter ip = (ImagePainter)oldInstance; // p("need to convert string: " + ip.getImageString()); // String s = owner.toXMLURL(ip.getImageString()); // p("converted to: " + s); //out.writeExpression(new Expression(oldInstance,owner,"fromXMLURL",new Object[]{ip.getImageString()})); //out.writeStatement(new Statement(owner,"fromXMLURL",new Object[]{ip.getImageString()})); //out.writeStatement(new Statement(oldInstance,"setImageString",new Object[]{ //new Expression(oldInstance,owner,"fromXMLURL",new Object[]{ip.getImageString()}) //})); out.writeStatement(new Statement(oldInstance,"setResolver",new Object[]{owner})); // out.writeStatement(new Statement(oldInstance,"setImageString",new Object[]{s})); }
public void test_initialize() { MockBean3 bean1 = new MockBean3(); bean1.setValue("bean1"); MockBean3 bean2 = new MockBean3(); bean2.setValue("bean2"); // clear flags bean1.setValueCalled = false; bean2.setValueCalled = false; MockPersistenceDelegate mockPersistenceDelegate = new MockPersistenceDelegate(); mockPersistenceDelegate.initialize(MockBean3.class, bean1, bean2, new Encoder()); assertEquals("bean1", bean1.getValue()); assertEquals("bean2", bean2.getValue()); assertFalse(bean1.setValueCalled); assertFalse(bean2.setValueCalled); }
public void testInitialize_NullClass() { MockPersistenceDelegate pd = new MockPersistenceDelegate(); Encoder enc = new Encoder(); Object o1 = new Object(); Object o2 = new Object(); // enc.setPersistenceDelegate(MockFooStop.class, // new MockPersistenceDelegate()); try { enc.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { CallVerificationStack.getInstance().push(e); } }); pd.initialize(null, o1, o2, enc); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } assertTrue(CallVerificationStack.getInstance().empty()); }
public void testInitialize_NullInstances() { MockPersistenceDelegate pd = new MockPersistenceDelegate(); Encoder enc = new Encoder(); MockFoo b = new MockFoo(); b.setName("myName"); // enc.setPersistenceDelegate(MockFooStop.class, // new MockPersistenceDelegate()); enc.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { CallVerificationStack.getInstance().push(e); } }); try { pd.initialize(MockFoo.class, null, b, enc); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } assertTrue(CallVerificationStack.getInstance().empty()); pd.initialize(MockFoo.class, b, null, enc); assertFalse(CallVerificationStack.getInstance().empty()); }
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); }
@Override protected Expression instantiate(Object oldInstance, Encoder out) { byte[] e = (byte[]) oldInstance; return new Expression(e, ByteArrayPersistenceDelegate.class, "decode", new Object[] { ByteArrayPersistenceDelegate.encode(e) }); }
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}); } }); }
protected void initialize(XMLEncoder encoder) { encoder.setPersistenceDelegate( OuterClass.InnerClass.class, new DefaultPersistenceDelegate() { protected Expression instantiate(Object oldInstance, Encoder out) { OuterClass.InnerClass inner = (OuterClass.InnerClass) oldInstance; OuterClass outer = inner.getOuter(); return new Expression(inner, outer, "getInner", new Object[0]); } } ); }
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { super.initialize(type, oldInstance, newInstance, out); Public<String, String> map = (Public) oldInstance; for (Entry<String, String> entry : map.getAll()) { String[] args = {entry.getKey(), entry.getValue()}; out.writeStatement(new Statement(oldInstance, "put", args)); } }
protected void initialize(XMLEncoder encoder) { encoder.setPersistenceDelegate(C.class, new DefaultPersistenceDelegate() { protected Expression instantiate(Object oldInstance, Encoder out) { C c = (C) oldInstance; return new Expression(c, c.getX(), "createC", new Object[] {}); } }); }
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"); }
private static void testDefaultPersistenceDelegate() { Encoder encoder = new Encoder(); String[] array = { "array" }; MyDPD dpd = new MyDPD(array); dpd.instantiate(dpd, encoder); array[0] = null; dpd.instantiate(dpd, encoder); }
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { java.util.Collection oldO = (java.util.Collection) oldInstance; java.util.Collection newO = (java.util.Collection) newInstance; if (newO.size() != 0) { out.writeStatement(new Statement(oldInstance, "clear", new Object[] {})); } for (Iterator i = oldO.iterator(); i.hasNext();) { out.writeStatement(new Statement(oldInstance, "add", new Object[] {i.next()})); } }
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) { Timestamp ts = (Timestamp)oldInstance; Object[] args = { ts.getNanos() }; Statement stmt = new Statement(oldInstance, "setNanos", args); out.writeStatement(stmt); }
@Override protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) { Iterator ite = ((Collection) oldInstance).iterator(); while (ite.hasNext()) { out.writeStatement(new Statement(oldInstance, "add", new Object[] {ite.next()})); } }