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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use jni_sys::{jboolean, jint, jmethodID, jobject, jvalue};
use jvm::{print_and_panic_on_jvm_exception, print_jvm_exception};
use jvm_attachment::JvmAttachment;
use jvm_class::JvmClass;
use jvm_object::JvmObject;
use std::ffi::CString;
/// Represents a method in a class in the JVM.
pub struct JvmMethod {
// Guaranteed not to be a null pointer.
jvm_method_ptr: jmethodID,
}
impl<'a> JvmMethod {
/// Tries to call the given JVM object constructor in the given JVM class.
/// Currently panics if a JVM exception occurs.
pub fn call_constructor(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_constructor_method: &JvmMethod, args: *const jvalue
) -> jobject {
let jvm_object = unsafe {
(**jvm_attachment.jni_environment()).NewObjectA.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_constructor_method.jvm_ptr(),
args
)
};
print_and_panic_on_jvm_exception(jvm_attachment.jni_environment());
jvm_object
}
// TODO: call_boolean_method()
// TODO: call_byte_method()
// TODO: call_char_method()
// TODO: call_double_method()
// TODO: call_float_method()
// TODO: call_int_method()
// TODO: call_long_method()
// TODO: call_object_method()
// TODO: call_short_method()
// TODO: call_void_method()
// TODO: call_nonvirtual_boolean_method()
// TODO: call_nonvirtual_byte_method()
// TODO: call_nonvirtual_char_method()
// TODO: call_nonvirtual_double_method()
// TODO: call_nonvirtual_float_method()
// TODO: call_nonvirtual_int_method()
// TODO: call_nonvirtual_long_method()
// TODO: call_nonvirtual_object_method()
// TODO: call_nonvirtual_short_method()
// TODO: call_nonvirtual_void_method()
// TODO: call_static_boolean_method()
/// Tries to call the given JVM static boolean method in the given JVM class.
/// Currently panics if a JVM exception occurs.
pub fn call_static_boolean_method(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method: &JvmMethod, args: *const jvalue
) -> jboolean {
let result = unsafe {
(**jvm_attachment.jni_environment()).CallStaticBooleanMethodA.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method.jvm_ptr(),
args
)
};
print_and_panic_on_jvm_exception(jvm_attachment.jni_environment());
result
}
// TODO: call_static_byte_method()
// TODO: call_static_char_method()
// TODO: call_static_double_method()
// TODO: call_static_float_method()
/// Tries to call the given JVM static int method in the given JVM class.
/// Currently panics if a JVM exception occurs.
pub fn call_static_int_method(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method: &JvmMethod, args: *const jvalue
) -> jint {
let result = unsafe {
(**jvm_attachment.jni_environment()).CallStaticIntMethodA.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method.jvm_ptr(),
args
)
};
print_and_panic_on_jvm_exception(jvm_attachment.jni_environment());
result
}
// TODO: call_static_long_method()
///
pub fn call_static_object_method(
jvm_attachment: &'a JvmAttachment, jvm_class: &JvmClass, jvm_method: &JvmMethod, args: *const jvalue
) -> Option<JvmObject<'a>> {
let result = unsafe {
(**jvm_attachment.jni_environment()).CallStaticObjectMethodA.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method.jvm_ptr(),
args
)
};
print_and_panic_on_jvm_exception(jvm_attachment.jni_environment());
JvmObject::from_jvm_ptr(jvm_attachment, result)
}
/// Tries to call the given JVM static void method in the given JVM class.
/// Currently panics if a JVM exception occurs.
pub fn call_static_void_method(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method: &JvmMethod, args: *const jvalue
) {
unsafe {
(**jvm_attachment.jni_environment()).CallStaticVoidMethodA.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method.jvm_ptr(),
args
);
}
print_and_panic_on_jvm_exception(jvm_attachment.jni_environment());
}
/// Tries to resolve the JVM constructor with the given signature in the given JVM class.
pub fn get_constructor(jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method_signature: &str) -> Option<JvmMethod> {
JvmMethod::get_method(jvm_attachment, jvm_class, "<init>", jvm_method_signature)
}
/// Tries to resolve the JVM method with the given name and signature in the given JVM class.
pub fn get_method(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method_name: &str, jvm_method_signature: &str
) -> Option<JvmMethod> {
let jvm_method_name_cstring = CString::new(jvm_method_name).unwrap();
let jvm_method_signature_cstring = CString::new(jvm_method_signature).unwrap();
let jvm_method_ptr = unsafe {
(**jvm_attachment.jni_environment()).GetMethodID.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method_name_cstring.as_ptr(),
jvm_method_signature_cstring.as_ptr()
)
};
// Print any JVM exception.
print_jvm_exception(jvm_attachment.jni_environment());
JvmMethod::new(jvm_method_ptr)
}
/// Tries to resolve the static JVM method with the given name and signature in the given JVM class.
pub fn get_static_method(
jvm_attachment: &JvmAttachment, jvm_class: &JvmClass, jvm_method_name: &str, jvm_method_signature: &str
) -> Option<JvmMethod> {
let jvm_method_name_cstring = CString::new(jvm_method_name).unwrap();
let jvm_method_signature_cstring = CString::new(jvm_method_signature).unwrap();
let jvm_method_ptr = unsafe {
(**jvm_attachment.jni_environment()).GetStaticMethodID.unwrap()(
jvm_attachment.jni_environment(),
jvm_class.jvm_ptr(),
jvm_method_name_cstring.as_ptr(),
jvm_method_signature_cstring.as_ptr()
)
};
// Print any JVM exception.
print_jvm_exception(jvm_attachment.jni_environment());
JvmMethod::new(jvm_method_ptr)
}
///
pub fn jvm_ptr(&self) -> jmethodID {
self.jvm_method_ptr
}
///
pub fn new(jvm_method_ptr: jmethodID) -> Option<JvmMethod> {
if jvm_method_ptr.is_null() {
return None;
}
// A `jmethodID` is not a `jobject`, that's why it's neither necessary nor possible to call `NewGlobalRef()`.
Some(JvmMethod { jvm_method_ptr } )
}
}