I followed jdev's suggestion and (after some research) implemented my own serialization using the Externalizable interface, like this:
class Foo implements Externalizable {
/**
* DO NOT USE THIS CONSTRUCTOR! This only exists for Externalizable
*/
public Foo( ) {
id = null;
children = null;
}
@Override public void writeExternal( final ObjectOutput o ) throws IOException {
o.writeInt( id.intValue( ) );
o.writeObject( children );
}
@SuppressWarnings( "unchecked" )
@Override public void readExternal( final ObjectInput o ) throws IOException, ClassNotFoundException {
id = Integer.valueOf( o.readInt( ) );
children = (Set<Foo>) o.readObject( );
}
// rest of code as before
}
My only gripe is that it needs a public no-param constructor now, but I can live with that (this is an internal-only API at least).
Now that everything's done in the correct order, I have no issues de-serialising.