Invokes the static method `method_name` of the class `class_name`, passing an array of `InvocationArg`s. It returns an `Instance` as the result of the invocation.
(
&self,
class_name: &str,
method_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
)
| 1067 | |
| 1068 | /// Invokes the static method `method_name` of the class `class_name`, passing an array of `InvocationArg`s. It returns an `Instance` as the result of the invocation. |
| 1069 | pub fn invoke_static( |
| 1070 | &self, |
| 1071 | class_name: &str, |
| 1072 | method_name: &str, |
| 1073 | inv_args: &[impl Borrow<InvocationArg>], |
| 1074 | ) -> errors::Result<Instance> { |
| 1075 | debug(&format!( |
| 1076 | "Invoking static method {} of class {} using {} arguments", |
| 1077 | method_name, |
| 1078 | class_name, |
| 1079 | inv_args.len() |
| 1080 | )); |
| 1081 | unsafe { |
| 1082 | // Factory invocation - first argument: create a jstring to pass as argument for the class_name |
| 1083 | let class_name_jstring: jstring = |
| 1084 | jni_utils::global_jobject_from_str(class_name, self.jni_env)?; |
| 1085 | // Call the method of the factory that creates a Instance for static calls to methods of class `class_name`. |
| 1086 | // This returns a Instance that acts like a proxy to the Java world. |
| 1087 | let tmp_java_instance = (opt_to_res(cache::get_jni_call_static_object_method())?)( |
| 1088 | self.jni_env, |
| 1089 | cache::get_factory_class()?, |
| 1090 | cache::get_factory_create_for_static_method()?, |
| 1091 | class_name_jstring, |
| 1092 | ); |
| 1093 | |
| 1094 | // First argument: create a jstring to pass as argument for the method_name |
| 1095 | let method_name_jstring: jstring = |
| 1096 | jni_utils::global_jobject_from_str(method_name, self.jni_env)?; |
| 1097 | |
| 1098 | // Rest of the arguments: Create a new objectarray of class InvocationArg |
| 1099 | let size = inv_args.len() as i32; |
| 1100 | let array_ptr = { |
| 1101 | let j = (opt_to_res(cache::get_jni_new_object_array())?)( |
| 1102 | self.jni_env, |
| 1103 | size, |
| 1104 | cache::get_invocation_arg_class()?, |
| 1105 | ptr::null_mut(), |
| 1106 | ); |
| 1107 | jni_utils::create_global_ref_from_local_ref(j, self.jni_env)? |
| 1108 | }; |
| 1109 | let mut inv_arg_jobjects: Vec<jobject> = Vec::with_capacity(size as usize); |
| 1110 | // Rest of the arguments: populate the array |
| 1111 | for i in 0..size { |
| 1112 | // Create an InvocationArg Java Object |
| 1113 | let inv_arg_java = inv_args[i as usize] |
| 1114 | .borrow() |
| 1115 | .as_java_ptr_with_global_ref(self.jni_env)?; |
| 1116 | // Set it in the array |
| 1117 | (opt_to_res(cache::get_jni_set_object_array_element())?)( |
| 1118 | self.jni_env, |
| 1119 | array_ptr, |
| 1120 | i, |
| 1121 | inv_arg_java, |
| 1122 | ); |
| 1123 | inv_arg_jobjects.push(inv_arg_java); |
| 1124 | } |
| 1125 | // Call the method of the instance |
| 1126 | let java_instance = (opt_to_res(cache::get_jni_call_object_method())?)( |