To rename fields from an object stream, you must override the ObjectInputStream.readClassDescriptor method that returns an ObjectStreamClass .
ObjectStreamClass instances perform one of two different roles through large, different subsets of the interface. For the avoidance of doubt, this design choice should not be copied.
- Describes the fields of a serializable class running in the current JVM instance. Find these instances through
ObjectStreamClass.lookup . - Describes the fields of a serializable class represented in a particular serialized stream. These instances are returned by the implementations of
ObjectInputStream.readClassDescriptor .
In your override call, super.readClassDescriptor . This will read data from the stream. Replace the value from the stream with a new one with field names, if this is the class you are interested in.
How to create your own ObjectStreamClass ? Write a dummy instance of the classes you are interested in in ObjectOutputStream . You can do this as part of the built-in by simply storing binary data. Reading with another ObjectInputStream with readClassDescriptor overridden for readClassDescriptor descriptors.
ObjectInputStream.defaultReadObject / readFields will not make any sense outside of readObject (or the like) because they rely on the current deserialization object and not on the argument. There are other restrictions that prohibit another code from calling defaultReadObject to overwrite fields that must remain constant, be checked during copying, checked for security, or the like.
source share