update at 2022-02-24 11:03:22 by ehlxr

This commit is contained in:
ehlxr 2022-02-24 11:03:22 +08:00
parent c8150fa580
commit 7280cff05a
2 changed files with 55 additions and 27 deletions

View File

@ -1,25 +1,35 @@
package me.ehlxr; package me.ehlxr;
class HelloWorld { class HelloWorld {
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 String fetchNameStr(HelloWorld param);
static { static {
// Linux: export LD_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug // 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 // Mac: export JAVA_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug
System.loadLibrary("mylib"); System.loadLibrary("mylib");
} }
public long no;
private String name;
public int age;
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 String getFiled(HelloWorld param);
public static void main(String[] args) { public static void main(String[] args) {
// String output = HelloWorld.hello("Java"); String output = HelloWorld.hello("Java");
// System.out.println(output); System.out.println(output);
// byte[] outputByte = HelloWorld.helloByte("byte".getBytes()); // byte[] outputByte = HelloWorld.helloByte("byte".getBytes());
// System.out.println(outputByte); // System.out.println(outputByte);
@ -37,20 +47,19 @@ 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());
HelloWorld hw = new HelloWorld(); HelloWorld hw = new HelloWorld();
hw.setNameStr("fsdfs"); hw.setName("Jack");
System.out.println(HelloWorld.fetchNameStr(hw)); hw.no = 123434555L;
hw.age = 30;
System.out.println(HelloWorld.getFiled(hw));
} }
private String nameStr; public String getName() {
return name;
public void setNameStr(String nameStr){
this.nameStr=nameStr;
} }
public String getNameStr(){ public void setName(String name) {
return nameStr; this.name = name;
} }
public void factCallback(int res) { public void factCallback(int res) {

View File

@ -4,13 +4,32 @@ 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_fetchNameStr( pub extern "system" fn Java_me_ehlxr_HelloWorld_getFiled(
env: JNIEnv, env: JNIEnv,
_class: JClass, _class: JClass,
input: JObject, input: JObject,
) -> jstring { ) -> jstring {
let age = env.get_field(input, "age", "I").unwrap().i().unwrap();
println!("get age field: {}", age);
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())
);
let no = env.get_field(input, "no", "J").unwrap().j().unwrap();
// let jstr = env.get_string(JString::from(no)).unwrap();
// println!("get addr field: {}", String::from(jstr));
println!("get no field: {}", no);
let out_str = if let JValue::Object(result) = env let out_str = if let JValue::Object(result) = env
.call_method(input, "getNameStr", "()Ljava/lang/String;", &[]) .call_method(input, "getName", "()Ljava/lang/String;", &[])
.unwrap() .unwrap()
{ {
let jstr = env.get_string(JString::from(result)).unwrap(); let jstr = env.get_string(JString::from(result)).unwrap();