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

(ref) Reference object should not support cloning

    XMLWordPrintable

Details

    • CSR
    • Status: Closed
    • P3
    • Resolution: Approved
    • 8u42
    • core-libs
    • behavioral
    • low
    • Hide
      This change will impact subclasses of `java.lang.ref.Reference` that rely on
      `Reference::clone` to return a cloned instance. For example, a custom `Reference`
      subclass that implements `Cloneable` and overrides the `clone` method to
      call `super.clone()` will no longer return a cloned instance; instead,
      `CloneNotSupportedException` will be thrown. The custom subclass must be
      modified to replace `super.clone()` with the creation of a new instance.

      However, it should be rare for a subclass of `Reference` to implement `Cloneable`, because user code generally relies on `Reference::clone`.

      Additional note for this backport:
      There has been no issue reported by customers that their applications/libraries are impacted by this change since Java 11 when this spec change was made.
      Show
      This change will impact subclasses of `java.lang.ref.Reference` that rely on `Reference::clone` to return a cloned instance. For example, a custom `Reference` subclass that implements `Cloneable` and overrides the `clone` method to call `super.clone()` will no longer return a cloned instance; instead, `CloneNotSupportedException` will be thrown. The custom subclass must be modified to replace `super.clone()` with the creation of a new instance. However, it should be rare for a subclass of `Reference` to implement `Cloneable`, because user code generally relies on `Reference::clone`. Additional note for this backport: There has been no issue reported by customers that their applications/libraries are impacted by this change since Java 11 when this spec change was made.
    • Java API
    • SE

    Description

      Summary

      Update Reference::clone to always throw CloneNotSupportedException.

      Problem

      The semantics of cloning a reference object (any instance of java.lang.ref.Reference) are not clearly defined. In addition, it is questionable whether cloning 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 reference object may have been cleared when it reaches its reachability state. On the other hand, it may be enqueued or pending for enqueuing. A newly cloned reference object could have a referent that is unreachable if the reference object being cloned is not yet cleared. A newly cloned reference object from an enqueued reference object will never be enqueued.

      A reference object cannot be meaningfully cloned.

      Solution

      Update Reference::clone to always throw CloneNotSupportedException. To clone a reference object, user code should create a new reference object with a constructor.

      Specification

       +    /**
       +     * Throws {@link CloneNotSupportedException}. A {@code Reference} cannot be
       +     * meaningfully cloned. Construct a new {@code Reference} instead.
       +     *
       +     * @apiNote This method is defined in Java SE 8 Maintenance Release 4.
       +     *
       +     * @return  never returns normally
       +     * @throws  CloneNotSupportedException always
       +     *
       +     * @since 8
       +     */
       +    @Override
       +    protected Object clone() throws CloneNotSupportedException {
       +        throw new CloneNotSupportedException();
       +    }

      Attachments

        Issue Links

          Activity

            People

              poonam Poonam Bajaj Parhar
              mchung Mandy Chung
              Iris Clark, Kim Barrett, Mandy Chung
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: