Summary
Expand javac
's Xlint:serial
checking to include suspicious types for instance fields.
Problem
If the serialization mechanism attempts to serialize an object that is not serializable at runtime a NotSerializableException
can result (https://docs.oracle.com/en/java/javase/17/docs/specs/serialization/output.html).
Solution
For serializable classes without serialPersistentFields
, examine the type of each non-transient instance field and issue a warning if the type of the field cannot be serialized. Primitive types, types declared to be serializable, and arrays can be serialized. While by the JLS all arrays are considered serializable, a warning is issued if the innermost component type is not serializable.
It is not necessarily erroneous that a field's type is not declared to be serializable; as a runtime invariant, the field could only point to objects that can be serialized. Another alternative, some collections are specified to be conditionally serializable based on their contents. In other cases, such fields are effectively transient if writeObject
or writeReplace
don't depend on their values.
However, It is worth some additional consideration and documentation if a serializable class has such a field.
Specification
No written specification of checking behavior.
- csr of
-
JDK-8160675 Issue lint warning for non-serializable non-transient instance fields in serializable type
- Resolved