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

Method cast

rust/src/api/mod.rs:1174–1203  ·  view source on GitHub ↗

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, from_instance: &Instance, to_class: &str)

Source from the content-addressed store, hash-verified

1172
1173 /// 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.
1174 pub fn cast(&self, from_instance: &Instance, to_class: &str) -> errors::Result<Instance> {
1175 debug(&format!("Casting to class {}", to_class));
1176 unsafe {
1177 // First argument is the jobject that is inside the from_instance
1178 // Second argument: create a jstring to pass as argument for the to_class
1179 let to_class_jstring: jstring =
1180 jni_utils::global_jobject_from_str(to_class, self.jni_env)?;
1181
1182 // Call the cast method
1183 let java_instance = (opt_to_res(cache::get_jni_call_static_object_method())?)(
1184 self.jni_env,
1185 cache::get_class_to_invoke_clone_and_cast()?,
1186 cache::get_cast_static_method()?,
1187 from_instance.jinstance,
1188 to_class_jstring,
1189 );
1190
1191 // Check for exceptions before creating the globalref
1192 Self::do_return(self.jni_env, ())?;
1193
1194 // Prevent memory leaks from the created local references
1195 jni_utils::delete_java_ref(self.jni_env, to_class_jstring);
1196
1197 // Create and return the Instance
1198 Self::do_return(
1199 self.jni_env,
1200 Instance::from_jobject_with_global_ref(java_instance)?,
1201 )
1202 }
1203 }
1204
1205 /// Checks whether an Instance a is equal to some InvocationArg.
1206 ///

Callers

nothing calls this directly

Calls 8

debugFunction · 0.85
global_jobject_from_strFunction · 0.85
opt_to_resFunction · 0.85
get_cast_static_methodFunction · 0.85
do_returnFunction · 0.85
delete_java_refFunction · 0.85

Tested by

no test coverage detected