Top Description Inners Fields Constructors Methods
jnr.ffi

public abstract Class LibraryLoader<T>

extends Object
Class Inheritance
Known Direct Subclasses
jnr.ffi.provider.jffi.NativeLibraryLoader
Imports
java.io.BufferedReader, .File, .FileReader, .IOException, java.lang.reflect.InvocationHandler, .Method, .Proxy, java.util.ArrayList, .Arrays, .Collection, .Collections, .EnumMap, .LinkedHashSet, .List, .Map, jnr.ffi.mapper.CompositeFunctionMapper, .CompositeTypeMapper, .DataConverter, .FromNativeConverter, .FunctionMapper, .SignatureTypeMapper, .SignatureTypeMapperAdapter, .ToNativeConverter, .TypeMapper, jnr.ffi.provider.FFIProvider, .LoadedLibrary

Loads a native library and maps it to a java interface.

Example usage

public interface LibC {
   int puts(String str);
}

LibC libc = LibraryLoader.create(LibC.class).load("c");

libc.puts("Hello, World");


Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
public static final String
private boolean
private final FunctionMapper.Builder
private final List<FunctionMapper>
private final Class<T>
private final List<String>
private final Map<LibraryOption, Object>
private final List<String>
private final TypeMapper.Builder
private final List<SignatureTypeMapper>

Constructor Summary

AccessConstructor and Description
protected
LibraryLoader(Class<T> interfaceClass)

Method Summary

Modifier and TypeMethod and Description
public LibraryLoader<T>

Returns:

The LibraryLoader instance.
convention
(CallingConvention
The calling convention.
convention
)

Sets the native function calling convention.

public static <
The library type.
T
>
LibraryLoader<T>

Returns:

A LibraryLoader instance.
create
(Class<T>
the interface that describes the native library functions
interfaceClass
)

Creates a new LibraryLoader instance.

private T
public final LibraryLoader<T>

Returns:

This LibraryLoader instance.
failImmediately
()

Turns off lazy propagation of load failures.

private static List<String>
private Collection<String>
public LibraryLoader<T>

Returns:

The LibraryLoader instance.
library
(String
The name or path of library to load.
libraryName
)

Adds a library to be loaded.

public T

Returns:

an implementation of the interface provided to create(Class) that will call the native methods.
load
(String
The name or path of library to load.
libraryName
)

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

public T

Returns:

an implementation of the interface provided to create(Class) that will call the native methods.
load
()

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

public static <
the interface type.
T
>
T

Returns:

an instance of interfaceclass that will call the native methods.
loadLibrary
(Class<T>
the interface that describes the native library interface
interfaceClass
,
Map<LibraryOption, ?>
options
libraryOptions
,
Map<String, List<String>>
a map of library names to paths that should be searched
searchPaths
,
String...
the name of the library to load
libraryNames
)

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

public static <
the interface type
T
>
T

Returns:

a new object implementing the interface and bound to the library
loadLibrary
(Class<T>
the interface to implement
interfaceClass
,
Map<LibraryOption, ?>
options
libraryOptions
,
String...
names to try when searching for the library
libraryNames
)

Same as calling loadLibrary(Class, Map, Map, String...) with an empty search path map.

protected abstract T

Returns:

an instance of interfaceClass that will call the native methods.
loadLibrary
(Class<T>
The java class that describes the functions to be mapped.
interfaceClass
,
Collection<String>
A list of libraries to load and search for symbols.
libraryNames
,
Collection<String>
The paths to search for libraries to be loaded.
searchPaths
,
Map<LibraryOption, Object>
The options to apply when loading the library.
options
,
boolean
whether to fast-fail when the library does not implement the requested functions
failImmediately
)

Implemented by FFI providers to load the actual library.

public <
The Java type.
J
>
LibraryLoader<T>

Returns:

The LibraryLoader instance.
map
(Class<? extends J>
The java type to convert to a native type.
javaType
,
ToNativeConverter<? extends J, ?>
A jnr.ffi.mapper.ToNativeConverter that will convert from the java type to a native type.
toNativeConverter
)

