Class NativeContext<T>

  • Type Parameters:
    T - The type of the Java peer.

    public class NativeContext<T>
    extends Object
    This class provides a way for native objects to reference their Java peers. The reserveKey() method creates a unique token that the native code can redeem, using getObjFromContext for the Java object that is its peer. Note that the token is a 31 bit integer (a positive int) so that it is relatively immune to sign extension. The internal map holds only a weak reference to the Java object. If nobody in java-land cares about the peer-pair anymore, calls to getObjFromContext will return null. While it would be possible to accomplish something similar, perhaps by passing the actual java reference to the native object, such an implementation would require the native code to manage LocalRefs.... with the distinct possibility of running out.

    !!! There should be a nanny thread cleaning out all the ref -> null

    • Constructor Detail

      • NativeContext

        public NativeContext()
    • Method Detail

      • reserveKey

        public int reserveKey()
        Reserve a token. Sometimes the object to be put into the map needs to know its own token. Pre-reserving it makes it possible to make it final.
        a unique positive int key.
      • bind

        public void bind​(int key,
                         T obj)
        Bind an object to a token.
        key - a previously reserved token
        obj - the object to be bound to the token.
      • unbind

        public void unbind​(int key)
        Remove the binding for a key Re-entrant.
        key - the key to be unbound.
      • getObjFromContext

        public T getObjFromContext​(long context)
        Get the object bound to the passed token. Returns null if no object is bound to the key. For legacy reasons, core holds these "contexts" as (void *), so they are longs.
        context - a token created by reserveKey()
        the bound object, or null if none exists.