java_bindgen/
j2r.rs

1use jni::objects::{JByteArray, JObject, JString, JValueGen};
2use crate::prelude::*;
3
4use super::*;
5use crate::exception::*;
6
7// Java To Rust
8pub trait IntoRustType<'local, T> {
9    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<T>;
10}
11
12// Option<T>
13impl<'local, T: IntoRustType<'local, R>, R> IntoRustType<'local, Option<R>> for Option<T> {
14    fn into_rust(self, env: &mut JNIEnv<'local>) -> JResult<Option<R>> {
15        match self {
16            None => { Ok(None) }
17            Some(v) => {
18                let t = v.into_rust(env)?;
19                Ok(Some(t))
20            }
21        }
22    }
23}
24
25impl<'local> IntoRustType<'local, bool> for jni::sys::jboolean {
26    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<bool> {
27        Ok(self == jni::sys::JNI_TRUE)
28    }
29}
30
31impl<'local> IntoRustType<'local, u8> for jni::sys::jbyte {
32    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<u8> {
33        Ok(self as u8)
34    }
35}
36
37impl<'local> IntoRustType<'local, i8> for jni::sys::jbyte {
38    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<i8> {
39        Ok(self)
40    }
41}
42
43impl<'local> IntoRustType<'local, char> for jni::sys::jchar {
44    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<char> {
45        char::from_u32(self as u32).ok_or(JException::from_class_and_msg(
46            JExceptionClass::ClassCastException,
47            "u32 to char",
48        ))
49    }
50}
51
52impl<'local> IntoRustType<'local, u16> for jni::sys::jchar {
53    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<u16> {
54        Ok(self)
55    }
56}
57
58impl<'local> IntoRustType<'local, i16> for jni::sys::jshort {
59    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<i16> {
60        Ok(self)
61    }
62}
63
64impl<'local> IntoRustType<'local, i32> for jni::sys::jint {
65    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<i32> {
66        Ok(self)
67    }
68}
69
70impl<'local> IntoRustType<'local, i64> for jni::sys::jlong {
71    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<i64> {
72        Ok(self)
73    }
74}
75
76impl<'local> IntoRustType<'local, f32> for jni::sys::jfloat {
77    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<f32> {
78        Ok(self)
79    }
80}
81
82impl<'local> IntoRustType<'local, f64> for jni::sys::jdouble {
83    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<f64> {
84        Ok(self)
85    }
86}
87
88impl<'local> IntoRustType<'local, Vec<u8>> for JByteArray<'local> {
89    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<Vec<u8>> {
90        env.convert_byte_array(self)
91            .j_catch_ini(env, "Cast failed [JByteArray -> Vec<u8>]")
92    }
93}
94
95impl<'local> IntoRustType<'local, String> for JString<'local> {
96    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<String> {
97        env.get_string_owned(&self)
98            .j_catch_ini(env, "Cast failed [JString -> String]")
99    }
100}
101
102impl<'local> IntoRustType<'local, String> for JObject<'local> {
103    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<String> {
104        let j_string = JString::from(self);
105        env.get_string_owned(&j_string)
106            .j_catch_ini(env, "Cast failed [JObject -> String]")
107    }
108}
109
110// JValueGen into_rust Option<T>
111
112impl<'local, T> IntoRustType<'local, Option<T>> for jni::objects::JValueGen<JObject<'local>>
113where
114    JValueGen<JObject<'local>>: IntoRustType<'local, T>,
115    JObject<'local>: IntoRustType<'local, T>,
116{
117    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> JResult<Option<T>> {
118        match self {
119            JValueGen::Object(obj) => {
120                if obj.is_null() { return Ok(None); }
121                let v: T = obj.into_rust(env)?;
122                Ok(Some(v))
123            }
124            _ => {
125                let v: T = self.into_rust(env)?;
126                Ok(Some(v))
127            }
128        }
129    }
130}
131
132// JValueGen to primitive
133
134impl<'local> IntoRustType<'local, u8> for jni::objects::JValueGen<JObject<'local>> {
135    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<u8> {
136        let b = self.b().j_catch_ini(env, "Cast failed [JObject -> u8]")?;
137        Ok(b as u8)
138    }
139}
140
141impl<'local> IntoRustType<'local, i8> for jni::objects::JValueGen<JObject<'local>> {
142    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i8> {
143        self.b().j_catch_ini(env, "Cast failed [JObject -> i8]")
144    }
145}
146
147impl<'local> IntoRustType<'local, i16> for jni::objects::JValueGen<JObject<'local>> {
148    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i16> {
149        self.s().j_catch_ini(env, "Cast failed [JObject -> i16]")
150    }
151}
152
153impl<'local> IntoRustType<'local, i32> for jni::objects::JValueGen<JObject<'local>> {
154    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i32> {
155        self.i().j_catch_ini(env, "Cast failed [JObject -> i32]")
156    }
157}
158
159impl<'local> IntoRustType<'local, i64> for jni::objects::JValueGen<JObject<'local>> {
160    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i64> {
161        self.j().j_catch_ini(env, "Cast failed [JObject -> i64]")
162    }
163}
164
165impl<'local> IntoRustType<'local, f32> for jni::objects::JValueGen<JObject<'local>> {
166    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<f32> {
167        self.f().j_catch_ini(env, "Cast failed [JObject -> f32]")
168    }
169}
170
171impl<'local> IntoRustType<'local, f64> for jni::objects::JValueGen<JObject<'local>> {
172    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<f64> {
173        self.d().j_catch_ini(env, "Cast failed [JObject -> f64]")
174    }
175}
176
177impl<'local> IntoRustType<'local, bool> for jni::objects::JValueGen<JObject<'local>> {
178    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<bool> {
179        self.z().j_catch_ini(env, "Cast failed [JObject -> bool]")
180    }
181}
182
183impl<'local> IntoRustType<'local, char> for jni::objects::JValueGen<JObject<'local>> {
184    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<char> {
185        let char = self.c().j_catch_ini(env, "Cast failed [JObject -> char]")?;
186        char.into_rust(env)
187    }
188}
189
190impl<'local> IntoRustType<'local, String> for jni::objects::JValueGen<JObject<'local>> {
191    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<String> {
192        let obj = self.l().j_catch_ini(env, "Cast failed [JObject -> String]")?;
193        obj.into_rust(env)
194    }
195}
196
197impl<'local> IntoRustType<'local, Vec<u8>> for jni::objects::JValueGen<JObject<'local>> {
198    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<Vec<u8>> {
199        let obj = self.l().j_catch_ini(env, "Cast failed [JObject -> byte[]]")?;
200        obj.into_rust(env)
201    }
202}
203
204// JValueGen to class primitive
205#[allow(non_snake_case)]
206macro_rules! JValueGen_obj_to_class_primitive_impl {
207    ($rust_type:tt) => {
208        impl<'local> IntoRustType<'local, $rust_type>
209            for jni::objects::JValueGen<JObject<'local>>
210        {
211            fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<$rust_type> {
212                let obj = self.l()?;
213                let byte = obj.into_rust(env)?;
214                Ok($rust_type(byte))
215            }
216        }
217    };
218}
219
220JValueGen_obj_to_class_primitive_impl!(JByte);
221JValueGen_obj_to_class_primitive_impl!(JShort);
222JValueGen_obj_to_class_primitive_impl!(JInt);
223JValueGen_obj_to_class_primitive_impl!(JLong);
224JValueGen_obj_to_class_primitive_impl!(JFloat);
225JValueGen_obj_to_class_primitive_impl!(JDouble);
226JValueGen_obj_to_class_primitive_impl!(JBoolean);
227JValueGen_obj_to_class_primitive_impl!(JChar);
228
229// JObject to class primitive
230
231macro_rules! obj_to_class_primitive_impl {
232    ($rust_type:tt) => {
233        impl<'local> IntoRustType<'local, $rust_type> for jni::objects::JObject<'local> {
234            fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<$rust_type> {
235                let value = self.into_rust(env)?;
236                Ok($rust_type(value))
237            }
238        }
239    };
240}
241
242obj_to_class_primitive_impl!(JByte);
243obj_to_class_primitive_impl!(JShort);
244obj_to_class_primitive_impl!(JInt);
245obj_to_class_primitive_impl!(JLong);
246obj_to_class_primitive_impl!(JFloat);
247obj_to_class_primitive_impl!(JDouble);
248obj_to_class_primitive_impl!(JBoolean);
249obj_to_class_primitive_impl!(JChar);
250
251// impl<'local> IntoRustType<'local, Option<JInt>> for jni::objects::JObject<'local> {
252//     fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<Option<JInt>> {
253//         if self.is_null() {
254//             return Ok(None)
255//         }
256//
257//         let value = self.into_rust(env)?;
258//         Ok(Some(JInt(value)))
259//     }
260// }
261
262// JObject to primitive
263
264impl<'local> IntoRustType<'local, u8> for JObject<'local> {
265    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<u8> {
266        let value: u8 = self.call_getter("byteValue", env)?;
267        Ok(value)
268    }
269}
270
271impl<'local> IntoRustType<'local, i8> for JObject<'local> {
272    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i8> {
273        let value: u8 = self.call_getter("byteValue", env)?;
274        Ok(value as i8)
275    }
276}
277
278impl<'local> IntoRustType<'local, i16> for JObject<'local> {
279    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i16> {
280        let value: i16 = self.call_getter("shortValue", env)?;
281        Ok(value)
282    }
283}
284
285impl<'local> IntoRustType<'local, i32> for JObject<'local> {
286    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i32> {
287        let value = self.call_getter("intValue", env)?;
288        Ok(value)
289    }
290}
291
292impl<'local> IntoRustType<'local, i64> for JObject<'local> {
293    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<i64> {
294        let value = self.call_getter("longValue", env)?;
295        Ok(value)
296    }
297}
298
299impl<'local> IntoRustType<'local, f32> for JObject<'local> {
300    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<f32> {
301        let value = self.call_getter("floatValue", env)?;
302        Ok(value)
303    }
304}
305
306impl<'local> IntoRustType<'local, f64> for JObject<'local> {
307    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<f64> {
308        let value = self.call_getter("doubleValue", env)?;
309        Ok(value)
310    }
311}
312
313impl<'local> IntoRustType<'local, char> for JObject<'local> {
314    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<char> {
315        let value = self.call_getter("charValue", env)?;
316        Ok(value)
317    }
318}
319
320impl<'local> IntoRustType<'local, bool> for JObject<'local> {
321    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<bool> {
322        let value = self.call_getter("booleanValue", env)?;
323        Ok(value)
324    }
325}
326
327impl<'local> IntoRustType<'local, Vec<u8>> for JObject<'local> {
328    fn into_rust(self, env: &mut jni::JNIEnv<'local>) -> crate::JResult<Vec<u8>> {
329        JByteArray::from(self).into_rust(env)
330    }
331}
332
333// Raw JNI types (Allows pass raw type as fn argument)
334
335impl<'local> IntoRustType<'local, JString<'local>> for JString<'local> {
336    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<JString<'local>> {
337        Ok(self)
338    }
339}
340
341impl<'local> IntoRustType<'local, JByteArray<'local>> for JByteArray<'local> {
342    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<JByteArray<'local>> {
343        Ok(self)
344    }
345}
346
347impl<'local> IntoRustType<'local, JObject<'local>> for JObject<'local> {
348    fn into_rust(self, _: &mut jni::JNIEnv<'local>) -> crate::JResult<JObject<'local>> {
349        Ok(self)
350    }
351}