Skip to content

Commit 6388436

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

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-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 <tt>NativeLibrary</tt> instance that is wrapped by the given <tt>Library</tt> interface instance
277+
* @param library the <tt>Library</tt> interface instance, usually this was obtained via the <tt>Native.load()</tt> method
278+
* @return the wrapped <tt>NativeLibrary</tt> 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 loaded Library instance")
284+
}
285+
return ((Handler)handler).getNativeLibrary();
286+
}
274287
}

src/com/sun/jna/Native.java

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

1921+
/**
1922+
* Get the <tt>NativeLibrary</tt> instance to which the native methods of the given "registered" class are bound
1923+
* @param cls the "registered" class, usually this class was previously registered via the <tt>Native.register()</tt> method
1924+
* @return if the given class is registered, the <tt>NativeLibrary</tt> instance to which the class' native methods are bound; otherwise <tt>null</tt>
1925+
*/
1926+
public static NativeLibrary getNativeLibrary(final Class<?> cls) {
1927+
synchronized(registeredClasses) {
1928+
return registeredLibraries.get(findDirectMappedClass(cls));
1929+
}
1930+
}
1931+
19211932
/* Take note of options used for a given library mapping, to facilitate
19221933
* looking them up later.
19231934
*/

0 commit comments

Comments
 (0)