void buildFieldDescriptors ( Field [ ] declaredFields ) { // We could find the field ourselves in the collection, but calling // reflect is easier. Optimize if needed. final Field f = ObjectStreamClass . fieldSerialPersistentFields ( this . forClass ( ) ) ; // If we could not find the emulated fields, we'll have to compute // dumpable fields from reflect fields boolean useReflectFields = f == null ; // Assume we will compute the // fields to dump based on the // reflect fields ObjectStreamField [ ] _fields = null ; if ( ! useReflectFields ) { // The user declared a collection of emulated fields. Use them. // We have to be able to fetch its value, even if it is private f . setAccessible ( true ) ; try { // static field, pass null _fields = ( ObjectStreamField [ ] ) f . get ( null ) ; } catch ( IllegalAccessException ex ) { throw new AssertionError ( ex ) ; } } else { // Compute collection of dumpable fields based on reflect fields List < ObjectStreamField > serializableFields = new ArrayList < ObjectStreamField > ( declaredFields . length ) ; // Filter, we are only interested in fields that are serializable for ( Field declaredField : declaredFields ) { int modifiers = declaredField . getModifiers ( ) ; if ( ! Modifier . isStatic ( modifiers ) && ! Modifier . isTransient ( modifiers ) ) { ObjectStreamField field = new ObjectStreamField ( declaredField . getName ( ) , declaredField . getType ( ) ) ; serializableFields . add ( field ) ; } } if ( serializableFields . size ( ) == 0 ) { _fields = NO_FIELDS ; // If no serializable fields, share the // special value so that users can test } else { _fields = serializableFields . toArray ( new ObjectStreamField [ serializableFields . size ( ) ] ) ; } } Arrays . sort ( _fields ) ; // assign offsets int primOffset = 0 , objectOffset = 0 ; for ( int i = 0 ; i < _fields . length ; i ++ ) { Class < ? > type = _fields [ i ] . getType ( ) ; if ( type . isPrimitive ( ) ) { _fields [ i ] . offset = primOffset ; primOffset += primitiveSize ( type ) ; } else { _fields [ i ] . offset = objectOffset ++ ; } } fields = _fields ; }
Builds the collection of field descriptors for the receiver