Adds a custom java type mapping.

public <
The Java type.
J
>
LibraryLoader<T>

Returns:

The LibraryLoader instance.
map
(Class<? extends J>
The java type to convert to a native type.
javaType
,
FromNativeConverter<? extends J, ?>
A jnr.ffi.mapper.ToNativeConverter that will convert from the native type to a java type.
fromNativeConverter
)

Adds a custom java type mapping.

public <J> LibraryLoader<T>
map(Class<? extends J> javaType, DataConverter<? extends J, ?> dataConverter)

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
map
(String
The java method name.
javaName
,
String
The native library symbol to map the java method name to.
nativeFunction
)

Adds a function name mapping to use when resolving symbols in this library.

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
mapper
(TypeMapper
The type mapper to use.
typeMapper
)

Adds a type mapper to use when resolving method parameter and result types.

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
mapper
(SignatureTypeMapper
The type mapper to use.
typeMapper
)

Adds a type mapper to use when resolving method parameter and result types.

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
mapper
(FunctionMapper
The function mapper to use.
functionMapper
)

Adds a function mapper to use when resolving symbols in this library.

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
option
(LibraryOption
The option to set.
option
,
Object
The value for the option.
value
)

Sets an option when loading libraries.

public static boolean

Returns:

true if the error was saved, false otherwise
saveError
(Map<LibraryOption, ?>
options
options
,
boolean
whether the method has error-saving enabled
methodHasSave
,
boolean
whether the method ignores errors
methodHasIgnore
)

When either the jnr.ffi.annotations.SaveError or jnr.ffi.annotations.IgnoreError annotations are used, the following matrix applies: (SL = save at library level, IM = ignore at method level, etc)

        | none |  SL  |  IL  | SL+IL|
-------------------------------------
none    | save | save | ignr | save |
SM      | save | save | save | save |
IM      | ignr | ignr | ignr | ignr |
SM + IM | save | save | save | save |
public LibraryLoader<T>

Returns:

The LibraryLoader instance.
search
(String
A directory to search.
path
)

Adds a path to search for libraries.

public LibraryLoader<T>

Returns:

The LibraryLoader instance.
searchDefault
()

Add the default library to the search order.

public final LibraryLoader<T>

Returns:

This LibraryLoader instance.
stdcall
()

Sets the calling convention of the library to the Windows stdcall calling convention

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_LIBRARYback to summary
public static final String DEFAULT_LIBRARY
failImmediatelyback to summary
private boolean failImmediately
functionMapperBuilderback to summary
private final FunctionMapper.Builder functionMapperBuilder
functionMappersback to summary
private final List<FunctionMapper> functionMappers
interfaceClassback to summary
private final Class<T> interfaceClass
libraryNamesback to summary
private final List<String> libraryNames
optionMapback to summary
private final Map<LibraryOption, Object> optionMap
searchPathsback to summary
private final List<String> searchPaths
typeMapperBuilderback to summary
private final TypeMapper.Builder typeMapperBuilder
typeMappersback to summary
private final List<SignatureTypeMapper> typeMappers

Constructor Detail

LibraryLoaderback to summary
protected LibraryLoader(Class<T> interfaceClass)

Method Detail

conventionback to summary
public LibraryLoader<T> convention(CallingConvention convention)

Sets the native function calling convention.

This is only needed on windows platforms - unless explicitly specified, all platforms assume CallingConvention#DEFAULT as the calling convention.

Parameters
convention:CallingConvention

The calling convention.

Returns:LibraryLoader<T>

The LibraryLoader instance.

createback to summary
public static <T> LibraryLoader<T> create(Class<T> interfaceClass)

Creates a new LibraryLoader instance.

Parameters
<T>
The library type.
interfaceClass:Class<T>

the interface that describes the native library functions

Returns:LibraryLoader<T>

A LibraryLoader instance.

