Compare commits

...

21 Commits

Author SHA1 Message Date
85c43bfe26 update at 2022-02-28 11:41:34 by ehlxr 2022-02-28 11:41:34 +08:00
d53aa005a6 update at 2022-02-25 19:54:10 by ehlxr 2022-02-25 19:54:10 +08:00
e54101b14f update at 2022-02-25 19:43:38 by ehlxr 2022-02-25 19:43:38 +08:00
0819343f0e update at 2022-02-25 17:06:09 by ehlxr 2022-02-25 17:06:09 +08:00
2758c25d35 update at 2022-02-25 14:57:47 by ehlxr 2022-02-25 14:57:47 +08:00
d4fa79c52c update at 2022-02-25 14:56:41 by ehlxr 2022-02-25 14:56:41 +08:00
bac25e0830 update at 2022-02-25 14:50:02 by ehlxr 2022-02-25 14:50:02 +08:00
cef17e09ec update at 2022-02-25 11:37:04 by ehlxr 2022-02-25 11:37:04 +08:00
a5a597a032 update at 2022-02-25 10:33:51 by ehlxr 2022-02-25 10:33:51 +08:00
7ffd8ac5f2 update at 2022-02-25 10:30:10 by ehlxr 2022-02-25 10:30:10 +08:00
7fa307f3a6 update at 2022-02-25 10:13:00 by ehlxr 2022-02-25 10:13:00 +08:00
3f420a6e55 update at 2022-02-25 10:03:38 by ehlxr 2022-02-25 10:03:38 +08:00
d8f8551f62 update at 2022-02-25 09:57:08 by ehlxr 2022-02-25 09:57:08 +08:00
0b58332005 update at 2022-02-24 11:18:34 by ehlxr 2022-02-24 11:18:34 +08:00
7280cff05a update at 2022-02-24 11:03:22 by ehlxr 2022-02-24 11:03:22 +08:00
c8150fa580 update at 2022-02-23 23:09:51 by ehlxr 2022-02-23 23:09:51 +08:00
09406d01da update at 2022-02-23 23:09:27 by ehlxr 2022-02-23 23:09:27 +08:00
21b0ed000d update at 2022-02-23 23:08:27 by ehlxr 2022-02-23 23:08:27 +08:00
53521e036f update at 2022-02-23 20:37:36 by ehlxr 2022-02-23 20:37:36 +08:00
ed1ac08fe6 update at 2022-02-23 09:41:05 by ehlxr 2022-02-23 09:41:05 +08:00
61b8d5338b update at 2022-02-23 09:39:30 by ehlxr 2022-02-23 09:39:30 +08:00
5 changed files with 426 additions and 30 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
mylib/target
**/*.class
**/*.class
**/*.h

12
Makefile Normal file
View File

@@ -0,0 +1,12 @@
java_run: lib
cd java_src && \
javac me/ehlxr/HelloWorld.java && \
java -Djava.library.path=../mylib/target/debug/ me.ehlxr.HelloWorld
.PHONY: lib
javah:
cd java_src && \
javah me.ehlxr.HelloWorld
lib:
cd mylib && cargo build

View File

@@ -1,23 +1,7 @@
Rust build
Rust bindings to the JNI Demo.
Run
```shell
cd mylib
cargo build
```
Java build
```shell
cd java_src
javac me/ehlxr/HelloWorld.java
```
Run Java
```shell
cd java_src
// Linux: export LD_LIBRARY_PATH=../mylib/target/debug/
export JAVA_LIBRARY_PATH=../mylib/target/debug/
java me.ehlxr.HelloWorld
> make java_run
```

View File

@@ -1,15 +1,94 @@
package me.ehlxr;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class HelloWorld {
class HelloWorld {
static {
// Linux: export LD_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug
// Mac: export JAVA_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug
// Linux: export LD_LIBRARY_PATH=/Users/ehlxr/Workspaces/Rust/jni_rs/mylib/target/debug
// Mac: export JAVA_LIBRARY_PATH=/Users/ehlxr/Workspaces/Rust/jni_rs/mylib/target/debug
System.loadLibrary("mylib");
}
public Long no;
private String name;
public int age;
public List<String> list;
public Map<String, Long> map;
private static native String hello(String input);
private static native byte[] helloByte(byte[] input);
private static native void factAndCallMeBack(int n, HelloWorld callback);
private static native long counterNew(HelloWorld callback);
private static native void counterIncrement(long counter_ptr);
private static native void counterDestroy(long counter_ptr);
private static native void asyncComputation(HelloWorld callback);
private static native List<Map<String, Object>> getField(HelloWorld param);
public static void main(String[] args) {
String output = HelloWorld.hello("Java");
System.out.println(output);
// byte[] outputByte = HelloWorld.helloByte("byte".getBytes());
// System.out.println(outputByte);
// HelloWorld.factAndCallMeBack(6, new HelloWorld());
// long counter_ptr = counterNew(new HelloWorld());
// for (int i = 0; i < 5; i++) {
// counterIncrement(counter_ptr);
// }
// counterDestroy(counter_ptr);
// System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")");
// asyncComputation(new HelloWorld());
List<String> ls = new ArrayList<>();
ls.add("ls1");
ls.add("ls2");
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();
hw.setName("Jack");
hw.no = 123434555L;
hw.age = 30;
hw.list = ls;
hw.map = map;
System.out.println("get return: " + HelloWorld.getField(hw));
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void factCallback(int res) {
System.out.println("factCallback: res = " + res);
}
public void counterCallback(int count) {
System.out.println("counterCallback: count = " + count);
}
public void asyncCallback(int progress) {
System.out.println("asyncCallback: thread id = " + Thread.currentThread().getId() + ", progress = " + progress + "%");
}
}

View File

@@ -1,6 +1,174 @@
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
use jni::{
errors::Result,
objects::{GlobalRef, JClass, JList, JMap, JObject, JString, JValue},
sys::{jbyteArray, jint, jlong, jobject, jstring},
JNIEnv,
};
use std::{sync::mpsc, thread, time::Duration};
#[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_getField(
env: JNIEnv,
_class: JClass,
input: JObject,
) -> jobject {
// Map field operate
let jmap = env
.get_map(
env.get_field(input, "map", "Ljava/util/Map;")
.unwrap()
.l()
.unwrap(),
)
.unwrap();
let v1 = jmap
.get(JObject::from(env.new_string("k1").unwrap()))
.unwrap()
.unwrap();
println!("get map key k1, value: {}", long_value(env, v1));
if let Ok(_) = jmap.put(
JObject::from(env.new_string("k9").unwrap()),
long_to_jobj(env, 9 as i64),
) {
println!("put key ok");
};
jmap.iter().unwrap().into_iter().for_each(|jmap_iter| {
let key: JString = jmap_iter.0.into();
let value = jmap_iter.1;
println!(
"iter map key: {}, value: {}",
String::from(env.get_string(key).unwrap()),
long_value(env, value)
);
});
// List field operate
let jlist = env
.get_list(
env.get_field(input, "list", "Ljava/util/List;")
.unwrap()
.l()
.unwrap(),
)
.unwrap();
jlist.iter().unwrap().into_iter().for_each(|jobj| {
let jstr: JString = jobj.into();
println!(
"get list field: {}",
String::from(env.get_string(jstr).unwrap())
);
});
// int field operate
let age = env.get_field(input, "age", "I").unwrap().i().unwrap();
println!("get age field: {}", age);
// String field operate
let name: JString = env
.get_field(input, "name", "Ljava/lang/String;")
.unwrap()
.l()
.unwrap()
.into();
println!(
"get name field: {}",
String::from(env.get_string(name).unwrap())
);
// Long field operate
let no = long_value(
env,
env.get_field(input, "no", "Ljava/lang/Long;")
.unwrap()
.l()
.unwrap(),
);
println!("get no field: {}", no);
// call method operate
let out_str = if let JValue::Object(result) = env
.call_method(input, "getName", "()Ljava/lang/String;", &[])
.unwrap()
{
let jstr = env.get_string(JString::from(result)).unwrap();
// println!("call getNameStr result: {}", String::from(jstr));
String::from(jstr)
} else {
"".to_string()
};
println!("Hello {}! from Rust..", out_str);
// new ArrayList operate
let list = unwrap(
&env,
JList::from_env(
&env,
unwrap(&env, env.new_object("java/util/ArrayList", "()V", &[])),
),
);
// new LinkedHashMap operate
let map = unwrap(
&env,
JMap::from_env(
&env,
unwrap(&env, env.new_object("java/util/LinkedHashMap", "()V", &[])),
),
);
// Map put key value operate
unwrap(
&env,
map.put(
JObject::from(env.new_string("no").unwrap()),
long_to_jobj(env, no),
),
);
unwrap(
&env,
map.put(
JObject::from(env.new_string("age").unwrap()),
long_to_jobj(env, age as i64),
),
);
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
unwrap(&env, list.add(JObject::from(map)));
unwrap(&env, list.add(jmap2));
println!("list size {}", list.size().unwrap());
list.into_inner()
// let output = env
// .new_string(format!("Hello {}! from Rust..", out_str))
// .expect("Couldn't create java string!");
// output.into_inner()
}
#[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_hello(
@@ -14,8 +182,160 @@ pub extern "system" fn Java_me_ehlxr_HelloWorld_hello(
.into();
let output = env
.new_string(format!("Hello, {}! from Rust...111", input))
.new_string(format!("Hello, {}! from Rust..", input))
.expect("Couldn't create java string!");
output.into_inner()
}
#[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_helloByte(
env: JNIEnv,
_class: JClass,
input: jbyteArray,
) -> jbyteArray {
let _input = env.convert_byte_array(input).unwrap();
let buf = [1; 2000];
let output = env.byte_array_from_slice(&buf).unwrap();
output
}
#[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_factAndCallMeBack(
env: JNIEnv,
_class: JClass,
n: jint,
callback: JObject,
) {
let i = n as i32;
let res: jint = (2..i + 1).product();
env.call_method(callback, "factCallback", "(I)V", &[res.into()])
.unwrap();
}
struct Counter {
count: i32,
callback: GlobalRef,
}
impl Counter {
pub fn new(callback: GlobalRef) -> Counter {
Counter {
count: 0,
callback: callback,
}
}
pub fn increment(&mut self, env: JNIEnv) {
self.count = self.count + 1;
env.call_method(
&self.callback,
"counterCallback",
"(I)V",
&[self.count.into()],
)
.unwrap();
}
}
#[no_mangle]
pub unsafe extern "system" fn Java_me_ehlxr_HelloWorld_counterNew(
env: JNIEnv,
_class: JClass,
callback: JObject,
) -> jlong {
let global_ref = env.new_global_ref(callback).unwrap();
let counter = Counter::new(global_ref);
Box::into_raw(Box::new(counter)) as jlong
}
#[no_mangle]
pub unsafe extern "system" fn Java_me_ehlxr_HelloWorld_counterIncrement(
env: JNIEnv,
_class: JClass,
counter_ptr: jlong,
) {
let counter = &mut *(counter_ptr as *mut Counter);
counter.increment(env);
}
#[no_mangle]
pub unsafe extern "system" fn Java_me_ehlxr_HelloWorld_counterDestroy(
_env: JNIEnv,
_class: JClass,
counter_ptr: jlong,
) {
let _boxed_counter = Box::from_raw(counter_ptr as *mut Counter);
}
#[no_mangle]
pub extern "system" fn Java_me_ehlxr_HelloWorld_asyncComputation(
env: JNIEnv,
_class: JClass,
callback: JObject,
) {
let jvm = env.get_java_vm().unwrap();
let callback = env.new_global_ref(callback).unwrap();
let (tx, rx) = mpsc::channel();
let _ = thread::spawn(move || {
tx.send(()).unwrap();
let env = jvm.attach_current_thread().unwrap();
for i in 0..11 {
let progress = (i * 10) as jint;
env.call_method(&callback, "asyncCallback", "(I)V", &[progress.into()])
.unwrap();
thread::sleep(Duration::from_millis(100));
}
});
rx.recv().unwrap();
}
fn long_value(env: JNIEnv, jobj: JObject) -> i64 {
env.call_method(jobj, "longValue", "()J", &[])
.unwrap()
.j()
.unwrap()
}
fn long_to_jobj<'a>(env: JNIEnv<'a>, lv: i64) -> JObject<'a> {
env.call_static_method(
"Ljava/lang/Long;",
"valueOf",
"(J)Ljava/lang/Long;",
&[JValue::from(lv)],
)
.unwrap()
.l()
.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)]
mod tests {
#[test]
fn it_works() {}
}