private void writeHierarchy(Object object, ObjectStreamClass classDesc)
            throws IOException, NotActiveException {
        if (object == null) {
            throw new NotActiveException();
        }

        // Fields are written from class closest to Object to leaf class
        // (down the chain)
        List<ObjectStreamClass> hierarchy = classDesc.getHierarchy();
        for (int i = 0, end = hierarchy.size(); i < end; ++i) {
            ObjectStreamClass osc = hierarchy.get(i);
            // Have to do this before calling defaultWriteObject or anything
            // that calls defaultWriteObject
            currentObject = object;
            currentClass = osc;

            // See if the object has a writeObject method. If so, run it
            try {
                boolean executed = false;
                if (osc.hasMethodWriteObject()) {
                    final Method method = osc.getMethodWriteObject();
                    try {
                        method.invoke(object, new Object[] { this });
                        executed = true;
                    } catch (InvocationTargetException e) {
                        Throwable ex = e.getTargetException();
                        if (ex instanceof RuntimeException) {
                            throw (RuntimeException) ex;
                        } else if (ex instanceof Error) {
                            throw (Error) ex;
                        }
                        throw (IOException) ex;
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e.toString());
                    }
                }

                if (executed) {
                    drain();
                    output.writeByte(TC_ENDBLOCKDATA);
                } else {
                    // If the object did not have a writeMethod, call
                    // defaultWriteObject
                    defaultWriteObject();
                }
            } finally {
                // Cleanup, needs to run always so that we can later detect
                // invalid calls to defaultWriteObject
                currentObject = null;
                currentClass = null;
                currentPutField = null;
            }
        }
    }
private void writeHierarchy ( Object object , ObjectStreamClass classDesc ) throws IOException , NotActiveException { if ( object == null ) { throw new NotActiveException ( ) ; } List < ObjectStreamClass > hierarchy = classDesc . getHierarchy ( ) ; for ( int i = 0 , end = hierarchy . size ( ) ; i < end ; ++ i ) { ObjectStreamClass osc = hierarchy . get ( i ) ; currentObject = object ; currentClass = osc ; try { boolean executed = false ; if ( osc . hasMethodWriteObject ( ) ) { final Method method = osc . getMethodWriteObject ( ) ; try { method . invoke ( object , new Object [ ] { this } ) ; executed = true ; } catch ( InvocationTargetException e ) { Throwable ex = e . getTargetException ( ) ; if ( ex instanceof RuntimeException ) { throw ( RuntimeException ) ex ; } else if ( ex instanceof Error ) { throw ( Error ) ex ; } throw ( IOException ) ex ; } catch ( IllegalAccessException e ) { throw new RuntimeException ( e . toString ( ) ) ; } } if ( executed ) { drain ( ) ; output . writeByte ( TC_ENDBLOCKDATA ) ; } else { defaultWriteObject ( ) ; } } finally { currentObject = null ; currentClass = null ; currentPutField = null ; } } }
Walks the hierarchy of classes described by class descriptor
{@code classDesc} and writes the field values corresponding to
fields declared by the corresponding class descriptor. The instance to
fetch field values from is {@code object}. If the class
(corresponding to class descriptor {@code classDesc}) defines
private instance method {@code writeObject} it will be used to
dump field values.

@param object
Instance from which to fetch field values to dump.
@param classDesc
A class descriptor (an {@code ObjectStreamClass})
defining which fields should be dumped.

@throws IOException
If an IO exception happened when writing the field values in
the hierarchy.
@throws NotActiveException
If the given object is not active

@see #defaultWriteObject
@see #writeObject(Object)