[JEP-301] Proposal: readUnknown(String) magic deserialization method

Chris seahen123 at gmail.com
Wed Sep 27 22:26:42 UTC 2017


One of my biggest complaints about Java serialization of enums is that
there's no way to make them forward-compatible with values that may be
added in later versions, even when one of the values means "unknown". Given
that this JEP is going to overhaul enums anyway, I think it'd be a good
time to add an additional magic method for serialization:

static ? readUnknown(String)

The ObjectInputStream would call this method on an enum class when trying
to read an instance whose name wasn't the name of any value of the enum,
with that unresolved name as the parameter. A realistic example of how it
would be used:

import java.util.logging.Logger;

public enum Gender {
  UNKNOWN, MALE, FEMALE;
  private static final Logger LOG =
Logger.getLogger(Gender.class.getName());
  private static Gender readUnknown(String name) {
    LOG.warning("Unknown gender " + name);
    return UNKNOWN;
  }
}

(And yes, this enum *would* get extended with more values in later versions
-- it's a weird San Francisco thing.) Thoughts?

Sincerely,
Chris Hennick


More information about the platform-jep-discuss mailing list