《Effective Java》學習筆記 —— 序列化
74條 謹慎地實現Serializable接口
* 實現Serializable接口最大的代價是,一旦一個類被發布,就大大降低了“改變這個類的實現”的靈活性。
* 實現Serializable接口的第二個代價是,它增加了出現Bug和安全漏洞的可能性。
* 實現Serializable接口的第三個代價是,隨著新版本的發布,相關的測試負擔也增加了。
* 內部類(inner class)不應該實現Serializable接口,內部類的默認序列化形式是定義不清楚的。而靜態成員類(static member class)可以實現。
第75條 考慮使用自定義的序列化形式
* 如果所有的實例域都是瞬時的(transient),從技術角度而言,不調用 defaultWriteObject 和 defaultReadObject 也是允許的,但不推薦這麽做。
* 被標記為transient的域,如果是對象引用域,則被初始化為null;如果是數值基本域,則為0;如果是boolean域,則為false。如果這些初始值不能被任何transient域接受,則需要提供一個readObject方法,先調用defaultReadObject,然後再將其恢復為可接受的值。
* 聲明一個顯式的序列化版本id。
第76條 保護性的編寫 readObject 方法
* readObject方法實際上如同一個公有的構造器,如同其他構造器一樣,也要註意同樣的所有註意事項。
* 編寫健壯的readO方法:
-- 對於對象引用域必須保持為私有的類,要保護性的拷貝這些域中的每一個對象。
-- 對於任何約束條件,如果檢查失敗,則拋出一個InvalidObjectException異常。這些檢查動作應該在跟在保護性拷貝之後。
-- 如果整個對象圖在被反序列化之後必須進行驗證,就應該使用ObjectInputValidation接口。
-- 無論是直接方式還是間接方式,都不要調用類中任何可被覆蓋的方法。
第78條 對於實例控制,枚舉類型優先於readResolve
* 如果以下類實現Serializable 接口,那它將不再是單例:
1 public class Singleon {
2 private static final Singleon INSTANCE = new Singleon();
3
4 private Singleon() { }
5
6 // other codes
7 }
// 實現序列化接口後將不再是一個單例
public class Singleon implements Serializable {
private static final Singleon INSTANCE = new Singleon();
private Singleon() { }
// other codes
}
readResolve特性允許通過readObject創建的實例代替另一個實例。對於一個正在被反序列化的對象,如果它的類定義了一個readResolve方法,並且具備正確的聲明,那麽在反序列化之後,新建對象上的readResolve方法就會被調用。然後該方法返回的引用將被返回,取代新建的對象。
如果上述的Singleon類實現了下面的方法,就足以保證它的單例屬性:
1 private Object readResolve() {
2 return INSTANCE;
3 }
事實上,如果依賴readResolve進行實例控制,帶有對象引用類型的所有實例域都必須聲明為transient。否則,攻擊者就有可能在readResolve方法被運行之前,保護指向反序列化的對象的引用。
* 將一個可序列化的實例受控的類編寫成枚舉,則可以絕對保證除了聲明的常量之外,不會有別的實例。https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
https://baobao.baidu.com/article/ccd93806c747710850bf0888cdfdce38.html
《Effective Java》學習筆記 —— 序列化