Top Description Constructors Methods
jnr.ffi

public abstract Class ObjectReferenceManager<T>

extends Object
Class Inheritance
Known Direct Subclasses
jnr.ffi.provider.DefaultObjectReferenceManager

Provides a mapping between java objects and unique native addresses.

The native addresses generated by this class do not relate to the object's true native address (since that is not supported by most java virtual machines), but is guaranteed to be unique within an ObjectReferenceManager instance.

This would be commonly used to create a unique native pointer that can be used to retrieve an object from within a callback. e.g.

      

      public interface MyLib {
        public static interface MyCallback {
          @Delegate public void call(Pointer value);
        }

        public void do_something_with_callback(MyCallback cb, Pointer cb_argument);
      }

      MyLib lib = LibraryLoader.create(MyLib.class).load("mylib");
      final ObjectReferenceManager referenceManager = Runtime.getRuntime(lib).newObjectReferenceManager();

      MyCallback cb = new MyCallback {
        public void call(Pointer cb_argument) {
          Object javaCallbackArgument = referenceManager.get(cb_argument);
          System.out.println("java callback parameter=" + javaCallbackArgument);
        }
      }

      String callbackArgument = "Hello, World";
      Pointer cb_argument = referenceManager.add(callback);
      lib.do_something_with_callback(cb, cb_argument);
      referenceManager.remove(cb_argument);

      
    
Note

Each call to add(Object) will return a unique native address, even for the same object, so each call to add(Object) must be matched with a call to remove(Pointer).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract Pointer

Returns:

A pointer representing the unique id.
add
(T
The java object to generate a reference for
object
)

Adds a mapping from a java object to a unique native address.

public void
freeReference(Pointer
a native memory pointer.
reference
)

Deprecated use remove(Pointer)
Removes a mapping from java object to native pointer.
public abstract T

Returns:

The java object corresponding to pointer.
get
(Pointer
a native memory pointer.
reference
)

Gets the java object that is mapped to the native memory address referred to by reference.

public T

Returns:

The java object corresponding to pointer.
getObject
(Pointer
a native memory pointer.
reference
)

Deprecated use get(Pointer)
Gets the java object that is mapped to the native memory address referred to by reference.
public static <T> ObjectReferenceManager<T>
public Pointer

Returns:

A pointer representing the unique id.
newReference
(T
The java object to generate a reference for
object
)

Deprecated use add(Object)
Adds a mapping from a java object to a unique native address.
public abstract boolean

Returns:

true if the mapping was removed.
remove
(Pointer
a native memory pointer.
reference
)

Removes a mapping from java object to native pointer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ObjectReferenceManagerback to summary
public ObjectReferenceManager()

Method Detail

addback to summary
public abstract Pointer add(T object)

Adds a mapping from a java object to a unique native address. Each call to this method is guaranteed to produce a memory address unique within the ObjectReferenceManager instance, even for the same object.

A strong reference to object is maintained internally, until remove(Pointer) is called.

Parameters
object:T

The java object to generate a reference for

Returns:Pointer

A pointer representing the unique id.

freeReferenceback to summary
public void freeReference(Pointer reference)

Deprecated

use remove(Pointer)

Removes a mapping from java object to native pointer.

Parameters
reference:Pointer

a native memory pointer.

Annotations
@Deprecated
getback to summary
public abstract T get(Pointer reference)

Gets the java object that is mapped to the native memory address referred to by reference.

Parameters
reference:Pointer

a native memory pointer.

Returns:T

The java object corresponding to pointer.

getObjectback to summary
public T getObject(Pointer reference)

Deprecated

use get(Pointer)

Gets the java object that is mapped to the native memory address referred to by reference.

Parameters
reference:Pointer

a native memory pointer.

Returns:T

The java object corresponding to pointer.

Annotations
@Deprecated
newInstanceback to summary
public static <T> ObjectReferenceManager<T> newInstance(Runtime runtime)
newReferenceback to summary
public Pointer newReference(T object)

Deprecated

use add(Object)

Adds a mapping from a java object to a unique native address. Each call to this method is guaranteed to produce a memory address unique within the ObjectReferenceManager instance, even for the same object.

A strong reference to object is maintained internally, until remove(Pointer) is called.

Parameters
object:T

The java object to generate a reference for

Returns:Pointer

A pointer representing the unique id.

Annotations
@Deprecated
removeback to summary
public abstract boolean remove(Pointer reference)

Removes a mapping from java object to native pointer.

Parameters
reference:Pointer

a native memory pointer.

Returns:boolean

true if the mapping was removed.