feat: better custom exception handling + related test

This commit is contained in:
zaaarf 2024-09-26 02:08:29 +02:00
parent 7e2c8d4138
commit 0ddc593771
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
5 changed files with 34 additions and 14 deletions

View file

@ -66,18 +66,9 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
quote::quote! { quote::quote! {
let ret = match result { let ret = match result {
Ok(x) => x, Ok(x) => x,
Err(e) => match #env_iden.find_class(e.jclass()) { Err(e) => match #env_iden.throw_new(e.jclass(), format!("{e:?}")) {
Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"), Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
Ok(class) => match #env_iden.new_string(format!("{e:?}")) { Ok(_) => return #return_expr
Err(e) => panic!("error throwing Java exception -- failed creating error string: {e}"),
Ok(msg) => match #env_iden.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)]) {
Err(e) => panic!("error throwing Java exception -- failed creating object: {e}"),
Ok(obj) => match #env_iden.throw(jni::objects::JThrowable::from(obj)) {
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
Ok(_) => return #return_expr,
},
},
},
} }
}; };
} }

View file

@ -9,5 +9,7 @@ crate-type = ["cdylib"]
path = "test.rs" path = "test.rs"
[dependencies] [dependencies]
jni-toolbox-macro = { path = "../../macro/" }
jni-toolbox = { path = "../.." } jni-toolbox = { path = "../.." }
jni = "0.21" jni = "0.21"
thiserror = "1"

View file

@ -0,0 +1,7 @@
package toolbox;
public class CustomException extends Exception {
public CustomException(String msg) {
super(msg);
}
}

View file

@ -17,6 +17,7 @@ public class Main {
static native boolean maybe(String optional); static native boolean maybe(String optional);
static native String optional(boolean present); static native String optional(boolean present);
static native String raw(); static native String raw();
static native void throw_error();
@Test @Test
public void argumentsByValue() { public void argumentsByValue() {
@ -61,5 +62,9 @@ public class Main {
assertNull(Main.optional(false)); assertNull(Main.optional(false));
assertEquals(Main.optional(true), "hello world!"); assertEquals(Main.optional(true), "hello world!");
} }
@Test
public void throwError() {
assertThrows(CustomException.class, Main::throw_error);
}
} }

View file

@ -1,4 +1,4 @@
use jni_toolbox::jni; use jni_toolbox::{jni, JniToolboxError};
#[jni(package = "toolbox", class = "Main")] #[jni(package = "toolbox", class = "Main")]
fn sum(a: i32, b: i32) -> i32 { fn sum(a: i32, b: i32) -> i32 {
@ -33,3 +33,18 @@ fn optional(present: bool) -> Option<String> {
fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> { fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> {
env.new_string("hello world!") env.new_string("hello world!")
} }
#[derive(thiserror::Error, Debug)]
#[error("some test error")]
struct CustomError;
impl JniToolboxError for CustomError {
fn jclass(&self) -> String {
"toolbox/CustomException".to_string()
}
}
#[jni(package = "toolbox", class = "Main")]
fn throw_error() -> Result<(), CustomError> {
Err(CustomError)
}