From bac25e08304e1c9be4d1596110c72534cecd9833 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 25 Feb 2022 14:50:02 +0800 Subject: [PATCH] update at 2022-02-25 14:50:02 by ehlxr --- java_src/me/ehlxr/HelloWorld.java | 4 +-- mylib/src/lib.rs | 56 ++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/java_src/me/ehlxr/HelloWorld.java b/java_src/me/ehlxr/HelloWorld.java index 8d73d47..9894ddd 100644 --- a/java_src/me/ehlxr/HelloWorld.java +++ b/java_src/me/ehlxr/HelloWorld.java @@ -31,7 +31,7 @@ class HelloWorld { private static native void asyncComputation(HelloWorld callback); - private static native String getFiled(HelloWorld param); + private static native List> getFiled(HelloWorld param); public static void main(String[] args) { String output = HelloWorld.hello("Java"); @@ -69,7 +69,7 @@ class HelloWorld { hw.age = 30; hw.ls = ls; hw.map = map; - System.out.println(HelloWorld.getFiled(hw)); + System.out.println("get return: "+HelloWorld.getFiled(hw)); } public String getName() { diff --git a/mylib/src/lib.rs b/mylib/src/lib.rs index b2aee56..aaa87a0 100644 --- a/mylib/src/lib.rs +++ b/mylib/src/lib.rs @@ -1,14 +1,14 @@ -use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; -use jni::sys::{jbyteArray, jint, jlong, jstring}; +use jni::errors::Result; +use jni::objects::{GlobalRef, JClass, JList, JMap, JObject, JString, JValue}; +use jni::sys::{jbyteArray, jint, jlong, jobject, jstring}; use jni::JNIEnv; use std::{sync::mpsc, thread, time::Duration}; - #[no_mangle] pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled( env: JNIEnv, _class: JClass, input: JObject, -) -> jstring { +) -> jobject { let jmap = env .get_map( env.get_field(input, "map", "Ljava/util/Map;") @@ -82,7 +82,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled( ); println!("get no field: {}", no); - let out_str = if let JValue::Object(result) = env + let _out_str = if let JValue::Object(result) = env .call_method(input, "getName", "()Ljava/lang/String;", &[]) .unwrap() { @@ -93,10 +93,32 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled( "".to_string() }; - let output = env - .new_string(format!("Hello {}! from Rust..", out_str)) - .expect("Couldn't create java string!"); - output.into_inner() + let map_object = unwrap(&env, env.new_object("java/util/LinkedHashMap", "()V", &[])); + let jmap = JMap::from_env(&env, map_object).unwrap(); + jmap.put( + JObject::from(env.new_string("kd").unwrap()), + long_to_jobj(env, 666 as i64), + ) + .unwrap(); + jmap.put( + JObject::from(env.new_string("kk").unwrap()), + long_to_jobj(env, 999 as i64), + ) + .unwrap(); + let jmap2 = jmap.clone(); + + let list_object = env.new_object("java/util/ArrayList", "()V", &[]).unwrap(); + let jlist = JList::from_env(&env, list_object).unwrap(); + jlist.add(JObject::from(jmap)).unwrap(); + + jlist.add(jmap2).unwrap(); + + jlist.into_inner() + + // let output = env + // .new_string(format!("Hello {}! from Rust..", _out_str)) + // .expect("Couldn't create java string!"); + // output.into_inner() } #[no_mangle] @@ -247,6 +269,22 @@ fn long_to_jobj<'a>(env: JNIEnv<'a>, lv: i64) -> JObject<'a> { .unwrap() } +pub fn print_exception(env: &JNIEnv) { + let exception_occurred = env.exception_check().unwrap_or_else(|e| panic!("{:?}", e)); + if exception_occurred { + env.exception_describe() + .unwrap_or_else(|e| panic!("{:?}", e)); + } +} + +#[allow(dead_code)] +pub fn unwrap(env: &JNIEnv, res: Result) -> T { + res.unwrap_or_else(|e| { + print_exception(&env); + panic!("{:#?}", e); + }) +} + #[cfg(test)] mod tests { #[test]