Class 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 Detail

      • VirtualField

        public VirtualField()
    • Method Detail

      • find

        public static <U extends T,​T,​F> VirtualField<U,​F> 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.