JDK-8274336 : Issue lint warning for non-serializable non-transient instance fields in serializable type
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Draft
  • Resolution: Unresolved
  • Fix Versions: 18
  • Submitted: 2021-09-27
  • Updated: 2021-10-14
Related Reports
CSR :  
Description
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 issue if the innermost component type is not serializable.

 It is not necessarily erroneous that the 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.