Skip to content

Commit

Permalink
🦄 refactor: Revise jni object cast
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 25, 2024
1 parent be29071 commit 76d363e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
17 changes: 9 additions & 8 deletions rust/src/jni_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ pub fn get_as_int<'local>(env: &mut JNIEnv<'local>, obj: &JObject<'_>, method: J
}
}

pub fn get_as_jobject<'local, 'a>(env: &mut JNIEnv<'local>, obj: &JObject<'_>, method: JMethodID) -> JObject<'a> {
pub fn get_as_jobject<'local, 'a>(env: &mut JNIEnv<'local>, obj: &JObject<'_>, method: JMethodID) -> JObject<'a>
where
'local: 'a,
{
unsafe {
JObject::from_raw(
env
.call_method_unchecked(&obj, method, ReturnType::Object, &[])
.expect("Object is expected")
.as_jni()
.l,
)
env
.call_method_unchecked(&obj, method, ReturnType::Object, &[])
.expect("Object is expected")
.l()
.expect("Object is expected")
}
}

Expand Down
31 changes: 21 additions & 10 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ impl JavaParseOptions {
}
}

pub fn get_media_type<'local, 'a, 'b>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'a>) -> JObject<'b> {
pub fn get_media_type<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> JObject<'a>
where
'local: 'a,
{
jni_utils::get_as_jobject(env, obj, self.method_get_media_type)
}

pub fn get_parse_mode<'local, 'a, 'b>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'a>) -> JObject<'b> {
pub fn get_parse_mode<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> JObject<'a>
where
'local: 'a,
{
jni_utils::get_as_jobject(env, obj, self.method_get_parse_mode)
}

Expand Down Expand Up @@ -232,11 +238,10 @@ impl JavaTranspileOptions {
}
}

pub fn get_imports_not_used_as_values<'local, 'a, 'b>(
&self,
env: &mut JNIEnv<'local>,
obj: &JObject<'a>,
) -> JObject<'b> {
pub fn get_imports_not_used_as_values<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> JObject<'a>
where
'local: 'a,
{
jni_utils::get_as_jobject(env, obj, self.method_get_imports_not_used_as_values)
}

Expand All @@ -248,15 +253,21 @@ impl JavaTranspileOptions {
jni_utils::get_as_string(env, obj, self.method_get_jsx_fragment_factory)
}

pub fn get_jsx_import_source<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'a>) -> Option<String> {
pub fn get_jsx_import_source<'local>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> Option<String> {
jni_utils::get_as_optional_string(env, obj, self.method_get_jsx_import_source)
}

pub fn get_media_type<'local, 'a, 'b>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'a>) -> JObject<'b> {
pub fn get_media_type<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> JObject<'a>
where
'local: 'a,
{
jni_utils::get_as_jobject(env, obj, self.method_get_media_type)
}

pub fn get_parse_mode<'local, 'a, 'b>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'a>) -> JObject<'b> {
pub fn get_parse_mode<'local, 'a>(&self, env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> JObject<'a>
where
'local: 'a,
{
jni_utils::get_as_jobject(env, obj, self.method_get_parse_mode)
}

Expand Down

0 comments on commit 76d363e

Please sign in to comment.