Creates a new Java Array with elements of the class `class_name`. The array will have the `InvocationArg`s populated. The `InvocationArg`s __must__ be of type _class_name_.
(
&self,
class_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
)
| 549 | /// The array will have the `InvocationArg`s populated. |
| 550 | /// The `InvocationArg`s __must__ be of type _class_name_. |
| 551 | pub fn create_java_array( |
| 552 | &self, |
| 553 | class_name: &str, |
| 554 | inv_args: &[impl Borrow<InvocationArg>], |
| 555 | ) -> errors::Result<Instance> { |
| 556 | debug(&format!( |
| 557 | "Creating a java array of class {} with {} elements", |
| 558 | class_name, |
| 559 | inv_args.len() |
| 560 | )); |
| 561 | unsafe { |
| 562 | // Factory invocation - first argument: create a jstring to pass as argument for the class_name |
| 563 | let class_name_jstring: jstring = |
| 564 | jni_utils::global_jobject_from_str(class_name, self.jni_env)?; |
| 565 | |
| 566 | // Factory invocation - rest of the arguments: Create a new objectarray of class InvocationArg |
| 567 | let size = inv_args.len() as i32; |
| 568 | let array_ptr = { |
| 569 | let j = (opt_to_res(cache::get_jni_new_object_array())?)( |
| 570 | self.jni_env, |
| 571 | size, |
| 572 | cache::get_invocation_arg_class()?, |
| 573 | ptr::null_mut(), |
| 574 | ); |
| 575 | jni_utils::create_global_ref_from_local_ref(j, self.jni_env)? |
| 576 | }; |
| 577 | let mut inv_arg_jobjects: Vec<jobject> = Vec::with_capacity(size as usize); |
| 578 | |
| 579 | // Factory invocation - rest of the arguments: populate the array |
| 580 | for i in 0..size { |
| 581 | // Create an InvocationArg Java Object |
| 582 | let inv_arg_java = inv_args[i as usize] |
| 583 | .borrow() |
| 584 | .as_java_ptr_with_global_ref(self.jni_env)?; |
| 585 | // Set it in the array |
| 586 | (opt_to_res(cache::get_jni_set_object_array_element())?)( |
| 587 | self.jni_env, |
| 588 | array_ptr, |
| 589 | i, |
| 590 | inv_arg_java, |
| 591 | ); |
| 592 | inv_arg_jobjects.push(inv_arg_java); |
| 593 | } |
| 594 | // Call the method of the factory that instantiates a new Java Array of `class_name`. |
| 595 | // This returns a Instance that acts like a proxy to the Java world. |
| 596 | let java_instance = (opt_to_res(cache::get_jni_call_static_object_method())?)( |
| 597 | self.jni_env, |
| 598 | cache::get_factory_class()?, |
| 599 | cache::get_factory_create_java_array_method()?, |
| 600 | class_name_jstring, |
| 601 | array_ptr, |
| 602 | ); |
| 603 | |
| 604 | // Check for exceptions before creating the globalref |
| 605 | Self::do_return(self.jni_env, ())?; |
| 606 | |
| 607 | let java_instance_global_instance = |
| 608 | jni_utils::create_global_ref_from_local_ref(java_instance, self.jni_env)?; |