update at 2022-02-25 14:50:02 by ehlxr

This commit is contained in:
ehlxr 2022-02-25 14:50:02 +08:00
parent cef17e09ec
commit bac25e0830
2 changed files with 49 additions and 11 deletions

View File

@ -31,7 +31,7 @@ class HelloWorld {
private static native void asyncComputation(HelloWorld callback); private static native void asyncComputation(HelloWorld callback);
private static native String getFiled(HelloWorld param); private static native List<Map<String, Long>> getFiled(HelloWorld param);
public static void main(String[] args) { public static void main(String[] args) {
String output = HelloWorld.hello("Java"); String output = HelloWorld.hello("Java");
@ -69,7 +69,7 @@ class HelloWorld {
hw.age = 30; hw.age = 30;
hw.ls = ls; hw.ls = ls;
hw.map = map; hw.map = map;
System.out.println(HelloWorld.getFiled(hw)); System.out.println("get return: "+HelloWorld.getFiled(hw));
} }
public String getName() { public String getName() {

View File

@ -1,14 +1,14 @@
use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; use jni::errors::Result;
use jni::sys::{jbyteArray, jint, jlong, jstring}; use jni::objects::{GlobalRef, JClass, JList, JMap, JObject, JString, JValue};
use jni::sys::{jbyteArray, jint, jlong, jobject, jstring};
use jni::JNIEnv; use jni::JNIEnv;
use std::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled( pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
env: JNIEnv, env: JNIEnv,
_class: JClass, _class: JClass,
input: JObject, input: JObject,
) -> jstring { ) -> jobject {
let jmap = env let jmap = env
.get_map( .get_map(
env.get_field(input, "map", "Ljava/util/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); 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;", &[]) .call_method(input, "getName", "()Ljava/lang/String;", &[])
.unwrap() .unwrap()
{ {
@ -93,10 +93,32 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
"".to_string() "".to_string()
}; };
let output = env let map_object = unwrap(&env, env.new_object("java/util/LinkedHashMap", "()V", &[]));
.new_string(format!("Hello {}! from Rust..", out_str)) let jmap = JMap::from_env(&env, map_object).unwrap();
.expect("Couldn't create java string!"); jmap.put(
output.into_inner() 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] #[no_mangle]
@ -247,6 +269,22 @@ fn long_to_jobj<'a>(env: JNIEnv<'a>, lv: i64) -> JObject<'a> {
.unwrap() .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<T>(env: &JNIEnv, res: Result<T>) -> T {
res.unwrap_or_else(|e| {
print_exception(&env);
panic!("{:#?}", e);
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]