1. Without any persistence delegate
When I simply call:
[CODE]
Date date = new Date( System.currentTimeMillis() );
XMLEncoder encoder = new XMLEncoder( new FileOutputStream( "TEST" ) );
encoder.writeObject( date );
encoder.close();
[/CODE]
I get the following error messages:
[CODE]
java.lang.InstantiationException: java.sql.Date
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Date);
Continuing ...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.sun.beans.ObjectHandler.dequeueResult(ObjectHandler.java:137)
at java.beans.XMLDecoder.readObject(XMLDecoder.java:218)
at Main.main(Main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
[/CODE]
2. With default persistance delegate
When I simply call:
[CODE]
Date date = new Date( System.currentTimeMillis() );
PersistenceDelegate delegate = new DefaultPersistenceDelegate( new String[]{"time"} );
XMLEncoder encoder = new XMLEncoder( new FileOutputStream( "TEST" ) );
encoder.setPersistenceDelegate( Date.class, delegate );
encoder.writeObject( date );
encoder.close();
[/CODE]
I get the following error messages:
[CODE]
java.lang.IllegalArgumentException
Continuing ...
java.lang.IllegalArgumentException
Continuing ...
java.lang.IllegalArgumentException
Continuing ...
[/CODE]
3. With custom persistance delegate
The following code works fine:
[CODE]
Date date = new Date( System.currentTimeMillis() );
PersistenceDelegate delegate = new PersistenceDelegate()
{
protected Expression instantiate( Object oldInstance, Encoder out )
{
Date date = ( Date )oldInstance;
Long time = Long.valueOf( date.getTime() );
return new Expression( date, date.getClass(), "new", new Object[]{time} );
}
};
XMLEncoder encoder = new XMLEncoder( new FileOutputStream( "TEST" ) );
encoder.setPersistenceDelegate( Date.class, delegate );
encoder.writeObject( date );
encoder.close();
[/CODE]