MCPcopy Index your code
hub / github.com/cedar-policy/cedar-java / assert_is_class

Function assert_is_class

CedarJavaFFI/src/utils.rs:27–48  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

25/// Queries the environment to check if `obj` belongs to the `name` class
26/// Errors if it does not
27pub 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
51pub fn get_class_name<'a>(env: &mut JNIEnv<'a>, class: JClass<'a>) -> Result<String> {

Callers 1

castMethod · 0.85

Calls 2

raise_npeFunction · 0.85
get_class_nameFunction · 0.85

Tested by

no test coverage detected