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

(ref) Reference object should not support cloning

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      Existing Reference subclass that relies on Reference::clone to return
      a cloned instance will be impacted. For example a custom Reference
      class that implements Cloneable and overrides the clone method to
      call super.clone() will no longer return a cloned instance but instead
      CloneNotSupportedException will be thrown. Existing code has to be
      modified to replace super.clone() with creating a new instance.

      It should be rare that a subtype of Reference also implements Cloneable
      and existing code relies on Reference::clone.
      Show
      Existing Reference subclass that relies on Reference::clone to return a cloned instance will be impacted. For example a custom Reference class that implements Cloneable and overrides the clone method to call super.clone() will no longer return a cloned instance but instead CloneNotSupportedException will be thrown. Existing code has to be modified to replace super.clone() with creating a new instance. It should be rare that a subtype of Reference also implements Cloneable and existing code relies on Reference::clone.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Propose Reference::clone to throw CloneNotSupportedException

      Problem

      The semantics of cloning a Reference object is not clearly defined. In addition, it is questionable whether it should be supported due to its tight interaction with garbage collector.

      The reachability state of a Reference object may change during GC reference processing. The referent may have been cleared when it reaches its reachability state. On the other hand, it may be enqueued or pending for enqueuing. Cloning a Reference object with a referent that is unreachable but not yet cleared might mean to resurrect the referent. A cloned enqueued Reference object will never be enqueued.

      A Reference object cannot be meaningfully cloned.

      Solution

      Propose Reference::clone to always throw CloneNotSupportedException. To clone a Reference object, it can create a new reference object through its constructor.

      Specification

      +    /**
      +     * Throws {@link CloneNotSupportedException}. A {@code Reference} cannot be
      +     * meaningfully cloned. Construct a new {@code Reference} instead.
      +     *
      +     * @returns never returns normally
      +     * @throws  CloneNotSupportedException always
      +     *
      +     * @since 11
      +     */
      +    @Override
      +    protected Object clone() throws CloneNotSupportedException {
      +        throw new CloneNotSupportedException();
      +    }
      +

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mchung Mandy Chung
                Reporter:
                mchung Mandy Chung
                Reviewed By:
                Alan Bateman, Paul Sandoz (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: