-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy paths006_create_object.rs
More file actions
46 lines (43 loc) · 1.63 KB
/
s006_create_object.rs
File metadata and controls
46 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use jni::objects::{JClass, JMethodID, JObject};
use jni::JNIEnv;
use std::sync::Mutex;
static MTX_CONSTRUCTOR_METHOD_ID: Mutex<Option<JMethodID>> = Mutex::new(None);
#[no_mangle]
pub extern "system" fn Java_sample_s006_ObjectCreator_create<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
) -> JObject<'local> {
let obj = env
.new_object("sample/data/Demo", "()V", &[])
.expect("Failed to create object");
obj
}
#[no_mangle]
pub extern "system" fn Java_sample_s006_ObjectCreator_createFast<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
) -> JObject<'local> {
match MTX_CONSTRUCTOR_METHOD_ID.lock() {
Ok(mut guard) => {
if guard.is_none() {
let constructor_method_id = env
.get_method_id("sample/data/Demo", "<init>", "()V")
.expect("Failed to get method id");
*guard = Some(constructor_method_id);
}
let constructor_method_id = guard.as_ref().unwrap();
let obj = unsafe {
env.new_object_unchecked("sample/data/Demo", *constructor_method_id, &[])
.expect("Failed to create object")
};
obj
}
Err(e) => panic!("lock failed: {}", e),
}
}