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.
+ *
+ * @returns never returns normally
+ * @throws CloneNotSupportedException always
+ *
+ * @since 11
+ */
+ @Override
+ protected Object clone() throws CloneNotSupportedException {
+ throw new CloneNotSupportedException();
+ }
+
- csr of
-
JDK-8201793 (ref) Reference object should not support cloning
- Resolved
- relates to
-
JDK-8285099 (ref) Reference object should not support cloning
- Closed