Class VirtualField<T,F>

java.lang.Object
io.opentelemetry.instrumentation.api.util.VirtualField<T,F>
Type Parameters:
T - The type that will contain the new virtual field.
F - The field type that'll be added to T.

public abstract class VirtualField<T,F> extends Object
Represents a "virtual" field of type F that is added to type T in the runtime.

A virtual field has similar semantics to a weak-keys strong-values map: the value will be garbage collected when their owner instance is collected. It is discouraged to use a virtual field for keeping values that might reference their key, as it may cause memory leaks.

  • Constructor Details

    • VirtualField

      public VirtualField()
  • Method Details

    • find

      public static <U extends T, V extends F, T, F> VirtualField<U,V> find(Class<T> type, Class<F> fieldType)
      Finds a VirtualField instance for given type and fieldType.

      Conceptually this can be thought of as a map lookup to fetch a second level map given type.

      In runtime, when using the javaagent, the calls to this method are rewritten to something more performant while injecting advice into a method.

      When using this method outside of Advice method, the VirtualField should be looked up once and stored in a field to avoid repeatedly calling this method.

      Parameters:
      type - The type that will contain the new virtual field.
      fieldType - The field type that will be added to type.
    • get

      @Nullable public abstract F get(T object)
      Gets the value of this virtual field.
    • set

      public abstract void set(T object, @Nullable F fieldValue)
      Sets the new value of this virtual field.