update at 2022-02-25 19:54:10 by ehlxr

This commit is contained in:
ehlxr 2022-02-25 19:54:10 +08:00
parent e54101b14f
commit d53aa005a6
2 changed files with 38 additions and 17 deletions

View File

@ -14,7 +14,7 @@ class HelloWorld {
public Long no; public Long no;
private String name; private String name;
public int age; public int age;
public List<String> ls; public List<String> list;
public Map<String, Long> map; public Map<String, Long> map;
private static native String hello(String input); private static native String hello(String input);
@ -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>> getField(HelloWorld param); private static native List<Map<String, Object>> 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");
@ -67,7 +67,7 @@ class HelloWorld {
hw.setName("Jack"); hw.setName("Jack");
hw.no = 123434555L; hw.no = 123434555L;
hw.age = 30; hw.age = 30;
hw.ls = ls; hw.list = ls;
hw.map = map; hw.map = map;
System.out.println("get return: " + HelloWorld.getField(hw)); System.out.println("get return: " + HelloWorld.getField(hw));
} }

View File

@ -44,7 +44,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getField(
// List field operate // 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, "list", "Ljava/util/List;")
.unwrap() .unwrap()
.l() .l()
.unwrap(), .unwrap(),
@ -98,7 +98,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getField(
println!("Hello {}! from Rust..", out_str); println!("Hello {}! from Rust..", out_str);
// new ArrayList operate // new ArrayList operate
let jlist = unwrap( let list = unwrap(
&env, &env,
JList::from_env( JList::from_env(
&env, &env,
@ -107,7 +107,7 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getField(
); );
// new LinkedHashMap operate // new LinkedHashMap operate
let jmap = unwrap( let map = unwrap(
&env, &env,
JMap::from_env( JMap::from_env(
&env, &env,
@ -118,27 +118,48 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_getField(
// Map put key value operate // Map put key value operate
unwrap( unwrap(
&env, &env,
jmap.put( map.put(
JObject::from(env.new_string("kd").unwrap()), JObject::from(env.new_string("no").unwrap()),
long_to_jobj(env, 666 as i64), long_to_jobj(env, no),
), ),
); );
unwrap( unwrap(
&env, &env,
jmap.put( map.put(
JObject::from(env.new_string("kk").unwrap()), JObject::from(env.new_string("age").unwrap()),
long_to_jobj(env, 999 as i64), long_to_jobj(env, age as i64),
), ),
); );
let jmap2 = jmap.clone(); unwrap(
&env,
map.put(
JObject::from(env.new_string("name").unwrap()),
JObject::from(name),
),
);
unwrap(
&env,
map.put(
JObject::from(env.new_string("list").unwrap()),
JObject::from(jlist),
),
);
unwrap(
&env,
map.put(
JObject::from(env.new_string("map").unwrap()),
JObject::from(jmap),
),
);
let jmap2 = map.clone();
// List add element operate // List add element operate
unwrap(&env, jlist.add(JObject::from(jmap))); unwrap(&env, list.add(JObject::from(map)));
unwrap(&env, jlist.add(jmap2)); unwrap(&env, list.add(jmap2));
println!("list size {}", jlist.size().unwrap()); println!("list size {}", list.size().unwrap());
jlist.into_inner() list.into_inner()
// let output = env // let output = env
// .new_string(format!("Hello {}! from Rust..", out_str)) // .new_string(format!("Hello {}! from Rust..", out_str))