| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use jni::objects::{JMap, JObject, JString, JValue}; |
| 16 | use jni::JNIEnv; |
| 17 | |
| 18 | use crate::Result; |
| 19 | |
| 20 | pub trait FromJObject<T> { |
| 21 | fn extract(&self) -> Result<T>; |
| 22 | } |
| 23 | |
| 24 | /// Convert a Rust type into a Java Object. |
| 25 | pub trait IntoJava { |
| 26 | fn into_java<'a>(self, env: &mut JNIEnv<'a>) -> JObject<'a>; |
| 27 | } |
| 28 | |
| 29 | impl FromJObject<i32> for JObject<'_> { |
| 30 | fn extract(&self) -> Result<i32> { |
| 31 | Ok(JValue::from(self).i()?) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl FromJObject<i64> for JObject<'_> { |
| 36 | fn extract(&self) -> Result<i64> { |
| 37 | Ok(JValue::from(self).j()?) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl FromJObject<f32> for JObject<'_> { |
| 42 | fn extract(&self) -> Result<f32> { |
| 43 | Ok(JValue::from(self).f()?) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl FromJObject<f64> for JObject<'_> { |
| 48 | fn extract(&self) -> Result<f64> { |
| 49 | Ok(JValue::from(self).d()?) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub trait FromJString { |
| 54 | fn extract(&self, env: &mut JNIEnv) -> Result<String>; |
| 55 | } |
| 56 | |
| 57 | impl FromJString for JString<'_> { |
| 58 | fn extract(&self, env: &mut JNIEnv) -> Result<String> { |
| 59 | Ok(env.get_string(self)?.into()) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub trait JMapExt { |
| 64 | #[allow(dead_code)] |
| 65 | fn get_string(&self, env: &mut JNIEnv, key: &str) -> Result<Option<String>>; |
| 66 | |
| 67 | #[allow(dead_code)] |
| 68 | fn get_i32(&self, env: &mut JNIEnv, key: &str) -> Result<Option<i32>>; |
| 69 | |
| 70 | #[allow(dead_code)] |
| 71 | fn get_i64(&self, env: &mut JNIEnv, key: &str) -> Result<Option<i64>>; |
| 72 | |
| 73 | #[allow(dead_code)] |
| 74 | fn get_f32(&self, env: &mut JNIEnv, key: &str) -> Result<Option<f32>>; |
| 75 | |
| 76 | #[allow(dead_code)] |
| 77 | fn get_f64(&self, env: &mut JNIEnv, key: &str) -> Result<Option<f64>>; |
| 78 | } |
| 79 | |
| 80 | fn get_map_value<T>(env: &mut JNIEnv, map: &JMap, key: &str) -> Result<Option<T>> |
| 81 | where |
| 82 | for<'a> JObject<'a>: FromJObject<T>, |
| 83 | { |
| 84 | let key_obj: JObject = env.new_string(key)?.into(); |
| 85 | if let Some(value) = map.get(env, &key_obj)? { |
| 86 | if value.is_null() { |
| 87 | Ok(None) |
| 88 | } else { |
| 89 | Ok(Some(value.extract()?)) |
| 90 | } |
| 91 | } else { |
| 92 | Ok(None) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl JMapExt for JMap<'_, '_, '_> { |
| 97 | fn get_string(&self, env: &mut JNIEnv, key: &str) -> Result<Option<String>> { |
| 98 | let key_obj: JObject = env.new_string(key)?.into(); |
| 99 | if let Some(value) = self.get(env, &key_obj)? { |
| 100 | let value_str: JString = value.into(); |
| 101 | Ok(Some(value_str.extract(env)?)) |
| 102 | } else { |
| 103 | Ok(None) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fn get_i32(&self, env: &mut JNIEnv, key: &str) -> Result<Option<i32>> { |
| 108 | get_map_value(env, self, key) |
| 109 | } |
| 110 | |
| 111 | fn get_i64(&self, env: &mut JNIEnv, key: &str) -> Result<Option<i64>> { |
| 112 | get_map_value(env, self, key) |
| 113 | } |
| 114 | |
| 115 | fn get_f32(&self, env: &mut JNIEnv, key: &str) -> Result<Option<f32>> { |
| 116 | get_map_value(env, self, key) |
| 117 | } |
| 118 | |
| 119 | fn get_f64(&self, env: &mut JNIEnv, key: &str) -> Result<Option<f64>> { |
| 120 | get_map_value(env, self, key) |
| 121 | } |
| 122 | } |