...
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="66e70d40-6477-422a-9685-7327c6e5f3a9"><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="962d0151-6555-4cd9-921f-2b183cfbe50e"><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> |
...