-
Notifications
You must be signed in to change notification settings - Fork 67
Support throwing Java exceptions in interface implementation #1209
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
Comments
Some pointers in case you want to fix this @Anikate-De Here is the place where we throw DartExceptions: https://github.com/dart-lang/native/blob/main/pkgs/jni/java/src/main/java/com/github/dart_lang/jni/PortProxy.java#L82-L90 Currently we only check if the result is a To add support for all kinds of exceptions. We can either:
Option 2 is probably faster to implement. In both cases you will need to check if the thrown object is a |
Thanks for the pointers! I tried following Option 2
private static final class DartException extends Exception {
Throwable cause;
private DartException(String message, Throwable cause) {
super(message);
this.cause = cause;
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object[] result = _invoke(port, isolateId, functionPtr, proxy, getDescriptor(method), args);
_cleanUp((Long) result[0]);
if (result[1] instanceof DartException) {
Throwable cause = ((DartException) result[1]).cause;
if (cause != null) {
throw cause;
} else {
throw (DartException) result[1];
}
}
return result[1];
} I then updated the JniResult DartException__ctor(jstring message, jthrowable cause); Likewise updated But here is the problem I faced: Reference: native/pkgs/jni/lib/src/accessors.dart Lines 107 to 111 in a44ef95
I'm confused about how I could check whether it is a Also, I'm still unable to workaround I can send a Draft PR if you'd like to check it out, just need to add some comments. |
Thanks for working on this!
No, that's just returning the new To regenerate the bindings for jni, run the script in After the regeneration of FFI bindings, Back to the codegen, if the caught exception
e is JObject && Jni.env.IsInstanceOf(e.reference.pointer, jThrowableClass.reference.pointer) You can temporarily put
Please do. I'll assign you to this issue! |
Another thing that users might want to do is throw actual Java exceptions. This seems to be useful actually, and we could use the same Dart throw keyword to achieve this by checking if the exception thrown is of type JException.
The text was updated successfully, but these errors were encountered: