Class Library

java.lang.Object
com.kenai.jffi.Library

public final class Library extends Object
Represents a native library
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
    A handle to the current process
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Map<String,WeakReference<Library>>
    A cache of opened libraries
    private int
    Indicates whether this library has been disposed of.
    private final Foreign
    A handle to the foreign interface to keep it alive as long as this object is alive
    static final int
    All symbols in the library are made available to other libraries
    private final long
    The native dl/LoadLibrary handle
    private static final ThreadLocal<String>
    Stores the last error returned by a dlopen or dlsym call
    static final int
    Perform lazy binding.
    static final int
    Symbols in this library are not made available to other libraries
    private static final Object
    A lock used to serialize all dlopen/dlsym calls
    private final String
    The name of this Library
    static final int
    Resolve all symbols when loading the library
    private static final AtomicIntegerFieldUpdater<Library>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Library(Foreign foreign, String name, long address)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static long
    dlopen(Foreign foreign, String name, int flags)
    Internal wrapper around dlopen.
    static final Library
    getCachedInstance(String name, int flags)
    Gets a handle for the named library.
    static final Library
    Gets a handle to the default library.
    static final String
    Gets the current error string from dlopen/LoadLibrary.
    final long
    Gets the address of a symbol within the Library.
    static final Library
    openLibrary(String name, int flags)
    Gets a handle for the named library.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • cache

      private static final Map<String,WeakReference<Library>> cache
      A cache of opened libraries
    • lock

      private static final Object lock
      A lock used to serialize all dlopen/dlsym calls
    • lastError

      private static final ThreadLocal<String> lastError
      Stores the last error returned by a dlopen or dlsym call
    • LAZY

      public static final int LAZY
      Perform lazy binding. Only resolve symbols as needed
      See Also:
    • NOW

      public static final int NOW
      Resolve all symbols when loading the library
      See Also:
    • LOCAL

      public static final int LOCAL
      Symbols in this library are not made available to other libraries
      See Also:
    • GLOBAL

      public static final int GLOBAL
      All symbols in the library are made available to other libraries
      See Also:
    • handle

      private final long handle
      The native dl/LoadLibrary handle
    • name

      private final String name
      The name of this Library
    • foreign

      private final Foreign foreign
      A handle to the foreign interface to keep it alive as long as this object is alive
    • disposed

      private volatile int disposed
      Indicates whether this library has been disposed of.
    • UPDATER

      private static final AtomicIntegerFieldUpdater<Library> UPDATER
  • Constructor Details

    • Library

      private Library(Foreign foreign, String name, long address)
  • Method Details

    • dlopen

      private static long dlopen(Foreign foreign, String name, int flags)
      Internal wrapper around dlopen. If the library open fails, then this stores the native error in a thread local variable for later retrieval.
      Parameters:
      name - The name of the library to open
      flags - The flags to pass to dlopen
      Returns:
      The native handle for the opened library, or 0 if it failed to open.
    • getDefault

      public static final Library getDefault()
      Gets a handle to the default library.
      Returns:
      A Library instance representing the default library.
    • getCachedInstance

      public static final Library getCachedInstance(String name, int flags)
      Gets a handle for the named library.
      Parameters:
      name - The name or path of the library to open.
      flags - The library flags (e.g. LAZY, NOW, LOCAL, GLOBAL)
      Returns:
      A Library instance representing the named library, or null if the library could not be opened.
    • openLibrary

      public static final Library openLibrary(String name, int flags)
      Gets a handle for the named library. Note This will not cache the instance, nor will it return a cached instance. Only use when you really need a new handle for the library.
      Parameters:
      name - The name or path of the library to open.
      flags - The library flags (e.g. LAZY, NOW, LOCAL, GLOBAL)
      Returns:
      A Library instance representing the named library, or null if the library cannot be opened.
    • getSymbolAddress

      public final long getSymbolAddress(String name)
      Gets the address of a symbol within the Library.
      Parameters:
      name - The name of the symbol to locate.
      Returns:
      The address of the symbol within the current address space.
    • getLastError

      public static final String getLastError()
      Gets the current error string from dlopen/LoadLibrary.
      Returns:
      A String describing the last error.