Top Description Inners Fields
com.sun.jna

public Interface Library

Known Direct Subinterfaces
com.sun.jna.win32.StdCallLibrary
Imports
com.sun.jna.internal.ReflectionUtils, java.lang.reflect.InvocationHandler, .Method, .Proxy, java.util.HashMap, .Map, .WeakHashMap

Derive from this interface for all native library definitions. Define an instance of your library like this:

MyNativeLibrary INSTANCE = (MyNativeLibrary)
    Native.load("mylib", MyNativeLibrary.class);

By convention, method names are identical to the native names, although you can map java names to different native names by providing a FunctionMapper as a value for key OPTION_FUNCTION_MAPPER in the options map passed to the Native#load(String, Class, Map) call.

Although the names for structures and structure fields may be chosen arbitrarily, they should correspond as closely as possible to the native definitions. The same is true for parameter names.

This interface supports multiple, concurrent invocations of any library methods on the Java side. Check your library documentation for its multi-threading requirements on the native side. If a library is not safe for simultaneous multi-threaded access, consider using Native#synchronizedLibrary to prevent simultaneous multi-threaded access to the native code.

Optional fields
Interface options will be automatically propagated to structures defined within the library provided a call to Native#load(String, Class, Map) is made prior to instantiating any of those structures. One common way of ensuring this is to declare an INSTANCE field in the interface which holds the load result.

OPTIONS (an instance of Map), TYPE_MAPPER (an instance of TypeMapper), STRUCTURE_ALIGNMENT (one of the alignment types defined in Structure), and STRING_ENCODING (a String) may also be defined. If no instance of the interface has been instantiated, these fields will be used to determine customization settings for structures and methods defined within the interface.

Authors
Todd Fast, [email protected], Timothy Wall, [email protected]

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class

Field Summary

Modifier and TypeField and Description
public static final String
OPTION_ALLOW_OBJECTS

Option key for a boolean flag to allow any Java class instance as a parameter.

public static final String
OPTION_CALLING_CONVENTION

Calling convention for the entire library.

public static final String
OPTION_CLASSLOADER

Class loader to use when searching for native libraries on the resource path (classpath).

public static final String
OPTION_FUNCTION_MAPPER

Option key for a FunctionMapper for the library.

public static final String
OPTION_INVOCATION_MAPPER

Option key for an InvocationMapper for the library.

public static final String
OPTION_OPEN_FLAGS

