Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8251554 JEP 401: Value Classes and Objects (Preview)
  3. JDK-8317279

Standard library implementation of value classes and objects

    XMLWordPrintable

Details

    • Sub-task
    • Resolution: Unresolved
    • P4
    • None
    • None
    • core-libs
    • None

    Description

      This task summarizes the standard library changes introduced by JEP 401.



      ### Object methods

      Most of the methods of java.lang.Object are fine as is, but their documentation may need to be updated.

      'equals', 'hashCode', and 'System.identityHashCode' rely on a revised definition of "the same object" and "distinct objects", where two value objects are "the same" if they belong to the same class and have the same field values.

      'clone' is useful for value objects if they store an identity object in a field. If not, the result should generally be == to the original. In either case, the implementation of 'Object.clone' will either throw CNSE or simply return 'this'.

      'toString' works out of the box, but the printed hash code reflects the lack of identity (as noted above).

      'wait'/'notify'/etc. work out of the box; they always throw IMSE, because it's impossible (per language/VM rules) to 'synchronize' on a value object.

      'finalize': not really relevant to value objects; the language may prevent this method being overridden by a value class



      ### Core reflection

      A new preview API method, `java.util.Objects.isValueObject`, indicates whether an object is a value object or an identity object. It always returns `false` for arrays and direct instances of the class `Object`. (We may consider a similar method as a member of class `Object`.)

      `java.lang.reflect.Modifier` adds support for the `value` flag; this is also exposed via a new `isValue` method in `java.lang.Class`.

      (Arguably, `obj.getClass().isValue()` is a good enough way to express `isValueObject(obj)`, and the latter is redundant. But value-ness is so meaningful for the object itself, nevermind its class, that a more direct query seems justified.)



      ### Deserialization

      The ObjectInputStream implementation of deserialization ensures that ACC_STRICT fields, including all instance fields of value classes, are initialized before leaking the under-construction object to user code. (This implies that cyclical references to the object from its strict fields may deserialize to 'null'.)

      For value classes, deserialization ensures that the under-construction object is "pinned" or marked "larval" in some way so that the JVM doesn't attempt to perform value-related optimizations that will lose track of field writes.



      ### java.lang.ref and WeakHashMap

      Instances of java.lang.ref.Reference throw an exception if created for a value object. Similarly, WeakHashMap rejects value objects as keys. (TBD whether this behavior is sufficient, or if we need to provide some mechanism for GC management of the identity objects referenced by a value object.)

      Attachments

        Activity

          People

            dlsmith Dan Smith
            dlsmith Dan Smith
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: