Search the class in the cache first. If not found, then call the FindClass of JNI and insert the result to the cache.
(env: *mut JNIEnv, classname: &str)
| 82 | |
| 83 | // Search the class in the cache first. If not found, then call the FindClass of JNI and insert the result to the cache. |
| 84 | pub(crate) fn find_class(env: *mut JNIEnv, classname: &str) -> errors::Result<jclass> { |
| 85 | unsafe { |
| 86 | let mut add_to_cache = false; |
| 87 | |
| 88 | let found_in_cache_opt = CLASSES |
| 89 | .lock()? |
| 90 | .get(classname) |
| 91 | .map(|j4rs_class| j4rs_class.class.clone()); |
| 92 | |
| 93 | let found: errors::Result<jclass> = match found_in_cache_opt { |
| 94 | Some(class) => Ok(class), |
| 95 | None => { |
| 96 | add_to_cache = true; |
| 97 | find_class_default(env, classname) |
| 98 | .or_else(|_| find_class_using_cached_classloader(env, classname)) |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | if add_to_cache { |
| 103 | let mut g = CLASSES.lock()?; |
| 104 | let global = create_global_ref_from_local_ref(found?, env)?; |
| 105 | g.insert( |
| 106 | classname.to_string(), |
| 107 | J4rsAndroidJclass { |
| 108 | class: global.clone(), |
| 109 | }, |
| 110 | ); |
| 111 | Ok(global as jclass) |
| 112 | } else { |
| 113 | Ok(found?) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | unsafe fn find_class_default(env: *mut JNIEnv, classname: &str) -> errors::Result<jclass> { |
| 119 | let fc = (**env).v1_6.FindClass; |
nothing calls this directly
no test coverage detected