Skip to content

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

Closed
Tracked by #561 ...
HosseinYousefi opened this issue Jun 13, 2024 · 3 comments · Fixed by #1211
Closed
Tracked by #561 ...

Support throwing Java exceptions in interface implementation #1209

HosseinYousefi opened this issue Jun 13, 2024 · 3 comments · Fixed by #1211
Assignees
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) package:jni package:jnigen

Comments

@HosseinYousefi
Copy link
Member

HosseinYousefi commented Jun 13, 2024

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.

@HosseinYousefi
Copy link
Member Author

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 DartException and only then we throw it. But we want to throw every type of exception. This works because the DartException class is private and we can be sure that no one is returning an instance of it (so the only way we get it as a result is if someone has thrown).

To add support for all kinds of exceptions. We can either:

  1. Add another object to the result array, where result[0] is the pointer, result[1] is the actual result and result[2] is an integer enum, for example 0 when it's a normal returned value and 1 when it's a thrown error. Or
  2. Add a nullable Throwable cause to DartException and fill it if we throw a Java throwable from Dart. Then check if cause is not null and if so throw the cause instead of the top level DartException.

Option 2 is probably faster to implement.

In both cases you will need to check if the thrown object is a JObject and is an instance of java.lang.Throwable in the generated code. https://github.com/dart-lang/native/blob/main/pkgs/jnigen/lib/src/bindings/dart_generator.dart#L415-L417

@HosseinYousefi HosseinYousefi added the contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) label Jun 13, 2024
@Anikate-De
Copy link
Contributor

Thanks for the pointers!
I am still a little confused, unfortunately.

I tried following Option 2

2. Add a nullable Throwable cause to DartException and fill it if we throw a Java throwable from Dart. Then check if cause is not null and if so throw the cause instead of the top level DartException.

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 dartjni.h to include the cause

JniResult DartException__ctor(jstring message, jthrowable cause);

Likewise updated dartjni.c

But here is the problem I faced:
Won't this https://github.com/dart-lang/native/blob/main/pkgs/jnigen/lib/src/bindings/dart_generator.dart#L415-L417, always throw a JniException?

Reference:

/// Rethrows Java exception in Dart as [JniException].
///
/// The original exception object is deleted by this method. The message
/// and Java stack trace are included in the exception.
void throwException(JThrowablePtr exception) {

I'm confused about how I could check whether it is a JObject and then a java.lang.Throwable

Also, I'm still unable to workaround java.net.SocketTimeoutException for ok_http with this approach :(

I can send a Draft PR if you'd like to check it out, just need to add some comments.

@HosseinYousefi
Copy link
Member Author

Thanks for working on this!

Won't this https://github.com/dart-lang/native/blob/main/pkgs/jnigen/lib/src/bindings/dart_generator.dart#L415-L417, always throw a JniException?

No, that's just returning the new DartException object. You just have to add the cause to it as well as the message: https://github.com/dart-lang/native/blob/main/pkgs/jni/lib/src/jni.dart#L239-L244

To regenerate the bindings for jni, run the script in tool/generate_ffi_bindings.dart. And after changing the code generation in jnigen, you can generate all the bindings again using tool/regenerate_all_bindings.dart (in jnigen).

After the regeneration of FFI bindings, DartException__ctor will get an extra parameter. There are some signatures that you need to also change in C. But it's easier for me to comment on a drafted PR for those.

Back to the codegen, if the caught exception e is an instance of java.lang.Throwable then you can use that for the cause argument, otherwise you can pass null.

I'm confused about how I could check whether it is a JObject and then a java.lang.Throwable

e is JObject && Jni.env.IsInstanceOf(e.reference.pointer, jThrowableClass.reference.pointer)

You can temporarily put final jThrowableClass = JClass.forName('java/lang/Throwable'); somewhere for example in internal_helpers_for_jnigen.dart. Until we have a full fledged JThrowable in package:jni.

I can send a Draft PR if you'd like to check it out, just need to add some comments.

Please do. I'll assign you to this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) package:jni package:jnigen
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants