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

    • Type: CSR
    • Status: Draft
    • Priority: P4
    • Resolution: Unresolved
    • Fix Version/s: 18
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      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.
    • Interface Kind:
      add/remove/modify command line option
    • Scope:
      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 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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              darcy Joe Darcy
              Reporter:
              darcy Joe Darcy
              Reviewed By:
              Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: