Top Description Inners Fields Constructors Methods
com.sun.jna

public Class CallbackReference

extends WeakReference<Callback>
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
com.sun.jna.internal.Cleaner, java.io.Closeable, java.lang.ref.Reference, .WeakReference, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.Arrays, .Collection, .HashMap, .HashSet, .Iterator, .LinkedList, .List, .Map, .Set, .WeakHashMap, .Collections, java.util.concurrent.ConcurrentHashMap

Provides a reference to an association between a native callback closure and a Java Callback closure.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
private static class
private class
private static class
CallbackReference.NativeFunctionHandler

Provide invocation handling for an auto-generated Java interface proxy for a native function pointer.

Field Summary

Modifier and TypeField and Description
private static final Map<Long, Reference<CallbackReference>>
pack-priv static final Map<Object, Object>
pack-priv static final Map<Callback, CallbackReference>
pack-priv int
pack-priv Pointer
pack-priv Cleaner.Cleanable
pack-priv static final Map<Callback, CallbackReference>
private static final Class<?>
private static final Map<Callback, CallbackThreadInitializer>
pack-priv Method
pack-priv static final Map<Pointer, Reference<Callback>[]>
pack-priv CallbackProxy
private static final Method
pack-priv Pointer

Constructor Summary

AccessConstructor and Description
private
CallbackReference(Callback callback, int callingConvention, boolean direct)

Method Summary

Modifier and TypeMethod and Description
private static Reference<Callback>[]
private static Method
public void
close()

Implements java.io.Closeable.close.

Free native resources associated with this callback.
private static Callback
protected void
dispose()

Deprecated
pack-priv static void
disposeAll()

Dispose of all memory allocated for callbacks.

pack-priv static Class<?>
public static Callback
getCallback(Class<?> type, Pointer p)

Return a Callback associated with the given function pointer.

private static Callback
getCallback(Class<?> type, Pointer p, boolean direct)

private Callback
private static Method
private static Method
public static Pointer
getFunctionPointer(Callback cb)

Return a Pointer to the native function address for the given callback.

private static Pointer
getFunctionPointer(Callback cb, boolean direct)

Native code may call this method with direct=true.

private static Pointer
getNativeFunctionPointer(Callback cb)

If the callback is one we generated to wrap a native function pointer, return that.

private static Pointer
getNativeString(Object value, boolean wide)

private Class<?>
public Pointer
getTrampoline()

Obtain a pointer to the native glue code for this callback.

private static Callback
private static ThreadGroup
private static boolean
isAllowableNativeType(Class<?> cls)

Returns whether the given class is supported in native code.

private void
setCallbackOptions(int options)

Set the behavioral options for this callback.

pack-priv static CallbackThreadInitializer

Returns:

The previous initializer instance (may be null)
setCallbackThreadInitializer
(Callback
The Callback instance
cb
,
CallbackThreadInitializer
The CallbackThreadInitializer - if null then the associated initializer instance is removed
initializer
)

Field Detail

allocatedMemoryback to summary
private static final Map<Long, Reference<CallbackReference>> allocatedMemory
allocationsback to summary
pack-priv static final Map<Object, Object> allocations
callbackMapback to summary
pack-priv static final Map<Callback, CallbackReference> callbackMap
callingConventionback to summary
pack-priv int callingConvention
cbstructback to summary
pack-priv Pointer cbstruct
cleanableback to summary
pack-priv Cleaner.Cleanable cleanable
directCallbackMapback to summary
pack-priv static final Map<Callback, CallbackReference> directCallbackMap
DLL_CALLBACK_CLASSback to summary
private static final Class<?> DLL_CALLBACK_CLASS
initializersback to summary
private static final Map<Callback, CallbackThreadInitializer> initializers
methodback to summary
pack-priv Method method
pointerCallbackMapback to summary
pack-priv static final Map<Pointer, Reference<Callback>[]> pointerCallbackMap
proxyback to summary
pack-priv CallbackProxy proxy
PROXY_CALLBACK_METHODback to summary
private static final Method PROXY_CALLBACK_METHOD
trampolineback to summary
pack-priv Pointer trampoline

Constructor Detail

CallbackReferenceback to summary
private CallbackReference(Callback callback, int callingConvention, boolean direct)

Method Detail

addCallbackToArrayback to summary
private static Reference<Callback>[] addCallbackToArray(Callback cb, Reference<Callback>[] array)
checkMethodback to summary
private static Method checkMethod(Method m)
closeback to summary
public void close()

Implements java.io.Closeable.close.

Free native resources associated with this callback.

createCallbackback to summary
private static Callback createCallback(Class<?> type, Pointer p)
disposeback to summary
protected void dispose()

Deprecated

Annotations
@Deprecated
disposeAllback to summary
pack-priv static void disposeAll()

Dispose of all memory allocated for callbacks.

