Adds support for a custom SymbolProvider in NativeLibrary & Library#1490
Conversation
d3198b2 to
fcd4408
Compare
|
@matthiasblaesing can this be considered? is there something else I can do to move this forward? |
|
@soywiz I am/was busy with other things. I'll look into it. |
|
Sure. No problem and no rush. Just wanted to know if I missed something. Thanks for letting me know! |
There was a problem hiding this comment.
Looks ok in general to me. What I noticed is:
- This change also affects the
InvocationHandlercodepath, so this also needs to have a test for the non-direct invocation case. - The naming of the third parameter in
SymbolProvider#getSymbolAddressmakes me wonder ifparentis really right here. Though I have not really reached a good conclusion - Please recheck the commit and ensure, that your real name is in the author field (
git log -n 1is your friend)
For the change in NativeLibrary - what do you think about this:
--- a/src/com/sun/jna/NativeLibrary.java
+++ b/src/com/sun/jna/NativeLibrary.java
@@ -87,17 +87,22 @@
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<String, Function> functions = new HashMap<String, Function>();
+ private final SymbolProvider symbolProvider;
final int callFlags;
private String encoding;
- private SymbolProvider symbolProvider;
- private SymbolProvider defaultSymbolProvider;
final Map<String, ?> options;
private static final Map<String, Reference<NativeLibrary>> libraries = new HashMap<String, Reference<NativeLibrary>>();
@@ -125,15 +130,11 @@
this.callFlags = callingConvention;
this.options = options;
this.encoding = (String)options.get(Library.OPTION_STRING_ENCODING);
- this.symbolProvider = (SymbolProvider)options.get(Library.OPTION_SYMBOL_PROVIDER);
- if (this.symbolProvider != null) {
- //noinspection Convert2Lambda
- defaultSymbolProvider = new SymbolProvider() {
- @Override
- public long getSymbolAddress(long handle, String name, SymbolProvider parent) {
- return Native.findSymbol(handle, name);
- }
- };
+ SymbolProvider optionSymbolProvider = (SymbolProvider)options.get(Library.OPTION_SYMBOL_PROVIDER);
+ if (optionSymbolProvider == null) {
+ this.symbolProvider = NATIVE_SYMBOL_PROVIDER;
+ } else {
+ this.symbolProvider = optionSymbolProvider;
}
if (this.encoding == null) {
@@ -649,11 +650,7 @@
if (handle == 0) {
throw new UnsatisfiedLinkError("Library has been unloaded");
}
- if (symbolProvider != null) {
- return symbolProvider.getSymbolAddress(handle, name, defaultSymbolProvider);
- } else {
- return Native.findSymbol(handle, name);
- }
+ return this.symbolProvider.getSymbolAddress(handle, name, NATIVE_SYMBOL_PROVIDER);
}
@Override
2b4c6f5 to
df762de
Compare
|
@matthiasblaesing applied your patch since it makes sense to me. Also added my real name in the git commit. Regarding to " |
I mean this construct (untested!): interface MathInterfaceWithSymbolProvider extends Library {
public MathInterfaceWithSymbolProvider INSTANCE = Native.load(
Platform.MATH_LIBRARY_NAME,
MathInterfaceWithSymbolProvider.class, Collections.singletonMap(
Library.OPTION_SYMBOL_PROVIDER,
new SymbolProvider() {
@Override
public long getSymbolAddress(long handle, String name, SymbolProvider parent) {
if (name.equals("sin")) {
return parent.getSymbolAddress(handle, "cos", null);
} else {
return parent.getSymbolAddress(handle, "sin", null);
}
}
}
)
);
double cos(double x);
double sin(double x);
}
The implementation can be found here: jna/src/com/sun/jna/Library.java Lines 117 to 268 in 9e7160e HTH |
e9f597f to
de7dd2e
Compare
|
I see. So I guess incidencially this should be usable with the interface variant too. I have updated the title of the PR, updated the README and added a test so the scope of this PR is properly described |
de7dd2e to
cd8474c
Compare
matthiasblaesing
left a comment
There was a problem hiding this comment.
Final nitpick: Please move the tests together into NativeCustomSymbolProviderTest. That way the test is focus on a single feature and not spread over two classes.
cd8474c to
5a6a9f2
Compare
|
@matthiasblaesing Done. |
matthiasblaesing
left a comment
There was a problem hiding this comment.
Change looks good to me and Appveyor, Travis and github actions agree. Thank you.
This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [net.java.dev.jna:jna](https://github.com/java-native-access/jna) | compile | minor | `5.12.1` -> `5.13.0` | --- ### Release Notes <details> <summary>java-native-access/jna</summary> ### [`v5.13.0`](https://github.com/java-native-access/jna/blob/HEAD/CHANGES.md#Release-5130) [Compare Source](java-native-access/jna@5.12.1...5.13.0) \================ ## Features - [#​1454](java-native-access/jna#1454): Add `c.s.j.p.win32.Psapi.QueryWorkingSetEx` and associated Types - [@​crain-32](https://github.com/Crain-32). - [#​1459](java-native-access/jna#1459): Add `VirtualLock` and `VirtualUnlock` in `c.s.j.p.win32.Kernel32` - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1471](java-native-access/jna#1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@​dbwiddis](https://github.com/dbwiddis). - [#​1474](java-native-access/jna#1474): Add `c.s.j.p.win32.WbemCli#IWbemClassObject.IWbemQualifierSet`, `IWbemServices.GetObject`, `IWbemContext.SetValue` and associated methods - [@​rchateauneu](https://github.com/rchateauneu). - [#​1482](java-native-access/jna#1482): Add multilingual support of `Kernel32Util.formatMessage` - [@​overpathz](https://github.com/overpathz). - [#​1490](java-native-access/jna#1490): Adds support for a custom `SymbolProvider` in `NativeLibrary` & `Library` - [@​soywiz](https://github.com/soywiz). - [#​1491](java-native-access/jna#1491): Update libffi to v3.4.4 - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1487](java-native-access/jna#1487): Add 'uses' information to OSGI metadata in MANIFEST.MF to improve stability of package resolution - [@​sratz](https://github.com/sratz). ## Bug Fixes - [#​1452](java-native-access/jna#1452): Fix memory allocation/handling for error message generation in native library code (`dispatch.c`) - [@​matthiasblaesing](https://github.com/matthiasblaesing). - [#​1460](java-native-access/jna#1460): Fix win32 variant date conversion in DST offest window and with millisecond values - [@​eranl](https://github.com/eranl). - [#​1472](java-native-access/jna#1472): Fix incorrect bitmask in `c.s.j.Pointer#createConstant(int)` - [@​dbwiddis](https://github.com/dbwiddis). - [#​1481](java-native-access/jna#1481): Fix NPE in NativeLibrary when unpacking from classpath is disabled - [@​trespasserw](https://github.com/trespasserw). - [#​1489](java-native-access/jna#1489): Fixes typo in `OpenGL32Util#wglGetProcAddress`, instead of parameter `procName` the hardcoded value `wglEnumGpusNV` was used - [@​soywiz](https://github.com/soywiz). </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNC4wIiwidXBkYXRlZEluVmVyIjoiMzQuMjQuMCJ9-->
Retake on #1483 I'm using a different branch, since I used master and that was problematic for syncing the original repo. I have also squashed all the commits into one.
This allows to do function hooking/replacement while still using direct mapping.
In addition to that, to properly using the OpenGL library on Windows, and access OpenGL >= 2.0, we need to use a custom
getSymbolfunction: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglgetprocaddressIn the test I'm switching
cosandsinfunctions to show it works.Started a thread here: https://groups.google.com/g/jna-users/c/RDt8sHAnTm0