MCPcopy Create free account
hub / github.com/astonbitecode/j4rs / invoke

Method invoke

rust/src/api/mod.rs:831–908  ·  view source on GitHub ↗

Invokes the method `method_name` of a created `Instance`, passing an array of `InvocationArg`s. It returns an `Instance` as the result of the invocation.

(
        &self,
        instance: &Instance,
        method_name: &str,
        inv_args: &[impl Borrow<InvocationArg>],
    )

Source from the content-addressed store, hash-verified

829
830 /// Invokes the method `method_name` of a created `Instance`, passing an array of `InvocationArg`s. It returns an `Instance` as the result of the invocation.
831 pub fn invoke(
832 &self,
833 instance: &Instance,
834 method_name: &str,
835 inv_args: &[impl Borrow<InvocationArg>],
836 ) -> errors::Result<Instance> {
837 debug(&format!(
838 "Invoking method {} of class {} using {} arguments",
839 method_name,
840 instance.class_name,
841 inv_args.len()
842 ));
843 unsafe {
844 // First argument: create a jstring to pass as argument for the method_name
845 let method_name_jstring: jstring =
846 jni_utils::global_jobject_from_str(method_name, self.jni_env)?;
847
848 // Rest of the arguments: Create a new objectarray of class InvocationArg
849 let size = inv_args.len() as i32;
850 let array_ptr = {
851 let j = (opt_to_res(cache::get_jni_new_object_array())?)(
852 self.jni_env,
853 size,
854 cache::get_invocation_arg_class()?,
855 ptr::null_mut(),
856 );
857 jni_utils::create_global_ref_from_local_ref(j, self.jni_env)?
858 };
859 let mut inv_arg_jobjects: Vec<jobject> = Vec::with_capacity(size as usize);
860
861 // Rest of the arguments: populate the array
862 for i in 0..size {
863 // Create an InvocationArg Java Object
864 let inv_arg_java = inv_args[i as usize]
865 .borrow()
866 .as_java_ptr_with_global_ref(self.jni_env)?;
867 // Set it in the array
868 (opt_to_res(cache::get_jni_set_object_array_element())?)(
869 self.jni_env,
870 array_ptr,
871 i,
872 inv_arg_java,
873 );
874 inv_arg_jobjects.push(inv_arg_java);
875 }
876
877 // Call the method of the instance
878 let java_instance = (opt_to_res(cache::get_jni_call_object_method())?)(
879 self.jni_env,
880 instance.jinstance,
881 cache::get_invoke_method()?,
882 method_name_jstring,
883 array_ptr,
884 );
885
886 // Check for exceptions before creating the globalref
887 Self::do_return(self.jni_env, ())?;
888

Callers

nothing calls this directly

Calls 13

debugFunction · 0.85
global_jobject_from_strFunction · 0.85
opt_to_resFunction · 0.85
get_jni_new_object_arrayFunction · 0.85
get_invocation_arg_classFunction · 0.85
get_invoke_methodFunction · 0.85
do_returnFunction · 0.85
delete_java_refFunction · 0.85

Tested by

no test coverage detected