Java:何时在序列化期间添加readObjectNoData()? [英] Java: When to add readObjectNoData() during serialization?

查看:660
本文介绍了Java:何时在序列化期间添加readObjectNoData()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Effective Java中的序列化章节。我试图理解书中的以下段落。

I am reading the serialization chapter in "Effective Java". I am trying to understand the below paragraph in the book.


如果你实现一个具有可序列化的实例字段的类
和可扩展的,你应该注意的是谨慎。如果

有不变量,如果它的实例字段

初始化为它们的默认值(整数类型为0,boolean为
false,并且null为对象引用类型),你
必须将
这个readObjectNoData方法添加到类中:

If you implement a class with instance fields that is serializable and extendable,there is a caution you should be aware of. If the class has invariants that would be violated if its instance fields were initialized to their default values (zero for integral types, false for boolean, and null for object reference types), you must add this readObjectNoData method to the class:



    // readObjectNoData for stateful extendable serializable classes
     private void readObjectNoData() throws InvalidObjectException {
             throw new InvalidObjectException("Stream data required");
     }

我不确定上述数据的含义。

I am not sure what the above statment means .

为了测试这个,我创建了一个Person类(可序列化和可扩展)

To test this, I created a class Person (both serializable and extendable)

   class Person   implements Serializable{

       private String name;

       private int age;

      Person() {
         this("default",1);
      }


     Person(String name, int y) {
       this.name = name;
        this.age = y;
      }
    }

以及扩展它的类Employee。

and a class Employee which extends it.

     class Employee extends Person  {

      String address ;


    public Employee()
    {
        super();
        address ="default_address";
    }

    public Employee(String name , int age, String address)
    {
        super(name,age);
        this.address = address;
    }
           }

我创建的Person类中是否有任何不变量?什么时候会被侵犯?我复制粘贴了Employee类中的readObjectData()方法的代码,但它从未被调用过。何时调用readObject()方法?我错过了什么吗?

Are there any invariants in the Person class I created ? When will they be violated ? I copy pasted the code for readObjectData() method in the Employee class , but it never got called. When will the method readObject() be called ? Am I missing something ?

推荐答案

readObjectNoData section
似乎很有意思(见下文)。

The readObjectNoData section in Java Object Serialization Specification seems interesting (see below).

您对问题的编辑提供了一个完美的例子。如果 Employee 序列化,当它没有扩展 Person 时稍后反序列化当它执行时, Person 部分将被初始化为空字符串和0年龄。使用此方法,您可以将它们分别初始化为name和1。

Your edits to the question give a perfect example. If Employee was serialized when it did not extend Person and later deserialized when it did then the Person part would be initialized to empty string and 0 age. Using this method, you can initialize them to "name" and 1 respectively.


对于可序列化的对象,readObjectNoData方法允许类
在$ $的情况下控制其自己字段的初始化b $ b子类实例被反序列化,并且序列化流执行
不会将所讨论的类列为反序列化的
对象的超类。如果接收方使用
不同版本的反序列化实例的类而不是
发送方,则可能会发生这种情况,并且接收方的版本扩展了由发送方版本扩展的不是
的类。如果
序列化流已被篡改,也可能发生这种情况;因此,readObjectNoData是
,用于正确初始化反序列化的对象,尽管
敌对或不完整的源流。

For serializable objects, the readObjectNoData method allows a class to control the initialization of its own fields in the event that a subclass instance is deserialized and the serialization stream does not list the class in question as a superclass of the deserialized object. This may occur in cases where the receiving party uses a different version of the deserialized instance's class than the sending party, and the receiver's version extends classes that are not extended by the sender's version. This may also occur if the serialization stream has been tampered; hence, readObjectNoData is useful for initializing deserialized objects properly despite a "hostile" or incomplete source stream.

private void readObjectNoData()throws ObjectStreamException;

每个可序列化类可以定义自己的readObjectNoData方法。如果
a可序列化的类没有定义一个readObjectNoData方法,那么在上面列出的类的字段中的
将被
初始化为它们的默认值(如4.5.5节所列)
JavaTM语言规范,第二版);当引入对readObjectNoData
方法的支持时,这种行为是

JavaTM 2 SDK标准版1.4版之前的ObjectInputStream一致。如果可序列化类确实定义了
readObjectNoData方法并且出现上述条件,则在反序列化
期间将调用
readObjectNoData,否则将调用类定义的readObject方法
有问题的类被流列为要反序列化的
实例的超类。

Each serializable class may define its own readObjectNoData method. If a serializable class does not define a readObjectNoData method, then in the circumstances listed above the fields of the class will be initialized to their default values (as listed in section 4.5.5 of The JavaTM Language Specification, Second Edition); this behavior is consistent with that of ObjectInputStream prior to version 1.4 of the JavaTM 2 SDK, Standard Edition, when support for readObjectNoData methods was introduced. If a serializable class does define a readObjectNoData method and the aforementioned conditions arise, then readObjectNoData will be invoked at the point during deserialization when a class-defined readObject method would otherwise be called had the class in question been listed by the stream as a superclass of the instance being deserialized.

这篇关于Java:何时在序列化期间添加readObjectNoData()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