diff --git a/CHANGES.md b/CHANGES.md index 0d02adc97..3823ee8d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ Features * [#1593](https://github.com/java-native-access/jna/pull/1593): Add support for DragonFly BSD x86-64 - [@liweitianux](https://github.com/liweitianux). * [#1595](https://github.com/java-native-access/jna/pull/1595): Add `IsProcessorFeaturePresent` to `c.s.j.p.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis). * [#1602](https://github.com/java-native-access/jna/pull/1602): Add `XMoveWindow`, `XResizeWindow`, `XMoveResizeWindow`, `XRaiseWindow`, `XLowerWindow` X11 calls to `c.s.j.p.unix.X11` - [@vinceh121](https://github.com/vinceh121). +* [#1612](https://github.com/java-native-access/jna/pull/1612): Added static helper methods for getting the underlying NativeLibrary instance from a Library interface instance or from a "registered" class. Bug Fixes --------- diff --git a/src/com/sun/jna/Library.java b/src/com/sun/jna/Library.java index 71745ffd7..3528dc7ca 100644 --- a/src/com/sun/jna/Library.java +++ b/src/com/sun/jna/Library.java @@ -271,4 +271,17 @@ public Object invoke(Object proxy, Method method, Object[] inArgs) } } } + + /** + * Get the {@link NativeLibrary} instance that is wrapped by the given {@link Library} interface instance. + * @param library the {@link Library} interface instance, which was created by the {@link Native#load Native.load()} method + * @return the wrapped {@link NativeLibrary} instance + */ + static NativeLibrary getNativeLibrary(final Library library) { + final InvocationHandler handler = Proxy.getInvocationHandler(library); + if (!(handler instanceof Handler)) { + throw new IllegalArgumentException("Object is not a properly initialized Library interface instance"); + } + return ((Handler)handler).getNativeLibrary(); + } } diff --git a/src/com/sun/jna/Native.java b/src/com/sun/jna/Native.java index 7edeb05c5..09a5da1f3 100644 --- a/src/com/sun/jna/Native.java +++ b/src/com/sun/jna/Native.java @@ -1918,6 +1918,23 @@ public static void register(Class cls, NativeLibrary lib) { } } + /** + * Get the {@link NativeLibrary} instance to which the given "registered" class is bound. + * @param cls the "registered" class, which was previously registered via the {@link Native#register register()} method + * @return the {@link NativeLibrary} instance to which the "registered" class is bound + */ + public static NativeLibrary getNativeLibrary(final Class cls) { + final Class mappedClass = findDirectMappedClass(cls); + synchronized(registeredClasses) { + final NativeLibrary nativeLibrary = registeredLibraries.get(mappedClass); + if (nativeLibrary == null) { + throw new IllegalArgumentException("Class " + cls.getName() + " is not currently registered"); + } else { + return nativeLibrary; + } + } + } + /* Take note of options used for a given library mapping, to facilitate * looking them up later. */