< prev index next >

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

Print this page
@@ -30,15 +30,20 @@
  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.
+  * <p>
+  * The referent must be an {@linkplain Objects#hasIdentity(Object) identity object}.
+  * Attempts to create a reference to a value object result in an {@link IdentityException}.
   * @param <T> the type of the referent
   *
   * @author   Mark Reinhold
   * @since    1.2
   * @sealedGraph

@@ -542,10 +547,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 >