findCallbackClassback to summary
pack-priv static Class<?> findCallbackClass(Class<?> type)
getCallbackback to summary
public static Callback getCallback(Class<?> type, Pointer p)

Return a Callback associated with the given function pointer. If the pointer refers to a Java callback trampoline, return the original Java Callback. Otherwise, return a proxy to the native function pointer.

Exceptions
IllegalStateException:
if the given pointer has already been mapped to a callback of a different type.
getCallbackback to summary
private static Callback getCallback(Class<?> type, Pointer p, boolean direct)
getCallbackback to summary
private Callback getCallback()
getCallbackMethodback to summary
private static Method getCallbackMethod(Callback callback)
getCallbackMethodback to summary
private static Method getCallbackMethod(Class<?> cls)
getFunctionPointerback to summary
public static Pointer getFunctionPointer(Callback cb)

Return a Pointer to the native function address for the given callback.

getFunctionPointerback to summary
private static Pointer getFunctionPointer(Callback cb, boolean direct)

Native code may call this method with direct=true.

getNativeFunctionPointerback to summary
private static Pointer getNativeFunctionPointer(Callback cb)

If the callback is one we generated to wrap a native function pointer, return that. Otherwise return null.

getNativeStringback to summary
private static Pointer getNativeString(Object value, boolean wide)
getNativeTypeback to summary
private Class<?> getNativeType(Class<?> cls)
getTrampolineback to summary
public Pointer getTrampoline()

Obtain a pointer to the native glue code for this callback.

getTypeAssignableCallbackback to summary
private static Callback getTypeAssignableCallback(Class<?> type, Reference<Callback>[] array)
initializeThreadback to summary
private static ThreadGroup initializeThread(Callback cb, CallbackReference.AttachOptions args)
isAllowableNativeTypeback to summary
private static boolean isAllowableNativeType(Class<?> cls)

Returns whether the given class is supported in native code. Other types (String, WString, Structure, arrays, NativeMapped, etc) are supported in the Java library.

setCallbackOptionsback to summary
private void setCallbackOptions(int options)

Set the behavioral options for this callback.

setCallbackThreadInitializerback to summary
pack-priv static CallbackThreadInitializer setCallbackThreadInitializer(Callback cb, CallbackThreadInitializer initializer)
Parameters
cb:Callback

The Callback instance

initializer:CallbackThreadInitializer

The CallbackThreadInitializer - if null then the associated initializer instance is removed

Returns:CallbackThreadInitializer

The previous initializer instance (may be null)

com.sun.jna back to summary

pack-priv Class CallbackReference.AttachOptions

extends Structure
Class Inheritance

Field Summary

Modifier and TypeField and Description
public boolean
public boolean
public static final List<String>
public String
Inherited from com.sun.jna.Structure:
ALIGN_DEFAULTALIGN_GNUCALIGN_MSVCALIGN_NONECALCULATE_SIZEfieldOrderlayoutInfo

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected List<String>
getFieldOrder()

Overrides com.sun.jna.Structure.getFieldOrder.

Returns this Structure's field names in their proper order.
Inherited from com.sun.jna.Structure:
allocateMemoryallocateMemoryautoAllocateautoReadautoReadautoWriteautoWritebusycacheTypeInfocalculateSizecalculateSizeclearconditionalAutoReadcreateFieldsOrdercreateFieldsOrdercreateFieldsOrdercreateFieldsOrderdataEqualsdataEqualsensureAllocatedequalsfieldOffsetfieldsgetAutoReadgetAutoWritegetFieldListgetFieldsgetFieldTypeInfogetFieldValuegetNativeAlignmentgetNativeSizegetNativeSizegetPointergetStringEncodinggetStructAlignmentgetTypeInfogetTypeInfogetTypeMapperhashCodenewInstancenewInstancereadreadFieldreadFieldreadingsetAlignTypesetAutoReadsetAutoSynchsetAutoWritesetFieldValuesetStringEncodingsizesizesizesortFieldstoArraytoArraytoStringtoStringupdateStructureByReferenceuseMemoryuseMemoryuseMemoryvalidatewritewriteFieldwriteFieldwriteField

Field Detail

daemonback to summary
public boolean daemon
detachback to summary
public boolean detach
FIELDSback to summary
public static final List<String> FIELDS
nameback to summary
public String name

Constructor Detail

AttachOptionsback to summary
pack-priv AttachOptions()

Method Detail

getFieldOrderback to summary
protected List<String> getFieldOrder()

Overrides com.sun.jna.Structure.getFieldOrder.

Doc from com.sun.jna.Structure.getFieldOrder.

Returns this Structure's field names in their proper order.
When defining a new Structure you shouldn't override this method, but use FieldOrder annotation to define your field order(this also works with inheritance)
If you want to do something non-standard you can override the method and define it as followed


protected List getFieldOrder() {
    return Arrays.asList(...);
}

