mirror of
https://github.com/hexedtech/jni-toolbox.git
synced 2024-11-21 23:14:55 +01:00
feat: better custom exception handling + related test
This commit is contained in:
parent
7e2c8d4138
commit
0ddc593771
5 changed files with 34 additions and 14 deletions
|
@ -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}"),
|
|
||||||
Ok(class) => match #env_iden.new_string(format!("{e:?}")) {
|
|
||||||
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}"),
|
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
|
||||||
Ok(_) => return #return_expr,
|
Ok(_) => return #return_expr
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
|
7
src/test/java/toolbox/CustomException.java
Normal file
7
src/test/java/toolbox/CustomException.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package toolbox;
|
||||||
|
|
||||||
|
public class CustomException extends Exception {
|
||||||
|
public CustomException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {
|
||||||
|
@ -62,4 +63,8 @@ public class Main {
|
||||||
assertEquals(Main.optional(true), "hello world!");
|
assertEquals(Main.optional(true), "hello world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void throwError() {
|
||||||
|
assertThrows(CustomException.class, Main::throw_error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue