Skip to main content

jnat/
class.rs

1use crate::{env::Env, signature::Signature, value::Value, Type};
2use jni::objects::{JClass, JObject, JStaticFieldID, JValueGen};
3
4/// A struct wrapping a JClass
5pub struct Class<'a> {
6  env: &'a Env<'a>,
7  class: JClass<'a>,
8}
9
10impl<'a> Class<'a> {
11  /// Creates a new Class
12  ///
13  /// # Arguments
14  ///
15  /// * `env` - The environment
16  /// * `class` - The JClass to wrap
17  pub fn new(env: &'a Env<'a>, class: JClass<'a>) -> Class<'a> {
18    Class { env, class }
19  }
20
21  /// Calls a static method on the class
22  ///
23  /// # Arguments
24  ///
25  /// * `name` - The name of the method
26  /// * `signature` - The signature of the method
27  /// * `args` - The arguments to pass to the method
28  pub fn call_static_method(
29    &self,
30    name: &str,
31    signature: Signature,
32    args: &[Value],
33  ) -> jni::errors::Result<JValueGen<JObject<'_>>> {
34    let class = &self.class;
35    let signature: String = signature.into();
36
37    let mut jni_env = self.env.get_jni_env();
38    jni_env.call_static_method(
39      class,
40      name,
41      signature,
42      args
43        .iter()
44        .map(|o| self.env.new_value(*o))
45        .collect::<Vec<JValueGen<&JObject>>>()
46        .as_slice(),
47    )
48  }
49
50  /// Creates an instance of the class
51  ///
52  /// # Arguments
53  ///
54  /// * `signature` - The signature of the constructor
55  /// * `args` - The arguments to pass to the constructor
56  pub fn create(&self, signature: Signature, args: &[Value]) -> jni::errors::Result<JObject> {
57    let class = &self.class;
58    let signature: String = signature.into();
59
60    let mut jni_env = self.env.get_jni_env();
61    jni_env.new_object(
62      class,
63      signature,
64      args
65        .iter()
66        .map(|o| self.env.new_value(*o))
67        .collect::<Vec<JValueGen<&JObject>>>()
68        .as_slice(),
69    )
70  }
71
72  /// Gets a static field on the class
73  ///
74  /// # Arguments
75  ///
76  /// * `name` - The name of the field
77  /// * `type` - The type of the field
78  pub fn get_static_field(
79    &self,
80    name: &str,
81    r#type: Type,
82  ) -> jni::errors::Result<JValueGen<JObject<'_>>> {
83    let class = &self.class;
84    let r#type: String = r#type.into();
85
86    let mut jni_env = self.env.get_jni_env();
87    jni_env.get_static_field(class, name, r#type)
88  }
89
90  /// Sets a static field on the class
91  ///
92  /// # Arguments
93  ///
94  /// * `name` - The name of the field
95  /// * `value` - The value to set the field to
96  pub fn set_static_field(
97    &self,
98    name: &str,
99    r#type: Type,
100    value: Value,
101  ) -> jni::errors::Result<()> {
102    let class = &self.class;
103    let value = self.env.new_value(value);
104    let field = self.get_static_field_id(name, r#type)?;
105
106    let mut jni_env = self.env.get_jni_env();
107    jni_env.set_static_field(class, field, value)
108  }
109
110  /// Get a static field ID on the class
111  ///
112  /// # Arguments
113  ///
114  /// * `name` - The name of the field
115  /// * `signature` - The signature of the field
116  pub fn get_static_field_id(
117    &self,
118    name: &str,
119    r#type: Type,
120  ) -> jni::errors::Result<JStaticFieldID> {
121    let class = &self.class;
122    let r#type: String = r#type.into();
123
124    let mut jni_env = self.env.get_jni_env();
125    jni_env.get_static_field_id(class, name, r#type)
126  }
127
128  /// Get the wrapped class
129  pub fn get_class(self) -> JClass<'a> {
130    self.class
131  }
132}