编程语言
首页 > 编程语言> > java-可序列化类中的非瞬态不可序列化实例字段

java-可序列化类中的非瞬态不可序列化实例字段

作者:互联网

考虑以下代码:

public class LIMSGrid extends ClientEventSource implements Focusable, FramingBlockWrapper {

  //cell that is curently in edit mode
  private CellCoord editingCell = null;

  //framing block info
  private FramingBlock framingBlock;   

}

现在,ClientEventSource扩展了一个实现Serializable接口的类. CellCoord和FramingBlock类是POJOS,具有大量的getter和setter方法. FindBugs在抱怨editingCell和framingBlock字段说:

This Serializable class defines a non-primitive instance field which
is neither transient, Serializable, or java.lang.Object, and does not
appear to implement the Externalizable interface or the readObject()
and writeObject() methods.  Objects of this class will not be
deserialized correctly if a non-Serializable object is stored in this
field.

好的,所以一切都很好,只是说实例字段不是“ java.lang.Object”.这完全是误导,还是我在这里缺少一些基础知识?

解决方法:

我的猜测(但这只是一个猜测)是,如果您引用java.lang.object实例,FindBugs不会触发此警告,因为它认为在这种情况下,您的类是通用容器,可以容纳任何类型的对象(如收藏).

在这种情况下,类的用户有责任确保容器中存储的对象可序列化,如果他希望容器可序列化. (就像并且只有当您将可序列化的对象存储在列表中时,ArrayList才能可序列化一样).

标签:serialization,findbugs,java
来源: https://codeday.me/bug/20191201/2077477.html