Queries the environment to check if `obj` belongs to the `name` class Errors if it does not
(env: &mut JNIEnv<'a>, obj: &JObject<'a>, name: &str)
| 25 | /// Queries the environment to check if `obj` belongs to the `name` class |
| 26 | /// Errors if it does not |
| 27 | pub fn assert_is_class<'a>(env: &mut JNIEnv<'a>, obj: &JObject<'a>, name: &str) -> Result<()> { |
| 28 | if obj.is_null() { |
| 29 | raise_npe(env)?; |
| 30 | return Err(Box::new(InternalJNIError::NullPointer)); |
| 31 | } |
| 32 | let expected_class = env.find_class(name)?; |
| 33 | let class = env.get_object_class(obj)?; |
| 34 | if env.is_same_object(&expected_class, &class)? { |
| 35 | Ok(()) |
| 36 | } else { |
| 37 | let class_name = get_class_name(env, class)?; |
| 38 | let expected_class_name = get_class_name(env, expected_class)?; |
| 39 | env.throw_new( |
| 40 | "java/lang/ClassCastException", |
| 41 | format!("{class_name} cannot be cast to {expected_class_name}"), |
| 42 | )?; |
| 43 | Err(Box::new(InternalJNIError::TypeError { |
| 44 | expected: expected_class_name, |
| 45 | got: class_name, |
| 46 | })) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Get the name of a class as a String |
| 51 | pub fn get_class_name<'a>(env: &mut JNIEnv<'a>, class: JClass<'a>) -> Result<String> { |
no test coverage detected