Consider the following code :
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;
}
Now ClientEventSource extends a class that implements Serializable interface . The classes CellCoord and FramingBlock are POJOS with a bunch of getters and setters . FindBugs is complaining about the editingCell and framingBlock fields saying :
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.
Okay so everything is fine except how come it is saying that the instance fields are not “java.lang.Object” . This is totally misleading or I am missing some basics here ?
My guess (but it’s only a guess) is that FindBugs doesn’t trigger this warning if you reference
java.lang.objectinstances, because it considers that in this case, your class is a generic container, which can hold any kind of object (like a Collection).In that case, it’s the responsibility of the user of the class to make sure that the object stored in the container is serializable if he wants the container to be serializable. (just like an ArrayList is serializable if and only if you store serializable objects inside the list).