Skip to content

Commit

Permalink
Write set_static_field
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasm-dev committed Jul 27, 2023
1 parent e9013be commit 89c5eb0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jnat"
version = "0.8.1"
version = "0.9.0"
edition = "2021"
license = "MIT"
description = "A wrapper around the jni crate"
Expand Down
22 changes: 21 additions & 1 deletion integration/tests/lib/static_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,27 @@ fn method(mut env: JNIEnv, _: JClass) {
let mut env = Env::new(&mut env);
let mut class = env.class("StaticField").unwrap();

if let Value::Int(value) = env.get_value(class.get_static_field("staticField", Type::Int).unwrap().borrow()) {
if let Value::Int(value) = env.get_value(
class
.get_static_field("staticField", Type::Int)
.unwrap()
.borrow(),
) {
println!("{}", value);
} else {
unreachable!();
}

class.set_static_field("staticField", Type::Int, Value::Int(1)).unwrap();

if let Value::Int(value) = env.get_value(
class
.get_static_field("staticField", Type::Int)
.unwrap()
.borrow(),
) {
println!("{}", value);
} else {
unreachable!();
}
}
2 changes: 1 addition & 1 deletion integration/tests/static_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::IntegrationTest;

fn test_static_field(s: String) -> bool {
return s == "0\n";
return s == "0\n1\n";
}

inventory::submit! {IntegrationTest {
Expand Down
44 changes: 41 additions & 3 deletions src/class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{env::Env, signature::Signature, value::Value, Type};
use jni::objects::{JClass, JObject, JValueGen};
use jni::objects::{JClass, JObject, JStaticFieldID, JValueGen};

/// A struct wrapping a JClass
pub struct Class<'a> {
Expand Down Expand Up @@ -70,9 +70,9 @@ impl<'a> Class<'a> {
}

/// Gets a static field on the class
///
///
/// # Arguments
///
///
/// * `name` - The name of the field
/// * `type` - The type of the field
pub fn get_static_field(
Expand All @@ -87,6 +87,44 @@ impl<'a> Class<'a> {
jni_env.get_static_field(class, name, r#type)
}

/// Sets a static field on the class
///
/// # Arguments
///
/// * `name` - The name of the field
/// * `value` - The value to set the field to
pub fn set_static_field(
&self,
name: &str,
r#type: Type,
value: Value,
) -> jni::errors::Result<()> {
let class = &self.class;
let value = self.env.value(value);
let field = self.get_static_field_id(name, r#type)?;

let mut jni_env = self.env.get_jni_env();
jni_env.set_static_field(class, field, value)
}

/// Get a static field ID on the class
///
/// # Arguments
///
/// * `name` - The name of the field
/// * `signature` - The signature of the field
pub fn get_static_field_id(
&self,
name: &str,
r#type: Type,
) -> jni::errors::Result<JStaticFieldID> {
let class = &self.class;
let r#type: String = r#type.to_string();

let mut jni_env = self.env.get_jni_env();
jni_env.get_static_field_id(class, name, r#type)
}

/// Get the wrapped class
pub fn get_class(self) -> JClass<'a> {
self.class
Expand Down

0 comments on commit 89c5eb0

Please sign in to comment.