Skip to content

Added static helper methods for getting the underlying NativeLibrary instance from a Library interface instance or from a "registered" class. #1612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------
Expand Down
13 changes: 13 additions & 0 deletions src/com/sun/jna/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
17 changes: 17 additions & 0 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down