< prev index next >

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

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

@@ -504,10 +510,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 >