Flags to use when opening the native library (see Native#open(String, int))

public static final String
OPTION_STRING_ENCODING

Option key for per-library String encoding.

public static final String
OPTION_STRUCTURE_ALIGNMENT

Option key for structure alignment type (Integer), which should be one of the predefined alignment types in Structure.

public static final String
OPTION_SYMBOL_PROVIDER

Supports a custom symbol provider for the NativeLibrary (see SymbolProvider)

public static final String
OPTION_TYPE_MAPPER

Option key for a TypeMapper for the library.

Field Detail

OPTION_ALLOW_OBJECTSback to summary
public static final String OPTION_ALLOW_OBJECTS

Option key for a boolean flag to allow any Java class instance as a parameter. If no type mapper is found, the object is passed as a pointer

Note

This is for use with raw JNI interactions via the JNIEnv data structure.

OPTION_CALLING_CONVENTIONback to summary
public static final String OPTION_CALLING_CONVENTION

Calling convention for the entire library.

OPTION_CLASSLOADERback to summary
public static final String OPTION_CLASSLOADER

Class loader to use when searching for native libraries on the resource path (classpath). If not provided the current thread's context class loader is used.

If extracted from the resource path (i.e. bundled in a jar file), the loaded library's lifespan will mirror that of the class loader, which means you can use the same library in isolated contexts without conflict.
OPTION_FUNCTION_MAPPERback to summary
public static final String OPTION_FUNCTION_MAPPER

Option key for a FunctionMapper for the library.

OPTION_INVOCATION_MAPPERback to summary
public static final String OPTION_INVOCATION_MAPPER

Option key for an InvocationMapper for the library.

OPTION_OPEN_FLAGSback to summary
public static final String OPTION_OPEN_FLAGS

Flags to use when opening the native library (see Native#open(String, int))

OPTION_STRING_ENCODINGback to summary
public static final String OPTION_STRING_ENCODING

Option key for per-library String encoding. This affects conversions between Java unicode and native (const char*) strings (as arguments or Structure fields).

Defaults to Native#getDefaultStringEncoding().
OPTION_STRUCTURE_ALIGNMENTback to summary
public static final String OPTION_STRUCTURE_ALIGNMENT

Option key for structure alignment type (Integer), which should be one of the predefined alignment types in Structure.

OPTION_SYMBOL_PROVIDERback to summary
public static final String OPTION_SYMBOL_PROVIDER

Supports a custom symbol provider for the NativeLibrary (see SymbolProvider)

OPTION_TYPE_MAPPERback to summary
public static final String OPTION_TYPE_MAPPER

Option key for a TypeMapper for the library.

com.sun.jna back to summary

public Class Library.Handler

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Library.Handler.FunctionInfo

FunctionInfo has to be immutable to to make the object visible to other threads fully initialized.

Field Summary

Modifier and TypeField and Description
private final Map<Method, Library.Handler.FunctionInfo>
private final Class<?>
private final InvocationMapper
private final NativeLibrary
pack-priv static final Method
pack-priv static final Method
pack-priv static final Method
private final Map<String, Object>

Constructor Summary

AccessConstructor and Description
public
Handler(String libname, Class<?> interfaceClass, Map<String, ?> options)

Method Summary

Modifier and TypeMethod and Description
public Class<?>
public String
public NativeLibrary
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.
inArgs
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

functionsback to summary
private final Map<Method, Library.Handler.FunctionInfo> functions
interfaceClassback to summary
private final Class<?> interfaceClass
invocationMapperback to summary
private final InvocationMapper invocationMapper
nativeLibraryback to summary
private final NativeLibrary nativeLibrary
OBJECT_EQUALSback to summary
pack-priv static final Method OBJECT_EQUALS
OBJECT_HASHCODEback to summary
pack-priv static final Method OBJECT_HASHCODE
OBJECT_TOSTRINGback to summary
pack-priv static final Method OBJECT_TOSTRING
optionsback to summary
private final Map<String, Object> options

Constructor Detail

Handlerback to summary
public Handler(String libname, Class<?> interfaceClass, Map<String, ?> options)

Method Detail

getInterfaceClassback to summary
public Class<?> getInterfaceClass()
getLibraryNameback to summary
public String getLibraryName()
getNativeLibraryback to summary
public NativeLibrary getNativeLibrary()
invokeback to summary
public Object invoke(Object proxy, Method method, Object[] inArgs) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

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

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method: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.

inArgs: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.

Returns:Object

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:
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.
com.sun.jna back to summary

private final Class Library.Handler.FunctionInfo

extends Object
Class Inheritance

FunctionInfo has to be immutable to to make the object visible to other threads fully initialized. This is a prerequisite for using the class in the double checked locking scenario of Handler#invoke(Object, Method, Object[])

Field Summary

Modifier and TypeField and Description
pack-priv final Function
pack-priv final InvocationHandler
pack-priv final boolean
pack-priv final Object
pack-priv final Map<String, ?>
pack-priv final Class<?>[]

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
FunctionInfo(InvocationHandler handler, Function function, Class<?>[] parameterTypes, boolean isVarArgs, Map<String, ?> options)

Field Detail

functionback to summary
pack-priv final Function function
handlerback to summary
pack-priv final InvocationHandler handler
isVarArgsback to summary
pack-priv final boolean isVarArgs
methodHandleback to summary
pack-priv final Object methodHandle
optionsback to summary
pack-priv final Map<String, ?> options
parameterTypesback to summary
pack-priv final Class<?>[] parameterTypes

Constructor Detail

FunctionInfoback to summary
pack-priv FunctionInfo(Object mh)
FunctionInfoback to summary
pack-priv FunctionInfo(InvocationHandler handler, Function function, Class<?>[] parameterTypes, boolean isVarArgs, Map<String, ?> options)