createErrorProxyback to summary
private T createErrorProxy(final Throwable ex)
failImmediatelyback to summary
public final LibraryLoader<T> failImmediately()

Turns off lazy propagation of load failures. By default, jnr.ffi.LibraryLoader#load() will not fail immediately if any libraries cannot be loaded - instead, it will create an instance of the library interface that re-throws any load errors when invoked. Calling this method will make jnr.ffi.LibraryLoader#load() throw errors immediately.

Returns:LibraryLoader<T>

This LibraryLoader instance.

getPropertyPathsback to summary
private static List<String> getPropertyPaths(String propName)
getSearchPathsback to summary
private Collection<String> getSearchPaths()
libraryback to summary
public LibraryLoader<T> library(String libraryName)

Adds a library to be loaded. Multiple libraries can be specified using additional calls to this method, and all libraries will be searched to resolve symbols (e.g. functions, variables).

Parameters
libraryName:String

The name or path of library to load.

Returns:LibraryLoader<T>

The LibraryLoader instance.

loadback to summary
public T load(String libraryName)

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

Parameters
libraryName:String

The name or path of library to load.

Returns:T

an implementation of the interface provided to create(Class) that will call the native methods.

loadback to summary
public T load()

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

Returns:T

an implementation of the interface provided to create(Class) that will call the native methods.

loadLibraryback to summary
public static <T> T loadLibrary(Class<T> interfaceClass, Map<LibraryOption, ?> libraryOptions, Map<String, List<String>> searchPaths, String... libraryNames)

Loads a native library and links the methods defined in interfaceClass to native methods in the library.

Parameters
<T>
the interface type.
interfaceClass:Class<T>

the interface that describes the native library interface

libraryOptions:Map<LibraryOption, ?>

options

searchPaths:Map<String, List<String>>

a map of library names to paths that should be searched

libraryNames:String[]

the name of the library to load

Returns:T

an instance of interfaceclass that will call the native methods.

loadLibraryback to summary
public static <T> T loadLibrary(Class<T> interfaceClass, Map<LibraryOption, ?> libraryOptions, String... libraryNames)

Same as calling loadLibrary(Class, Map, Map, String...) with an empty search path map.

Parameters
<T>
the interface type
interfaceClass:Class<T>

the interface to implement

libraryOptions:Map<LibraryOption, ?>

options

libraryNames:String[]

names to try when searching for the library

Returns:T

a new object implementing the interface and bound to the library

See Also
loadLibrary(Class, Map, Map, String...)
loadLibraryback to summary
protected abstract T loadLibrary(Class<T> interfaceClass, Collection<String> libraryNames, Collection<String> searchPaths, Map<LibraryOption, Object> options, boolean failImmediately)

Implemented by FFI providers to load the actual library.

Parameters
interfaceClass:Class<T>

The java class that describes the functions to be mapped.

libraryNames:Collection<String>

A list of libraries to load and search for symbols.

searchPaths:Collection<String>

The paths to search for libraries to be loaded.

options:Map<LibraryOption, Object>

The options to apply when loading the library.

failImmediately:boolean

whether to fast-fail when the library does not implement the requested functions

Returns:T

an instance of interfaceClass that will call the native methods.

mapback to summary
public <J> LibraryLoader<T> map(Class<? extends J> javaType, ToNativeConverter<? extends J, ?> toNativeConverter)

Adds a custom java type mapping.

Parameters
<J>
The Java type.
javaType:Class<? extends J>

The java type to convert to a native type.

toNativeConverter:ToNativeConverter<? extends J, ?>

A jnr.ffi.mapper.ToNativeConverter that will convert from the java type to a native type.

Returns:LibraryLoader<T>

The LibraryLoader instance.

mapback to summary
public <J> LibraryLoader<T> map(Class<? extends J> javaType, FromNativeConverter<? extends J, ?> fromNativeConverter)

Adds a custom java type mapping.

Parameters
<J>
The Java type.
javaType:Class<? extends J>

The java type to convert to a native type.

fromNativeConverter:FromNativeConverter<? extends J, ?>

