update at 2022-02-25 19:43:38 by ehlxr

This commit is contained in:
ehlxr 2022-02-25 19:43:38 +08:00
parent 0819343f0e
commit e54101b14f
2 changed files with 17 additions and 7 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 List<Map<String, Long>> getFiled(HelloWorld param); private static native List<Map<String, Long>> getField(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("get return: "+HelloWorld.getFiled(hw)); System.out.println("get return: " + HelloWorld.getField(hw));
} }
public String getName() { public String getName() {

View File

@ -4,11 +4,12 @@ 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_getField(
env: JNIEnv, env: JNIEnv,
_class: JClass, _class: JClass,
input: JObject, input: JObject,
) -> jobject { ) -> jobject {
// Map field operate
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;")
@ -40,6 +41,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
); );
}); });
// List field operate
let jlist = env let jlist = env
.get_list( .get_list(
env.get_field(input, "ls", "Ljava/util/List;") env.get_field(input, "ls", "Ljava/util/List;")
@ -51,14 +53,16 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
jlist.iter().unwrap().into_iter().for_each(|jobj| { jlist.iter().unwrap().into_iter().for_each(|jobj| {
let jstr: JString = jobj.into(); let jstr: JString = jobj.into();
println!( println!(
"get list filed: {}", "get list field: {}",
String::from(env.get_string(jstr).unwrap()) String::from(env.get_string(jstr).unwrap())
); );
}); });
// int field operate
let age = env.get_field(input, "age", "I").unwrap().i().unwrap(); let age = env.get_field(input, "age", "I").unwrap().i().unwrap();
println!("get age field: {}", age); println!("get age field: {}", age);
// String field operate
let name: JString = env let name: JString = env
.get_field(input, "name", "Ljava/lang/String;") .get_field(input, "name", "Ljava/lang/String;")
.unwrap() .unwrap()
@ -70,9 +74,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
String::from(env.get_string(name).unwrap()) String::from(env.get_string(name).unwrap())
); );
// let no = env.get_field(input, "no", "J").unwrap().j().unwrap(); // Long field operate
// println!("get no field: {}", no);
let no = long_value( let no = long_value(
env, env,
env.get_field(input, "no", "Ljava/lang/Long;") env.get_field(input, "no", "Ljava/lang/Long;")
@ -82,6 +84,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
); );
println!("get no field: {}", no); println!("get no field: {}", no);
// call method operate
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()
@ -94,6 +97,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
}; };
println!("Hello {}! from Rust..", out_str); println!("Hello {}! from Rust..", out_str);
// new ArrayList operate
let jlist = unwrap( let jlist = unwrap(
&env, &env,
JList::from_env( JList::from_env(
@ -102,6 +106,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
), ),
); );
// new LinkedHashMap operate
let jmap = unwrap( let jmap = unwrap(
&env, &env,
JMap::from_env( JMap::from_env(
@ -109,6 +114,8 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
unwrap(&env, env.new_object("java/util/LinkedHashMap", "()V", &[])), unwrap(&env, env.new_object("java/util/LinkedHashMap", "()V", &[])),
), ),
); );
// Map put key value operate
unwrap( unwrap(
&env, &env,
jmap.put( jmap.put(
@ -125,9 +132,12 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
); );
let jmap2 = jmap.clone(); let jmap2 = jmap.clone();
// List add element operate
unwrap(&env, jlist.add(JObject::from(jmap))); unwrap(&env, jlist.add(JObject::from(jmap)));
unwrap(&env, jlist.add(jmap2)); unwrap(&env, jlist.add(jmap2));
println!("list size {}", jlist.size().unwrap());
jlist.into_inner() jlist.into_inner()
// let output = env // let output = env