【正文】
11/4 Institute of Computer Software Nanjing University 48 Onthefly object conversion ? Three separate issues: ?Detection: ?Catch object mismatch ?Notification: ?Make retrieving system aware of object mismatch ?Correction: ?Bring mismatched object to a consistent state ?Make it a correct instance of the new class version 2020/11/4 Institute of Computer Software Nanjing University 49 Detection ? Detect a mismatch between two versions of an object?s generating class ? Two categories of detection policy: ? Nominal approach: ? Each class version has a version name ? Central registration mechanism necessary ? Structural approach: ? Deduce class descriptor from actual class structure ? Store class descriptor ? Simple detection: pare class descriptors of retrieved object with new class descriptor 2020/11/4 Institute of Computer Software Nanjing University 50 Detection: Structural Approach ? What does the class descriptor need to contain? ? Tradeoff between efficiency and reliability ? Two extreme approaches: ? C1: class name ? C2: entire class text (. abstract syntax tree) ? Reasonable approaches: ? C3: class name, list of attributes (name and type) ? C4: in addition to C3: class invariant 2020/11/4 Institute of Computer Software Nanjing University 51 Notification ? What happens when the detection mechanism has caught an object mismatch? ? Language level mechanism ? Class ANY could include a procedure: correct_mismatch is Handle object retrieval mismatch. local exception: EXCEPTIONS do create exception (??[ Routine failure: Object mismatch during retrieval ]??) end 2020/11/4 Institute of Computer Software Nanjing University 52 Correction ? How do we correct an object that caused a mismatch? ? Current situation: ?Retrieval mechanism has created a new object (deduced from a stored object with same generating class) ?A mismatch has been detected ? new object is in temporary (maybe inconsistent) state 2020/11/4 Institute of Computer Software Nanjing University 53 Correction ?增加 attribute ?刪除 attribute 2020/11/4 Institute of Computer Software Nanjing University 54 Attribute was not in stored version. Field is initialized to default value of attribute type Stored version had a field. New version has removed attribute. Attributes have not changed. Correction correct_mismatch is Handle object retrieval mismatch by correctly setting up balance. do balance := ensure consistent: balance = end 2020/11/4 Institute of Computer Software Nanjing University 55 deposits withdrawals balance 900 100 200 240 300 new field (initialized to default value) old fields Wrong! 維護(hù)不變式 自動(dòng)對(duì)象轉(zhuǎn)換: Java ? serialVersionUID ?自動(dòng)定義 (根據(jù)類文件生成) ? 1. Class name 2. The class modifiers 3. The name of each interface 4. For each field of the class (except private static and private transient fields): ? The name of the field ? The modifiers of the field ? The descriptor of the field ? 5. For each method including constructors, except private methods and constructors: ? The name of the method ? The modifiers of the method ? The descriptor of the method 2020/11/4 Institute of Computer Software Nanjing University 56 自動(dòng)對(duì)象轉(zhuǎn)換: Java ?手工指定 ?ANYACCESSMODIFIER static final long serialVersionUID = 42L。 ?可序列化類的變化越大,它就越需要測(cè)試。 Java Object Serialization Example ? Reading from an object stream 2020/11/4 Institute of Computer Software Nanjing University 10 // Deserialize a string and date from a file. FileInputStream in = new FileInputStream(tmp)。 InputStream o = ()。 OutputStream o = ()。 Date d = null。 (Today)。 ?代價(jià) 2:增加了錯(cuò)誤和安全漏洞的可能性。 2020/11/4 Institute of Computer Software Nanjing University 32 private static final long serialVersionID = randomLongValue Effective Java for Serialization ? 3. Write readObject methods defensively 保護(hù)性地編寫(xiě) readObject方法 ?readObject方法實(shí)際上相當(dāng)于另一個(gè)共有的構(gòu)造函數(shù),如同其他構(gòu)造函數(shù)一樣,它也要求所有同樣的注意事項(xiàng): 檢查實(shí)參的有效性 ,并且 必要時(shí)對(duì)參數(shù)進(jìn)行保護(hù)性拷貝 。 根據(jù)用戶的輸入組裝查詢 SQL 語(yǔ)句。} public void setId(Integer id) { = id。 子類屬性非常多,且對(duì)性能要求不是很嚴(yán)格時(shí),優(yōu)先考慮 [方案 3]。 import 。而關(guān)系數(shù)據(jù)庫(kù)不支持繼承的概念。 ?它要消耗過(guò)多的空間。 public void readExternal(ObjectInput in) throws IOException, 。 } catch (Exception e) { (())。 } catch (Exception e) { (())。 ()。 (d)。 ? A Serializable class must do the following: ?Implement the interface ?Identify the fields that should be serializable ?Have access to the noarg constructor of its first nonserializable superclass 2020/11/4 Institute of Computer Software Nanjing University 14 The Serializable Interface ? The class can optionally define the following methods: ?writeObject (ObjectOutputStream) ?readObject (ObjectInputStream) ?writeReplace () ?readResolve () 2020/11/4 Institute of Computer Software Nanjing University 15 思考:如果一個(gè)可序列化的類實(shí)現(xiàn)了以上四個(gè)方法,那么在序列化和反序列化的過(guò)程中,這幾個(gè)方法的調(diào)用次序如何? The Externalizable Interface public interface Externalizable extends Serializable { public void writeExternal(ObjectOutput out) throws IOException。 2020/11/4 Institute of Computer Software Nanjing University 26 Effective Java for Serialization 2020/11/4 Institute of Computer Software Nanjing University 27 Effective Java for Serialization 2020/11/4 Institute of Computer Software Nanjing University 28 Effective Java for Serialization ? 當(dāng)一個(gè)對(duì)象的物理表示與它的邏輯數(shù)據(jù)內(nèi)容有實(shí)質(zhì)性的區(qū)