< prev index next >

src/java.base/share/classes/java/io/ObjectOutputStream.java

Print this page
@@ -125,10 +125,16 @@
   * form.  The methods of the Externalizable interface, writeExternal and
   * readExternal, are called to save and restore the objects state.  When
   * implemented by a class they can write and read their own state using all of
   * the methods of ObjectOutput and ObjectInput.  It is the responsibility of
   * the objects to handle any versioning that occurs.
+  * Value classes implementing {@link Externalizable} cannot be serialized
+  * or deserialized, the value object is immutable and the state cannot be restored.
+  * Use {@link Serializable} {@code writeReplace} to delegate to another serializable
+  * object such as a record.
+  *
+  * Value objects cannot be {@code java.io.Externalizable}.
   *
   * <p>Enum constants are serialized differently than ordinary serializable or
   * externalizable objects.  The serialized form of an enum constant consists
   * solely of its name; field values of the constant are not transmitted.  To
   * serialize an enum constant, ObjectOutputStream writes the string returned by

@@ -1444,10 +1450,13 @@
              handles.assign(unshared ? null : obj);
  
              if (desc.isRecord()) {
                  writeRecordData(obj, desc);
              } else if (desc.isExternalizable() && !desc.isProxy()) {
+                 if (desc.forClass().isValue())
+                     throw new NotSerializableException("Externalizable not valid for value class "
+                             + desc.forClass().getName());
                  writeExternalData((Externalizable) obj);
              } else {
                  writeSerialData(obj, desc);
              }
          } finally {
< prev index next >