toad_jni/java/lang/
primitive_wrappers.rs

1macro_rules! wrapper {
2  (
3    #[doc = $doc:expr]
4    class $cls:ident {
5      static $cls_:ident valueOf($inner_ty:ty);
6      ($inner_ty2:ty) $inner_id:ident();
7    }
8  ) => {
9    #[doc = $doc]
10    pub struct $cls($crate::java::lang::Object);
11    impl $cls {
12      #[doc = concat!("Construct a new ", stringify!($cls))]
13      pub fn new<'local>(e: &mut $crate::java::Env<'local>, b: $inner_ty) -> Self {
14        use $crate::java::Class;
15
16        let cls = e.find_class(Self::PATH).unwrap();
17        let obj = e.call_static_method(cls,
18                                       "valueOf",
19                                       $crate::java::Signature::of::<fn($inner_ty) -> Self>(),
20                                       &[b.into()])
21                   .unwrap()
22                   .l()
23                   .unwrap();
24        Self($crate::java::lang::Object::from_local(e, obj))
25      }
26
27      #[doc = concat!("yield the [`", stringify!($inner_ty), "`] value contained in this `", stringify!($cls), "` by invoking `", stringify!($cls), "#", stringify!($inner_id), "`")]
28      pub fn inner<'local>(&self, e: &mut $crate::java::Env<'local>) -> $inner_ty {
29        let jv = e.call_method(&self.0,
30                               stringify!($inner_id),
31                               $crate::java::Signature::of::<fn() -> $inner_ty>(),
32                               &[])
33                  .unwrap();
34        <$inner_ty as $crate::java::Primitive>::from_jvalue(jv)
35      }
36    }
37
38    impl $crate::java::Class for $cls {
39      const PATH: &'static str = concat!("java/lang/", stringify!($cls));
40    }
41
42    impl $crate::java::Object for $cls {
43      fn upcast<'a, 'e>(_: &'a mut $crate::java::Env<'e>, jobj: $crate::java::lang::Object) -> Self {
44        Self(jobj)
45      }
46
47      fn downcast<'a, 'e>(self, _: &'a mut $crate::java::Env<'e>) -> $crate::java::lang::Object {
48        self.0
49      }
50
51      fn downcast_ref<'a, 'e>(&'a self, e: &'a mut $crate::java::Env<'e>) -> $crate::java::lang::Object {
52        (&self.0).downcast_ref(e)
53      }
54    }
55  };
56}
57
58wrapper! {
59  #[doc = "`java.lang.Byte`"]
60  class Byte {
61    static Byte valueOf(i8);
62    (i8) byteValue();
63  }
64}
65
66wrapper! {
67  #[doc = "`java.lang.Short`"]
68  class Short {
69    static Short valueOf(i16);
70    (i16) shortValue();
71  }
72}
73
74wrapper! {
75  #[doc = "`java.lang.Integer`"]
76  class Integer {
77    static Integer valueOf(i32);
78    (i32) intValue();
79  }
80}
81
82wrapper! {
83  #[doc = "`java.lang.Long`"]
84  class Long {
85    static Long valueOf(i64);
86    (i64) longValue();
87  }
88}
89
90wrapper! {
91  #[doc = "`java.lang.Float`"]
92  class Float {
93    static Float valueOf(f32);
94    (f32) floatValue();
95  }
96}
97
98wrapper! {
99  #[doc = "`java.lang.Double`"]
100  class Double {
101    static Double valueOf(f64);
102    (f64) doubleValue();
103  }
104}
105
106wrapper! {
107  #[doc = "`java.lang.Bool`"]
108  class Bool {
109    static Bool valueOf(bool);
110    (bool) boolValue();
111  }
112}
113
114wrapper! {
115  #[doc = "`java.lang.Char`"]
116  class Char {
117    static Char valueOf(u16);
118    (u16) boolValue();
119  }
120}