update at 2022-02-25 10:30:10 by ehlxr

This commit is contained in:
ehlxr 2022-02-25 10:30:10 +08:00
parent 7fa307f3a6
commit 7ffd8ac5f2
2 changed files with 26 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package me.ehlxr;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.HashMap;
class HelloWorld { class HelloWorld {
static { static {
@ -56,11 +57,18 @@ class HelloWorld {
ls.add("ls1"); ls.add("ls1");
ls.add("ls2"); ls.add("ls2");
ls.add("ls3"); ls.add("ls3");
Map<String, Long> map = new HashMap<>();
map.put("k1", 1L);
map.put("k2", 2L);
map.put("k3", 3L);
HelloWorld hw = new HelloWorld(); HelloWorld hw = new HelloWorld();
hw.setName("Jack"); hw.setName("Jack");
hw.no = 123434555L; hw.no = 123434555L;
hw.age = 30; hw.age = 30;
hw.ls = ls; hw.ls = ls;
hw.map = map;
System.out.println(HelloWorld.getFiled(hw)); System.out.println(HelloWorld.getFiled(hw));
} }

View File

@ -1,7 +1,6 @@
use jni::objects::{GlobalRef, JClass, JList, JObject, JString, JValue}; use jni::objects::{GlobalRef, JClass, JObject, JString, JValue};
use jni::sys::{jbyteArray, jint, jlong, jstring}; use jni::sys::{jbyteArray, jint, jlong, jstring};
use jni::JNIEnv; use jni::JNIEnv;
use std::slice::SliceIndex;
use std::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
#[no_mangle] #[no_mangle]
@ -16,14 +15,14 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
.l() .l()
.unwrap(); .unwrap();
let jmap = env.get_map(map).unwrap(); let jmap = env.get_map(map).unwrap();
// jmp.get(slice)
jmap.iter().unwrap().into_iter().for_each(|jmap_iter| { jmap.iter().unwrap().into_iter().for_each(|jmap_iter| {
let key: JString = jmap_iter.0.into(); let key: JString = jmap_iter.0.into();
let value: JString = jmap_iter.1.into(); let value = jmap_iter.1;
println!( println!(
"get map key: {}, value: {}", "get map key: {}, value: {}",
String::from(env.get_string(key).unwrap()), String::from(env.get_string(key).unwrap()),
String::from(env.get_string(value).unwrap()) long_value(env, value)
); );
}); });
@ -60,19 +59,13 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
// let no = env.get_field(input, "no", "J").unwrap().j().unwrap(); // let no = env.get_field(input, "no", "J").unwrap().j().unwrap();
// println!("get no field: {}", no); // println!("get no field: {}", no);
let no = env let no = long_value(
.call_method( env,
env.get_field(input, "no", "Ljava/lang/Long;") env.get_field(input, "no", "Ljava/lang/Long;")
.unwrap() .unwrap()
.l() .l()
.unwrap(), .unwrap(),
"longValue", );
"()J",
&[],
)
.unwrap()
.j()
.unwrap();
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
@ -221,6 +214,13 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_asyncComputation(
rx.recv().unwrap(); rx.recv().unwrap();
} }
fn long_value(env: JNIEnv, jobj: JObject) -> i64 {
env.call_method(jobj, "longValue", "()J", &[])
.unwrap()
.j()
.unwrap()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]