private static Map<String, Object> cacheOptions(Class<?> cls, Map<String, ?> options, Object proxy) { Map<String, Object> libOptions = new HashMap<String, Object>(options); libOptions.put(_OPTION_ENCLOSING_LIBRARY, cls); typeOptions.put(cls, libOptions); if (proxy != null) { libraries.put(cls, new WeakReference<Object>(proxy)); } // If it's a direct mapping, AND implements a Library interface, // cache the library interface as well, so that any nested // classes get the appropriate associated options if (!cls.isInterface() && Library.class.isAssignableFrom(cls)) { Class<?> ifaces[] = cls.getInterfaces(); for (Class<?> ifc : ifaces) { if (Library.class.isAssignableFrom(ifc)) { cacheOptions(ifc, libOptions, proxy); break; } } } return libOptions; }
/** * Provided for improved compatibility between JNA 4.X and 5.X * * @see Native#load(java.lang.String, java.lang.Class, java.util.Map) */ @Deprecated public static <T> T loadLibrary(String name, Class<T> interfaceClass, Map<String, ?> options) { if (!Library.class.isAssignableFrom(interfaceClass)) { // Maybe still possible if the caller is not using generics? throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")" + " of library=" + name + " does not extend " + Library.class.getSimpleName()); } Library.Handler handler = new Library.Handler(name, interfaceClass, options); ClassLoader loader = interfaceClass.getClassLoader(); Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler); cacheOptions(interfaceClass, options, proxy); return interfaceClass.cast(proxy); }
/** Load a library interface from the given shared library, providing * the explicit interface class and a map of options for the library. * If no library options are detected the map is interpreted as a map * of Java method names to native function names.<p> * If <code>name</code> is null, attempts to map onto the current process. * Native libraries loaded via this method may be found in * <a href="NativeLibrary.html#library_search_paths">several locations</a>. * @param <T> Type of expected wrapper * @param name Library base name * @param interfaceClass The implementation wrapper interface * @param options Map of library options * @return an instance of the requested interface, mapped to the indicated * native library. * @throws UnsatisfiedLinkError if the library cannot be found or * dependent libraries are missing. */ public static <T extends Library> T load(String name, Class<T> interfaceClass, Map<String, ?> options) { if (!Library.class.isAssignableFrom(interfaceClass)) { // Maybe still possible if the caller is not using generics? throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")" + " of library=" + name + " does not extend " + Library.class.getSimpleName()); } Library.Handler handler = new Library.Handler(name, interfaceClass, options); ClassLoader loader = interfaceClass.getClassLoader(); Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler); cacheOptions(interfaceClass, options, proxy); return interfaceClass.cast(proxy); }
libraryOptions.put(Library.OPTION_STRING_ENCODING, lookupField(mappingClass, "STRING_ENCODING", String.class)); libraryOptions = cacheOptions(mappingClass, libraryOptions, null);
TypeMapper mapper = (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER); boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS)); options = cacheOptions(cls, options, null);
private static Map<String, Object> cacheOptions(Class<?> cls, Map<String, ?> options, Object proxy) { Map<String, Object> libOptions = new HashMap<String, Object>(options); libOptions.put(_OPTION_ENCLOSING_LIBRARY, cls); synchronized(libraries) { typeOptions.put(cls, libOptions); if (proxy != null) { libraries.put(cls, new WeakReference<Object>(proxy)); } // If it's a direct mapping, AND implements a Library interface, // cache the library interface as well, so that any nested // classes get the appropriate associated options if (!cls.isInterface() && Library.class.isAssignableFrom(cls)) { Class<?> ifaces[] = cls.getInterfaces(); for (Class<?> ifc : ifaces) { if (Library.class.isAssignableFrom(ifc)) { cacheOptions(ifc, libOptions, proxy); break; } } } } return libOptions; }
/** Load a library interface from the given shared library, providing * the explicit interface class and a map of options for the library. * If no library options are detected the map is interpreted as a map * of Java method names to native function names.<p> * If <code>name</code> is null, attempts to map onto the current process. * Native libraries loaded via this method may be found in * <a href="NativeLibrary.html#library_search_paths">several locations</a>. * @param <T> Type of expected wrapper * @param name Library base name * @param interfaceClass The implementation wrapper interface * @param options Map of library options * @return an instance of the requested interface, mapped to the indicated * native library. * @throws UnsatisfiedLinkError if the library cannot be found or * dependent libraries are missing. */ public static <T> T loadLibrary(String name, Class<T> interfaceClass, Map<String, ?> options) { if (!Library.class.isAssignableFrom(interfaceClass)) { throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")" + " of library=" + name + " does not extend " + Library.class.getSimpleName()); } Library.Handler handler = new Library.Handler(name, interfaceClass, options); ClassLoader loader = interfaceClass.getClassLoader(); Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler); cacheOptions(interfaceClass, options, proxy); return interfaceClass.cast(proxy); }
libraryOptions.put(Library.OPTION_STRING_ENCODING, lookupField(mappingClass, "STRING_ENCODING", String.class)); libraryOptions = cacheOptions(mappingClass, libraryOptions, null);
TypeMapper mapper = (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER); boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS)); options = cacheOptions(cls, options, null);