Class TruffleWeakReference<T>

java.lang.Object
java.lang.ref.Reference<T>
java.lang.ref.WeakReference<T>
com.oracle.truffle.api.utilities.TruffleWeakReference<T>

public final class TruffleWeakReference<T> extends WeakReference<T>
Creates a new weak reference that is safe to be used in compiled code paths. If a weak reference is PE constant then the referenced object will be PE constant as well.

When a language is compiled using native-image then a closed world closure of all runtime compiled methods is computed. That list of methods can rely on static type information only. If the host application or any of the loaded libraries uses custom weak reference sub-classes that were not designed for partial evaluation then these method will be listed as runtime compilable methods, causing black listed method errors at native-image compilation time. To avoid this problem use this custom weak reference subclass which is designed for PE if used as exact class of the field.

Wrong usage of weak references in compiled code paths:

 class MyNode extends Node {

     @CompilationFinal private WeakReference reference;

     Object execute(Object arg) {
         if (reference == null) {
             CompilerDirectives.transferToInterpreterAndInvalidate();
             reference = new WeakReference<>(arg);
         }
         return reference.get();
     }

 }
 

 Correct usage of weak references in compiled code paths :

 
 static class MyNode extends Node {

     @CompilationFinal private TruffleWeakReference reference;

     Object execute(Object arg) {
         if (reference == null) {
             CompilerDirectives.transferToInterpreterAndInvalidate();
             reference = new TruffleWeakReference(arg);
         }
         return reference.get();
     }

 }
 
Since:
20.2
  • Constructor Details

    • TruffleWeakReference

      public TruffleWeakReference(T t)
      Creates a new Truffle weak reference that refers to the given object. The new reference is not registered with any queue.
      Since:
      20.2
      See Also:
    • TruffleWeakReference

      public TruffleWeakReference(T referent, ReferenceQueue<? super T> q)
      Creates a new Truffle weak reference that refers to the given object and is registered with the given queue.
      Since:
      20.2
      See Also: