...
| Code Block | ||
|---|---|---|
| ||
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = ois.readFields();
Date inDate = (Date) fields.get("date", epoch);
// Defensively copy the mutable component
date = new Date(inDate.getTime());
// Perform validation if necessary
}
|
There is no need to copy immutable subobjects. Also, avoid using the subobject's {{Wiki Markup clone()}} method because it can be overridden when the subobject's class is not final and produces only a shallow copy. The references to the subobjects themselves must be nonfinal so that defensive copying can occur. It is also inadvisable to use the {{writeUnshared()}} and {{readUnshared()}} methods as an alternative \ [[Bloch 2008|AA. References#Bloch 08]\].
Risk Assessment
Failure to defensively copy mutable components during deserialization can violate the immutability contract of an object.
...
Bibliography
...
[[API 2006AA. References#API 06] ] |
| |||
]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c6cfeb2e-9cf4-46cd-a54a-6363693b4a9f"><ac:plain-text-body><![CDATA [ [[Bloch 2008AA. References#Bloch 08]] | Item 76, Write | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="744790d2-fe0c-40ae-90b4-b4d3c48ec065"><ac:plain-text-body><![CDATA[ |
[ [Sun 2006AA. References#Sun 06] ] | Serialization Specification, A.6, Guarding Unshared Deserialized Objects ]]></ac:plain-text-body></ac:structured-macro> |
...