From de7dd2e82cd72745b367174232cac454e262eeae Mon Sep 17 00:00:00 2001 From: Carlos Ballesteros Date: Fri, 2 Dec 2022 09:22:18 +0100 Subject: [PATCH] Adds support for a custom SymbolProvider in NativeLibrary & Library --- CHANGES.md | 1 + src/com/sun/jna/Library.java | 5 +++ src/com/sun/jna/NativeLibrary.java | 18 ++++++++- src/com/sun/jna/SymbolProvider.java | 43 ++++++++++++++++++++++ test/com/sun/jna/DirectTest.java | 57 +++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/com/sun/jna/SymbolProvider.java diff --git a/CHANGES.md b/CHANGES.md index 0ba882e459..27f50204a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Features * [#1471](https://github.com/java-native-access/jna/pull/1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@dbwiddis](https://github.com/dbwiddis). * [#1474](https://github.com/java-native-access/jna/pull/1474): Add `c.s.j.p.win32.WbemCli#IWbemClassObject.IWbemQualifierSet`, `IWbemServices.GetObject`, `IWbemContext.SetValue` and associated methods - [@rchateauneu](https://github.com/rchateauneu). * [#1482](https://github.com/java-native-access/jna/pull/1482): Add multilingual support of `Kernel32Util.formatMessage` - [@overpathz](https://github.com/overpathz). +* [#1490](https://github.com/java-native-access/jna/pull/1490): Adds support for a custom `SymbolProvider` in `NativeLibrary` & `Library` - [@soywiz](https://github.com/soywiz). Bug Fixes --------- diff --git a/src/com/sun/jna/Library.java b/src/com/sun/jna/Library.java index ce7dcbc8d3..3b79fabde0 100644 --- a/src/com/sun/jna/Library.java +++ b/src/com/sun/jna/Library.java @@ -114,6 +114,11 @@ public interface Library { */ String OPTION_CLASSLOADER = "classloader"; + /** + * Supports a custom symbol provider for the NativeLibrary (see {@link SymbolProvider}) + */ + String OPTION_SYMBOL_PROVIDER = "symbol-provider"; + static class Handler implements InvocationHandler { static final Method OBJECT_TOSTRING; diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index d7bd902110..27eb569423 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -87,13 +87,20 @@ public class NativeLibrary implements Closeable { private static final Logger LOG = Logger.getLogger(NativeLibrary.class.getName()); - private final static Level DEBUG_LOAD_LEVEL = DEBUG_LOAD ? Level.INFO : Level.FINE; + private static final Level DEBUG_LOAD_LEVEL = DEBUG_LOAD ? Level.INFO : Level.FINE; + private static final SymbolProvider NATIVE_SYMBOL_PROVIDER = new SymbolProvider() { + @Override + public long getSymbolAddress(long handle, String name, SymbolProvider parent) { + return Native.findSymbol(handle, name); + } + }; private Cleaner.Cleanable cleanable; private long handle; private final String libraryName; private final String libraryPath; private final Map functions = new HashMap(); + private final SymbolProvider symbolProvider; final int callFlags; private String encoding; final Map options; @@ -123,6 +130,13 @@ private NativeLibrary(String libraryName, String libraryPath, long handle, Map