public void writeObject ( Object oldInstance , Encoder out ) { Object newInstance = out . get ( oldInstance ) ; Class < ? > clazz = oldInstance . getClass ( ) ; if ( mutatesTo ( oldInstance , newInstance ) ) { initialize ( clazz , oldInstance , newInstance , out ) ; } else { out . remove ( oldInstance ) ; out . writeExpression ( instantiate ( oldInstance , out ) ) ; newInstance = out . get ( oldInstance ) ; if ( newInstance != null ) { initialize ( clazz , oldInstance , newInstance , out ) ; } } }
Writes a bean object to the given encoder . First it is checked whether the simulating new object can be mutated to the old instance . If yes it is initialized to produce a series of expressions and statements that can be used to restore the old instance . Otherwise remove the new object in the simulating new environment and writes an expression that can instantiate a new instance of the same type as the old one to the given encoder .