This repository has been archived by the owner on May 10, 2019. It is now read-only.
/
main.rs
139 lines (117 loc) · 4.57 KB
/
main.rs
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
extern crate jni_sys;
extern crate rucaja;
use rucaja::{
jvalue_from_jboolean, jvalue_from_jobject, Jvm, JvmAttachment, JvmClass, JvmMethod, JvmString,
JNI_FALSE, JNI_TRUE,
};
use std::ptr::null;
/// The main function.
fn main() {
// JVM options.
let jvm_options = [
//"-Djava.security.debug=all",
//"-verbose:gc",
//"-verbose:jni",
//"-Xcheck:jni",
];
// Instantiate the embedded JVM.
let jvm = Jvm::new(&jvm_options);
// Attach the current native thread to the JVM.
let jvm_attachment = JvmAttachment::new(jvm.jvm()).unwrap();
// Get the Java class `Test` from `Test.class`.
let class = JvmClass::get_class(&jvm_attachment, "Test").expect("Could not find JVM class");
// Get the `println()` method from `Test.class` so that we can print JVM objects.
let println =
JvmMethod::get_static_method(&jvm_attachment, &class, "println", "(Ljava/lang/Object;)V")
.expect("Could not find JVM method");
create_a_java_string(&jvm_attachment, &class, &println);
// Call some Java methods from `Test.class`.
call_static_boolean_method(&jvm_attachment, &class);
call_static_object_method(&jvm_attachment, &class, &println);
call_static_void_method(&jvm_attachment, &class);
}
/// Calls a static Java method that returns a Java `bool`.
fn call_static_boolean_method(jvm_attachment: &JvmAttachment, class: &JvmClass) {
// The arguments for the Java methods.
let jvm_method_arguments = [JNI_FALSE, JNI_TRUE];
// Get the Java method.
let jvm_method = JvmMethod::get_static_method(
&jvm_attachment,
&class,
"static_method_that_returns_a_boolean",
"(Z)Z",
)
.expect("Could not find JVM method");
// Iterate over the Java method arguments.
for jvm_method_argument in &jvm_method_arguments {
let args = vec![jvalue_from_jboolean(*jvm_method_argument)];
// Call the java method with the current argument.
let result = JvmMethod::call_static_boolean_method(
&jvm_attachment,
&class,
&jvm_method,
args.as_ptr(),
);
// Print the result of the Java call.
println!(
"* `call_static_boolean_method({})`; {:?}",
jvm_method_argument, result
);
}
}
/// Calls static Java methods that return a Java object.
fn call_static_object_method(
jvm_attachment: &JvmAttachment,
class: &JvmClass,
println: &JvmMethod,
) {
// The names of Java methods to call.
let jvm_method_names = [
"static_method_that_returns_a_string",
"static_method_that_returns_an_interned_string",
];
// Iterate over the Java method names.
for jvm_method_name in &jvm_method_names {
// Get the current Java method.
let jvm_method = JvmMethod::get_static_method(
&jvm_attachment,
&class,
jvm_method_name,
"()Ljava/lang/String;",
)
.expect("Could not find JVM method");
// Call the Java method.
let jvm_object =
JvmMethod::call_static_object_method(&jvm_attachment, &class, &jvm_method, null())
.unwrap();
println!(
"* `call_static_object_method(): `{}()` returned {:?}`",
jvm_method_name,
jvm_object.jvm_ptr()
);
// Print the Java result object via a Java method.
println!("** print the JVM object:");
let args = vec![jvalue_from_jobject(jvm_object.jvm_ptr())];
JvmMethod::call_static_void_method(&jvm_attachment, &class, &println, args.as_ptr());
}
}
/// Calls a static void Java method.
fn call_static_void_method(jvm_attachment: &JvmAttachment, class: &JvmClass) {
// Get the Java method.
let jvm_method =
JvmMethod::get_static_method(&jvm_attachment, &class, "static_void_method", "()V")
.expect("Could not find JVM method");
JvmMethod::call_static_void_method(&jvm_attachment, &class, &jvm_method, null());
println!("* `call_static_void_method()`");
}
/// Creates a Java string.
fn create_a_java_string(jvm_attachment: &JvmAttachment, class: &JvmClass, println: &JvmMethod) {
println!("* `create_a_java_string()`");
// Create a Java string.
let jvm_string =
JvmString::new(&jvm_attachment, "Hello World").expect("Could not create a string");
// Print the Java string via a Java method.
println!("** print the JVM string:");
let args = vec![jvalue_from_jobject(jvm_string.jvm_ptr())];
JvmMethod::call_static_void_method(&jvm_attachment, &class, &println, args.as_ptr());
}