I am using the Rust jni crate, and currently have a native method defined. I am trying to figure out how I get a java field, which is a different class that I have also made, and interact with it inside my Rust code.
I have tried the following based on the small amount of documentation I could find for this crate and the JNI in general:
#[no_mangle]
pub extern "system" fn Java_name__11nteresting_example_FirstClass_example<'local>(mut env: JNIEnv<'local>, class: JClass<'local>) {
let field_id: jni::objects::JFieldID = env.get_field_id(class, "fieldName", "Lname/_1nteresting/example/SecondClass;").expect("Unable to get field ID");
let field = env.get_field(class, "fieldName", field_id);
}
Currently, I am getting the error:
the trait bound `JFieldID: AsRef<str>` is not satisfied
the trait `AsRef<str>` is not implemented for `JFieldID`
but trait `AsRef<JFieldID>` is implemented for it
for that trait implementation, expected `JFieldID`, found `str`
on the field_id
when I give it to the get_field
function. I assume that I probably have misread what I could find about this, although the jni crate doesn't seem to have much documentation about this.
From your code it looks like you're trying to access class field (static
in Java), in which case you want to use get_static_field
instead.
The documentation for get_static_field
states that it does an class lookup and a field id lookup internally.
So you can write your code as:
env.get_static_field(class, "fieldName", "Lname/_1nteresting/example/SecondClass;");
If you wanted to access an instance field, you need to have an object in hand and use get_field
instead.