A jnr.ffi.mapper.ToNativeConverter that will convert from the native type to a java type.

Returns:LibraryLoader<T>

The LibraryLoader instance.

mapback to summary
public <J> LibraryLoader<T> map(Class<? extends J> javaType, DataConverter<? extends J, ?> dataConverter)
mapback to summary
public LibraryLoader<T> map(String javaName, String nativeFunction)

Adds a function name mapping to use when resolving symbols in this library.

Parameters
javaName:String

The java method name.

nativeFunction:String

The native library symbol to map the java method name to.

Returns:LibraryLoader<T>

The LibraryLoader instance.

mapperback to summary
public LibraryLoader<T> mapper(TypeMapper typeMapper)

Adds a type mapper to use when resolving method parameter and result types. Multiple type mappers can be specified by additional calls to this method, and each mapper will be tried in order until one is successful.

Parameters
typeMapper:TypeMapper

The type mapper to use.

Returns:LibraryLoader<T>

The LibraryLoader instance.

mapperback to summary
public LibraryLoader<T> mapper(SignatureTypeMapper typeMapper)

Adds a type mapper to use when resolving method parameter and result types. Multiple type mappers can be specified by additional calls to this method, and each mapper will be tried in order until one is successful.

Parameters
typeMapper:SignatureTypeMapper

The type mapper to use.

Returns:LibraryLoader<T>

The LibraryLoader instance.

mapperback to summary
public LibraryLoader<T> mapper(FunctionMapper functionMapper)

Adds a function mapper to use when resolving symbols in this library. Multiple function mappers can be specified by additional calls to this method, and each mapper will be tried in order, until one is successful.

Parameters
functionMapper:FunctionMapper

The function mapper to use.

Returns:LibraryLoader<T>

The LibraryLoader instance.

optionback to summary
public LibraryLoader<T> option(LibraryOption option, Object value)

Sets an option when loading libraries.

Parameters
option:LibraryOption

The option to set.

value:Object

The value for the option.

Returns:LibraryLoader<T>

The LibraryLoader instance.

See Also
LibraryOption
saveErrorback to summary
public static boolean saveError(Map<LibraryOption, ?> options, boolean methodHasSave, boolean methodHasIgnore)

When either the jnr.ffi.annotations.SaveError or jnr.ffi.annotations.IgnoreError annotations are used, the following matrix applies: (SL = save at library level, IM = ignore at method level, etc)

        | none |  SL  |  IL  | SL+IL|
-------------------------------------
none    | save | save | ignr | save |
SM      | save | save | save | save |
IM      | ignr | ignr | ignr | ignr |
SM + IM | save | save | save | save |
Parameters
options:Map<LibraryOption, ?>

options

methodHasSave:boolean

whether the method has error-saving enabled

methodHasIgnore:boolean

whether the method ignores errors

Returns:boolean

true if the error was saved, false otherwise

searchback to summary
public LibraryLoader<T> search(String path)

Adds a path to search for libraries. Multiple paths can be specified using multiple calls to this method, and all paths will be searched..

Parameters
path:String

A directory to search.

Returns:LibraryLoader<T>

The LibraryLoader instance.

searchDefaultback to summary
public LibraryLoader<T> searchDefault()

Add the default library to the search order. This will search all loaded libraries in the current process according to the system's implementation of dlsym(RTLD_DEFAULT).

Returns:LibraryLoader<T>

The LibraryLoader instance.

stdcallback to summary
public final LibraryLoader<T> stdcall()

Sets the calling convention of the library to the Windows stdcall calling convention

Returns:LibraryLoader<T>

This LibraryLoader instance.

jnr.ffi back to summary

pack-priv final Class LibraryLoader.DefaultLibPaths

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv static final List<String>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PATHSback to summary
pack-priv static final List<String> PATHS

Constructor Detail

DefaultLibPathsback to summary
pack-priv DefaultLibPaths()

Method Detail

addPathsFromFileback to summary
private static void addPathsFromFile(Collection<String> paths, File file)