< prev index next >

src/java.base/share/classes/java/lang/ref/Reference.java

Print this page
@@ -30,15 +30,25 @@
  import jdk.internal.vm.annotation.IntrinsicCandidate;
  import jdk.internal.access.JavaLangRefAccess;
  import jdk.internal.access.SharedSecrets;
  import jdk.internal.ref.Cleaner;
  
+ import java.util.Objects;
+ 
  /**
   * Abstract base class for reference objects.  This class defines the
   * operations common to all reference objects.  Because reference objects are
   * implemented in close cooperation with the garbage collector, this class may
   * not be subclassed directly.
+  *
+  * <div class="preview-block">
+  *      <div class="preview-comment">
+  *          The referent must have {@linkplain Objects#hasIdentity(Object) object identity}.
+  *          When preview features are enabled, attempts to create a reference
+  *          to a {@linkplain Class#isValue value object} result in an {@link IdentityException}.
+  *      </div>
+  * </div>
   * @param <T> the type of the referent
   *
   * @author   Mark Reinhold
   * @since    1.2
   * @sealedGraph

@@ -542,10 +552,13 @@
      Reference(T referent) {
          this(referent, null);
      }
  
      Reference(T referent, ReferenceQueue<? super T> queue) {
+         if (referent != null) {
+             Objects.requireIdentity(referent);
+         }
          this.referent = referent;
          this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
      }
  
      /**
< prev index next >