Returns the Rust representation of the provided instance, boxed
(&self, instance: Instance)
| 1259 | |
| 1260 | /// Returns the Rust representation of the provided instance, boxed |
| 1261 | pub fn to_rust_boxed<T>(&self, instance: Instance) -> errors::Result<Box<T>> |
| 1262 | where |
| 1263 | T: DeserializeOwned + Any, |
| 1264 | { |
| 1265 | // Define the macro inside the function in order to have access to &self |
| 1266 | macro_rules! rust_box_from_java_object { |
| 1267 | ($jni_transformation:path) => {{ |
| 1268 | // Call the getObjectMethod. This returns a localref |
| 1269 | let object_instance = (opt_to_res(cache::get_jni_call_object_method())?)( |
| 1270 | self.jni_env, |
| 1271 | instance.jinstance, |
| 1272 | cache::get_get_object_method()?, |
| 1273 | ); |
| 1274 | let v = Box::new($jni_transformation(object_instance, self.jni_env)?); |
| 1275 | let v_any = v as Box<dyn Any>; |
| 1276 | |
| 1277 | jni_utils::delete_java_local_ref(self.jni_env, object_instance); |
| 1278 | |
| 1279 | match v_any.downcast::<T>() { |
| 1280 | Ok(v) => Ok(v), |
| 1281 | Err(error) => Err(errors::J4RsError::RustError(format!( |
| 1282 | "Could not downcast to Rust type: {:?}", |
| 1283 | error |
| 1284 | ))), |
| 1285 | } |
| 1286 | }}; |
| 1287 | } |
| 1288 | |
| 1289 | let t_type = TypeId::of::<T>(); |
| 1290 | |
| 1291 | unsafe { |
| 1292 | // Call the getClassName method. This returns a localref |
| 1293 | let object_class_name_instance = (opt_to_res(cache::get_jni_call_object_method())?)( |
| 1294 | self.jni_env, |
| 1295 | instance.jinstance, |
| 1296 | cache::get_get_object_class_name_method()?, |
| 1297 | ); |
| 1298 | let class_name = |
| 1299 | &(jni_utils::string_from_jobject(object_class_name_instance, self.jni_env)?); |
| 1300 | jni_utils::delete_java_local_ref(self.jni_env, object_class_name_instance); |
| 1301 | if t_type == TypeId::of::<String>() && JavaClass::String.get_class_str() == class_name { |
| 1302 | rust_box_from_java_object!(jni_utils::string_from_jobject) |
| 1303 | } else if t_type == TypeId::of::<i32>() |
| 1304 | && (JavaClass::Integer.get_class_str() == class_name || PRIMITIVE_INT == class_name) |
| 1305 | { |
| 1306 | rust_box_from_java_object!(jni_utils::i32_from_jobject) |
| 1307 | } else if t_type == TypeId::of::<i8>() |
| 1308 | && (JavaClass::Byte.get_class_str() == class_name || PRIMITIVE_BYTE == class_name) |
| 1309 | { |
| 1310 | rust_box_from_java_object!(jni_utils::i8_from_jobject) |
| 1311 | } else if t_type == TypeId::of::<i16>() |
| 1312 | && (JavaClass::Short.get_class_str() == class_name || PRIMITIVE_SHORT == class_name) |
| 1313 | { |
| 1314 | rust_box_from_java_object!(jni_utils::i16_from_jobject) |
| 1315 | } else if t_type == TypeId::of::<u16>() |
| 1316 | && (JavaClass::Character.get_class_str() == class_name |
| 1317 | || PRIMITIVE_CHAR == class_name) |
| 1318 | { |