Skip to content

Commit 3bedcfb

Browse files
committed
Added static helper method to the Library interface for getting the wrapped NativeLibrary instance + added static helper method to the Native class for getting the NativeLibrary instance that a "registered" class is bound to.
1 parent 4f94c57 commit 3bedcfb

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111
* [#1593](https://github.com/java-native-access/jna/pull/1593): Add support for DragonFly BSD x86-64 - [@liweitianux](https://github.com/liweitianux).
1212
* [#1595](https://github.com/java-native-access/jna/pull/1595): Add `IsProcessorFeaturePresent` to `c.s.j.p.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
1313
* [#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).
14+
* [#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.
1415

1516
Bug Fixes
1617
---------

src/com/sun/jna/Library.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ public Object invoke(Object proxy, Method method, Object[] inArgs)
271271
}
272272
}
273273
}
274+
275+
/**
276+
* Get the {@link NativeLibrary} instance that is wrapped by the given {@link Library} interface instance.
277+
* @param library the {@link Library} interface instance, which was created by the {@link Native#load Native.load()} method
278+
* @return the wrapped {@link NativeLibrary} instance
279+
*/
280+
static NativeLibrary getNativeLibrary(final Library library) {
281+
final InvocationHandler handler = Proxy.getInvocationHandler(library);
282+
if (!(handler instanceof Handler)) {
283+
throw new IllegalArgumentException("Object is not a properly initialized Library interface instance");
284+
}
285+
return ((Handler)handler).getNativeLibrary();
286+
}
274287
}

src/com/sun/jna/Native.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,23 @@ public static void register(Class<?> cls, NativeLibrary lib) {
19181918
}
19191919
}
19201920

1921+
/**
1922+
* Get the {@link NativeLibrary} instance to which the given "registered" class is bound.
1923+
* @param cls the "registered" class, which was previously registered via the {@link Native#register register()} method
1924+
* @return the {@link NativeLibrary} instance to which the "registered" class is bound
1925+
*/
1926+
public static NativeLibrary getNativeLibrary(final Class<?> cls) {
1927+
final Class<?> mappedClass = findDirectMappedClass(cls);
1928+
synchronized(registeredClasses) {
1929+
final NativeLibrary nativeLibrary = registeredLibraries.get(mappedClass);
1930+
if (nativeLibrary == null) {
1931+
throw new IllegalArgumentException("Class " + cls.getName() + " is not currently registered");
1932+
} else {
1933+
return nativeLibrary;
1934+
}
1935+
}
1936+
}
1937+
19211938
/* Take note of options used for a given library mapping, to facilitate
19221939
* looking them up later.
19231940
*/

0 commit comments

Comments
 (0)