Important Note

When deriving from an existing Structure subclass, ensure that you augment the list provided by the superclass, e.g.


protected List getFieldOrder() {
    List fields = new LinkedList(super.getFieldOrder());
    fields.addAll(Arrays.asList(...));
    return fields;
}
Field order must be explicitly indicated, since the field order as returned by Class#getFields() is not guaranteed to be predictable.
Returns:List<String>

ordered list of field names

Annotations
@Override
com.sun.jna back to summary

private final Class CallbackReference.CallbackReferenceDisposer

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private Pointer

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public synchronized void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cbstructback to summary
private Pointer cbstruct

Constructor Detail

CallbackReferenceDisposerback to summary
public CallbackReferenceDisposer(Pointer cbstruct)

Method Detail

runback to summary
public synchronized void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

com.sun.jna back to summary

private Class CallbackReference.DefaultCallbackProxy

extends Object
implements CallbackProxy
Class Inheritance
All Implemented Interfaces
com.sun.jna.CallbackProxy, com.sun.jna.Callback

Field Summary

Modifier and TypeField and Description
private final Method
private final String
private final FromNativeConverter[]
private ToNativeConverter

Constructor Summary

AccessConstructor and Description
public
DefaultCallbackProxy(Method callbackMethod, TypeMapper mapper, String encoding)

Method Summary

Modifier and TypeMethod and Description
public Object
callback(Object[] args)

Implements com.sun.jna.CallbackProxy.callback.

Called from native code.
private Object
convertArgument(Object value, Class<?> dstType)

Convert argument from its basic native type to the given Java parameter type.

private Object
public Callback
public Class<?>[]
getParameterTypes()

Implements com.sun.jna.CallbackProxy.getParameterTypes.

Returns the types of the parameters to the callback method.
public Class<?>
getReturnType()

Implements com.sun.jna.CallbackProxy.getReturnType.

Returns the type of the callback method's return value.
private Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callbackMethodback to summary
private final Method callbackMethod
encodingback to summary
private final String encoding
fromNativeback to summary
private final FromNativeConverter[] fromNative
toNativeback to summary
private ToNativeConverter toNative

Constructor Detail

DefaultCallbackProxyback to summary
public DefaultCallbackProxy(Method callbackMethod, TypeMapper mapper, String encoding)

Method Detail

callbackback to summary
public Object callback(Object[] args)

Implements com.sun.jna.CallbackProxy.callback.

Called from native code. All arguments are in an array of Object as the first argument. Converts all arguments to types required by the actual callback method signature, and converts the result back into an appropriate native type. This method must not throw exceptions.

Annotations
@Override
convertArgumentback to summary
private Object convertArgument(Object value, Class<?> dstType)

Convert argument from its basic native type to the given Java parameter type.

convertResultback to summary
private Object convertResult(Object value)
getCallbackback to summary
public Callback getCallback()
getParameterTypesback to summary
public Class<?>[] getParameterTypes()

Implements com.sun.jna.CallbackProxy.getParameterTypes.

Doc from com.sun.jna.CallbackProxy.getParameterTypes.

Returns the types of the parameters to the callback method.

Annotations
@Override
getReturnTypeback to summary
public Class<?> getReturnType()

Implements com.sun.jna.CallbackProxy.getReturnType.

Doc from com.sun.jna.CallbackProxy.getReturnType.

Returns the type of the callback method's return value.

Annotations
@Override
invokeCallbackback to summary
private Object invokeCallback(Object[] args)
com.sun.jna back to summary

private Class CallbackReference.NativeFunctionHandler

extends Object
implements InvocationHandler
Class Inheritance
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Provide invocation handling for an auto-generated Java interface proxy for a native function pointer. Cf. Library.Handler

Field Summary

Modifier and TypeField and Description
private final Function
private final Map<String, ?>

Constructor Summary

AccessConstructor and Description
public
NativeFunctionHandler(Pointer address, int callingConvention, Map<String, ?> options)

Method Summary

Modifier and TypeMethod and Description
public Pointer
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Chain invocation to the native function.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

functionback to summary
private final Function function
optionsback to summary
private final Map<String, ?> options

Constructor Detail

NativeFunctionHandlerback to summary
public NativeFunctionHandler(Pointer address, int callingConvention, Map<String, ?> options)

Method Detail

getPointerback to summary
public Pointer getPointer()
invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Chain invocation to the native function.

Parameters
proxy:Object

Doc from java.lang.reflect.InvocationHandler.invoke.

the proxy instance that the method was invoked on

method:Method

Doc from java.lang.reflect.InvocationHandler.invoke.

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

Doc from java.lang.reflect.InvocationHandler.invoke.

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

Doc from java.lang.reflect.InvocationHandler.invoke.

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
Exceptions
Throwable:

Doc from java.lang.reflect.InvocationHandler.invoke.

the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.