update at 2022-02-25 09:57:08 by ehlxr

This commit is contained in:
ehlxr 2022-02-25 09:57:08 +08:00
parent 0b58332005
commit d8f8551f62
2 changed files with 45 additions and 5 deletions

View File

@ -1,4 +1,7 @@
package me.ehlxr; package me.ehlxr;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
class HelloWorld { class HelloWorld {
static { static {
@ -7,9 +10,11 @@ class HelloWorld {
System.loadLibrary("mylib"); System.loadLibrary("mylib");
} }
public long no; public Long no;
private String name; private String name;
public int age; public int age;
public List<String> ls;
public Map<String, Long> map;
private static native String hello(String input); private static native String hello(String input);
@ -47,10 +52,15 @@ class HelloWorld {
// System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")"); // System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")");
// asyncComputation(new HelloWorld()); // asyncComputation(new HelloWorld());
List<String> ls = new ArrayList<>();
ls.add("ls1");
ls.add("ls2");
ls.add("ls3");
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;
System.out.println(HelloWorld.getFiled(hw)); System.out.println(HelloWorld.getFiled(hw));
} }

View File

@ -1,4 +1,4 @@
use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; use jni::objects::{GlobalRef, JClass, JList, 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::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
@ -9,6 +9,23 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
_class: JClass, _class: JClass,
input: JObject, input: JObject,
) -> jstring { ) -> jstring {
let jlist = env
.get_list(
env.get_field(input, "ls", "Ljava/util/List;")
.unwrap()
.l()
.unwrap(),
)
.unwrap();
let jlist_iter = jlist.iter().unwrap();
jlist_iter.into_iter().for_each(|jobj| {
let jstr: JString = jobj.into();
println!(
"get list filed: {}",
String::from(env.get_string(jstr).unwrap())
);
});
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);
@ -23,9 +40,22 @@ 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(); // let no = env.get_field(input, "no", "J").unwrap().j().unwrap();
// let jstr = env.get_string(JString::from(no)).unwrap(); // println!("get no field: {}", no);
// println!("get addr field: {}", String::from(jstr));
let no = env
.call_method(
env.get_field(input, "no", "Ljava/lang/Long;")
.unwrap()
.l()
.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