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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
macro_rules! jvm_wrapper {
($rust_struct_name:ident, $java_type:ident) => (
pub struct $rust_struct_name<'a> {
jvm_attachment: &'a JvmAttachment,
jvm_ptr: $java_type,
}
impl<'a> $rust_struct_name<'a> {
pub fn jvm_ptr(&self) -> $java_type {
self.jvm_ptr
}
pub fn from_jvm_ptr(jvm_attachment: &JvmAttachment, jvm_ptr: $java_type) -> Option<$rust_struct_name> {
if jvm_ptr.is_null() {
return None;
}
let jvm_ptr_global = unsafe {
(**jvm_attachment.jni_environment()).NewGlobalRef.unwrap()(
jvm_attachment.jni_environment(),
jvm_ptr
)
};
if jvm_ptr_global.is_null() {
return None;
}
Some(
$rust_struct_name {
jvm_attachment: jvm_attachment,
jvm_ptr: jvm_ptr_global
}
)
}
}
impl<'a> Drop for $rust_struct_name<'a> {
fn drop(&mut self) {
unsafe {
(**self.jvm_attachment.jni_environment()).DeleteGlobalRef.unwrap()(
self.jvm_attachment.jni_environment(),
self.jvm_ptr
);
}
}
}
)
}
macro_rules! jvm_array_wrapper {
($rust_struct_name:ident, $java_type:ident) => (
jvm_wrapper!($rust_struct_name, $java_type);
impl<'a> $rust_struct_name<'a> {
pub fn length(&self) -> jsize {
let jvm_array_length = unsafe {
(**self.jvm_attachment.jni_environment()).GetArrayLength.unwrap()(
self.jvm_attachment.jni_environment(),
self.jvm_ptr
)
};
jvm_array_length
}
}
)
}