-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Support Default Methods on interfaces #813
Comments
JNA aims for a compatibility with older Java versions. Default interfaces were added Java 8 and the instrumentation for default methods was also added in Java 8. This is what happens:
So what would need to be implemented:
With reflection this could be implemented in a backwards compatible manner. At this point default methods can't be used with JNA. Please note: Issues should be discussed on the jna-users user forum first (as noted in the issue template). |
It might be necessary to cover different invocation handlers at least |
@joerg1985 @Master-01 if one of you wants to have a try. I did some experimenting here: |
@joerg1985 @Master-01 I updated the branch so that actually works. I tested it on Java 8 + 11. Testing/comments would be appreciated! |
@matthiasblaesing thanks for the fast response, i tryed it but it throws an IllegalArgumentException (see stacktrace below). I think the call to ReflectionUtils.invokeDefaultMethod is not correct, the proxy object is passed twice and the method is ignored. But even if i change the method call to: Update: I think the call should be Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class |
@joerg1985 I screwed the implementation in ProxyObject up (I should not commit right before I want to go to bed). This should work (ProxyObject, line 220): if((!declaredAsInterface) && ReflectionUtils.isDefault(method)) {
Object methodHandle = ReflectionUtils.getMethodHandle(method);
return ReflectionUtils.invokeDefaultMethod(proxy, methodHandle, args);
} I'm currently rebuilding my windows test setup and run a test on it also. |
@joerg1985 I pushed updates to the referenced branch. I ran the unittest on windows and it worked. Please see if it works for you. |
@matthiasblaesing 👍 this time it works as expected, thanks a lot. |
Thank you for testing. Merged the changeset to master. |
Hellow!
I use Java8 interface
public static interface IKernelApiAll extends Kernel32
{
@Override public HANDLE GetCurrentThread();
public int SetThreadAffinityMask(final Pointer hThread, final int dwThreadAffinityMask);
default public int SetCurrentThreadAffinityMask(final int dwThreadAffinityMask) { return SetThreadAffinityMask(GetCurrentThread().getPointer(), dwThreadAffinityMask); }
}
when call SetCurrentThreadAffinityMask(0) I get an error
Exception in thread "thread1" java.lang.UnsatisfiedLinkError: Error looking up function 'SetCurrentThreadAffinityMask': not found procedure
at com.sun.jna.Function.(Function.java:245)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:566)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:542)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:528)
at com.sun.jna.Library$Handler.invoke(Library.java:228)
at com.sun.proxy.$Proxy8.SetCurrentThreadAffinityMask(Unknown Source)
Why JNA is trying to find a procedure in the DLL if the keyword is specified in the declaration as default.
The default functions should simply be ignored for find prodedure procedures in the DLL, and simple execute default java code!
The text was updated successfully, but these errors were encountered: