Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8274336

Issue lint warning for non-serializable non-transient instance fields in serializable type

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P4
    • 18
    • tools
    • None
    • behavioral
    • low
    • Hide
      Augmented checks could cause types which currently compile cleanly under javac's -Xlint:serial to generate warnings.

      New warnings can be suppressed using @SuppressWarnings("serial") annotations and/or disabling the lint category.
      Show
      Augmented checks could cause types which currently compile cleanly under javac's -Xlint:serial to generate warnings. New warnings can be suppressed using @SuppressWarnings("serial") annotations and/or disabling the lint category.
    • add/remove/modify command line option
    • JDK

    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 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.

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              darcy Joe Darcy
              Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: