Struct jni::AttachGuard
source · [−]pub struct AttachGuard<'a> { /* private fields */ }
Expand description
A RAII implementation of scoped guard which detaches the current thread
when dropped. The attached JNIEnv
can be accessed through this guard
via its Deref
implementation.
Methods from Deref<Target = JNIEnv<'a>>
sourcepub fn get_version(&self) -> Result<JNIVersion>
pub fn get_version(&self) -> Result<JNIVersion>
Get the java version that we’re being executed from.
sourcepub fn define_class<S>(
&self,
name: S,
loader: JObject<'a>,
buf: &[u8]
) -> Result<JClass<'a>> where
S: Into<JNIString>,
pub fn define_class<S>(
&self,
name: S,
loader: JObject<'a>,
buf: &[u8]
) -> Result<JClass<'a>> where
S: Into<JNIString>,
Load a class from a buffer of raw class data. The name of the class must match the name encoded within the class file data.
sourcepub fn define_unnamed_class<S>(
&self,
loader: JObject<'a>,
buf: &[u8]
) -> Result<JClass<'a>> where
S: Into<JNIString>,
pub fn define_unnamed_class<S>(
&self,
loader: JObject<'a>,
buf: &[u8]
) -> Result<JClass<'a>> where
S: Into<JNIString>,
Load a class from a buffer of raw class data. The name of the class is inferred from the buffer.
sourcepub fn get_superclass<'c, T>(&self, class: T) -> Result<JClass<'a>> where
T: Desc<'a, JClass<'c>>,
pub fn get_superclass<'c, T>(&self, class: T) -> Result<JClass<'a>> where
T: Desc<'a, JClass<'c>>,
Returns the superclass for a particular class OR JObject::null()
for java.lang.Object
or
an interface. As with find_class
, takes a descriptor.
sourcepub fn is_assignable_from<'t, 'u, T, U>(
&self,
class1: T,
class2: U
) -> Result<bool> where
T: Desc<'a, JClass<'t>>,
U: Desc<'a, JClass<'u>>,
pub fn is_assignable_from<'t, 'u, T, U>(
&self,
class1: T,
class2: U
) -> Result<bool> where
T: Desc<'a, JClass<'t>>,
U: Desc<'a, JClass<'u>>,
Tests whether class1 is assignable from class2.
sourcepub fn is_instance_of<'c, O, T>(&self, object: O, class: T) -> Result<bool> where
O: Into<JObject<'a>>,
T: Desc<'a, JClass<'c>>,
pub fn is_instance_of<'c, O, T>(&self, object: O, class: T) -> Result<bool> where
O: Into<JObject<'a>>,
T: Desc<'a, JClass<'c>>,
Returns true if the object reference can be cast to the given type.
NB: Unlike the operator instanceof
, function IsInstanceOf
returns true
for all classes if object
is null
.
See JNI documentation for details.
sourcepub fn is_same_object<'b, 'c, O, T>(&self, ref1: O, ref2: T) -> Result<bool> where
O: Into<JObject<'b>>,
T: Into<JObject<'c>>,
pub fn is_same_object<'b, 'c, O, T>(&self, ref1: O, ref2: T) -> Result<bool> where
O: Into<JObject<'b>>,
T: Into<JObject<'c>>,
Returns true if ref1 and ref2 refer to the same Java object, or are both NULL
. Otherwise,
returns false.
sourcepub fn throw<'e, E>(&self, obj: E) -> Result<()> where
E: Desc<'a, JThrowable<'e>>,
pub fn throw<'e, E>(&self, obj: E) -> Result<()> where
E: Desc<'a, JThrowable<'e>>,
Raise an exception from an existing object. This will continue being
thrown in java unless exception_clear
is called.
Examples
let _ = env.throw(("java/lang/Exception", "something bad happened"));
Defaulting to “java/lang/Exception”:
let _ = env.throw("something bad happened");
sourcepub fn throw_new<'c, S, T>(&self, class: T, msg: S) -> Result<()> where
S: Into<JNIString>,
T: Desc<'a, JClass<'c>>,
pub fn throw_new<'c, S, T>(&self, class: T, msg: S) -> Result<()> where
S: Into<JNIString>,
T: Desc<'a, JClass<'c>>,
Create and throw a new exception from a class descriptor and an error message.
Example
let _ = env.throw_new("java/lang/Exception", "something bad happened");
sourcepub fn exception_occurred(&self) -> Result<JThrowable<'a>>
pub fn exception_occurred(&self) -> Result<JThrowable<'a>>
Check whether or not an exception is currently in the process of being
thrown. An exception is in this state from the time it gets thrown and
not caught in a java function until exception_clear
is called.
sourcepub fn exception_describe(&self) -> Result<()>
pub fn exception_describe(&self) -> Result<()>
Print exception information to the console.
sourcepub fn exception_clear(&self) -> Result<()>
pub fn exception_clear(&self) -> Result<()>
Clear an exception in the process of being thrown. If this is never called, the exception will continue being thrown when control is returned to java.
sourcepub fn fatal_error<S: Into<JNIString>>(&self, msg: S) -> !
pub fn fatal_error<S: Into<JNIString>>(&self, msg: S) -> !
Abort the JVM with an error message.
sourcepub fn exception_check(&self) -> Result<bool>
pub fn exception_check(&self) -> Result<bool>
Check to see if an exception is being thrown. This only differs from
exception_occurred
in that it doesn’t return the actual thrown
exception.
sourcepub fn new_direct_byte_buffer(&self, data: &mut [u8]) -> Result<JByteBuffer<'a>>
pub fn new_direct_byte_buffer(&self, data: &mut [u8]) -> Result<JByteBuffer<'a>>
Create a new instance of a direct java.nio.ByteBuffer.
sourcepub fn get_direct_buffer_address(
&self,
buf: JByteBuffer<'_>
) -> Result<&mut [u8]>
pub fn get_direct_buffer_address(
&self,
buf: JByteBuffer<'_>
) -> Result<&mut [u8]>
Returns the starting address of the memory of the direct java.nio.ByteBuffer.
sourcepub fn get_direct_buffer_capacity(&self, buf: JByteBuffer<'_>) -> Result<jlong>
pub fn get_direct_buffer_capacity(&self, buf: JByteBuffer<'_>) -> Result<jlong>
Returns the capacity of the direct java.nio.ByteBuffer.
sourcepub fn new_global_ref<O>(&self, obj: O) -> Result<GlobalRef> where
O: Into<JObject<'a>>,
pub fn new_global_ref<O>(&self, obj: O) -> Result<GlobalRef> where
O: Into<JObject<'a>>,
Turns an object into a global ref. This has the benefit of removing the lifetime bounds since it’s guaranteed to not get GC’d by java. It releases the GC pin upon being dropped.
sourcepub fn new_local_ref<T>(&self, obj: JObject<'a>) -> Result<JObject<'a>>
pub fn new_local_ref<T>(&self, obj: JObject<'a>) -> Result<JObject<'a>>
Create a new local ref to an object.
Note that the object passed to this is already a local ref. This creates yet another reference to it, which is most likely not what you want.
sourcepub fn auto_local<'b, O>(&'b self, obj: O) -> AutoLocal<'a, 'b> where
O: Into<JObject<'a>>,
pub fn auto_local<'b, O>(&'b self, obj: O) -> AutoLocal<'a, 'b> where
O: Into<JObject<'a>>,
Creates a new auto-deleted local reference.
See also with_local_frame
method that
can be more convenient when you create a bounded number of local references
but cannot rely on automatic de-allocation (e.g., in case of recursion, deep call stacks,
permanently-attached native threads, etc.).
sourcepub fn delete_local_ref(&self, obj: JObject<'_>) -> Result<()>
pub fn delete_local_ref(&self, obj: JObject<'_>) -> Result<()>
Deletes the local reference.
Local references are valid for the duration of a native method call. They are freed automatically after the native method returns. Each local reference costs some amount of Java Virtual Machine resource. Programmers need to make sure that native methods do not excessively allocate local references. Although local references are automatically freed after the native method returns to Java, excessive allocation of local references may cause the VM to run out of memory during the execution of a native method.
In most cases it is better to use AutoLocal
(see auto_local
method)
or with_local_frame
instead of direct delete_local_ref
calls.
sourcepub fn push_local_frame(&self, capacity: i32) -> Result<()>
pub fn push_local_frame(&self, capacity: i32) -> Result<()>
Creates a new local reference frame, in which at least a given number of local references can be created.
Returns Err
on failure, with a pending OutOfMemoryError
.
Prefer to use with_local_frame
instead of
direct push_local_frame
/pop_local_frame
calls.
See also auto_local
method
and AutoLocal
type — that approach can be more convenient in loops.
sourcepub fn pop_local_frame(&self, result: JObject<'a>) -> Result<JObject<'a>>
pub fn pop_local_frame(&self, result: JObject<'a>) -> Result<JObject<'a>>
Pops off the current local reference frame, frees all the local
references allocated on the current stack frame, except the result
,
which is returned from this function and remains valid.
The resulting JObject
will be NULL
iff result
is NULL
.
sourcepub fn with_local_frame<F>(&self, capacity: i32, f: F) -> Result<JObject<'a>> where
F: FnOnce() -> Result<JObject<'a>>,
pub fn with_local_frame<F>(&self, capacity: i32, f: F) -> Result<JObject<'a>> where
F: FnOnce() -> Result<JObject<'a>>,
Executes the given function in a new local reference frame, in which at least a given number of references can be created. Once this method returns, all references allocated in the frame are freed, except the one that the function returns, which remains valid.
If no new frames can be allocated, returns Err
with a pending OutOfMemoryError
.
See also auto_local
method
and AutoLocal
type - that approach can be more convenient in loops.
sourcepub fn alloc_object<'c, T>(&self, class: T) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
pub fn alloc_object<'c, T>(&self, class: T) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
Allocates a new object from a class descriptor without running a constructor.
sourcepub fn get_method_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JMethodID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
pub fn get_method_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JMethodID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
Look up a method by class descriptor, name, and signature.
Example
let method_id: JMethodID =
env.get_method_id("java/lang/String", "substring", "(II)Ljava/lang/String;");
sourcepub fn get_static_method_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JStaticMethodID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
pub fn get_static_method_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JStaticMethodID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
Look up a static method by class descriptor, name, and signature.
Example
let method_id: JMethodID =
env.get_static_method_id("java/lang/String", "valueOf", "(I)Ljava/lang/String;");
sourcepub fn get_field_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JFieldID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
pub fn get_field_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JFieldID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
Look up the field ID for a class/name/type combination.
Example
let field_id = env.get_field_id("com/my/Class", "intField", "I");
sourcepub fn get_static_field_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JStaticFieldID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
pub fn get_static_field_id<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V
) -> Result<JStaticFieldID<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString>,
Look up the static field ID for a class/name/type combination.
Example
let field_id = env.get_static_field_id("com/my/Class", "intField", "I");
sourcepub fn get_object_class<'b, O>(&self, obj: O) -> Result<JClass<'a>> where
O: Into<JObject<'b>>,
pub fn get_object_class<'b, O>(&self, obj: O) -> Result<JClass<'a>> where
O: Into<JObject<'b>>,
Get the class for an object.
sourcepub fn call_static_method_unchecked<'c, 'm, T, U>(
&self,
class: T,
method_id: U,
ret: JavaType,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticMethodID<'m>>,
pub fn call_static_method_unchecked<'c, 'm, T, U>(
&self,
class: T,
method_id: U,
ret: JavaType,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticMethodID<'m>>,
Call a static method in an unsafe manner. This does nothing to check whether the method is valid to call on the class, whether the return type is correct, or whether the number of args is valid for the method.
Under the hood, this simply calls the CallStatic<Type>MethodA
method
with the provided arguments.
sourcepub fn call_method_unchecked<'m, O, T>(
&self,
obj: O,
method_id: T,
ret: JavaType,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
T: Desc<'a, JMethodID<'m>>,
pub fn call_method_unchecked<'m, O, T>(
&self,
obj: O,
method_id: T,
ret: JavaType,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
T: Desc<'a, JMethodID<'m>>,
Call an object method in an unsafe manner. This does nothing to check whether the method is valid to call on the object, whether the return type is correct, or whether the number of args is valid for the method.
Under the hood, this simply calls the Call<Type>MethodA
method with
the provided arguments.
sourcepub fn call_method<O, S, T>(
&self,
obj: O,
name: S,
sig: T,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
pub fn call_method<O, S, T>(
&self,
obj: O,
name: S,
sig: T,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
Calls an object method safely. This comes with a number of lookups/checks. It
- Parses the type signature to find the number of arguments and return type
- Looks up the JClass for the given object.
- Looks up the JMethodID for the class/name/signature combination
- Ensures that the number of args matches the signature
- Calls
call_method_unchecked
with the verified safe arguments.
Note: this may cause a java exception if the arguments are the wrong type, in addition to if the method itself throws.
sourcepub fn call_static_method<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString> + AsRef<str>,
pub fn call_static_method<'c, T, U, V>(
&self,
class: T,
name: U,
sig: V,
args: &[JValue<'_>]
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString> + AsRef<str>,
Calls a static method safely. This comes with a number of lookups/checks. It
- Parses the type signature to find the number of arguments and return type
- Looks up the JMethodID for the class/name/signature combination
- Ensures that the number of args matches the signature
- Calls
call_method_unchecked
with the verified safe arguments.
Note: this may cause a java exception if the arguments are the wrong type, in addition to if the method itself throws.
sourcepub fn new_object<'c, T, U>(
&self,
class: T,
ctor_sig: U,
ctor_args: &[JValue<'_>]
) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString> + AsRef<str>,
pub fn new_object<'c, T, U>(
&self,
class: T,
ctor_sig: U,
ctor_args: &[JValue<'_>]
) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString> + AsRef<str>,
Create a new object using a constructor. This is done safely using
checks similar to those in call_static_method
.
sourcepub fn new_object_unchecked<'c, T>(
&self,
class: T,
ctor_id: JMethodID<'_>,
ctor_args: &[JValue<'_>]
) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
pub fn new_object_unchecked<'c, T>(
&self,
class: T,
ctor_id: JMethodID<'_>,
ctor_args: &[JValue<'_>]
) -> Result<JObject<'a>> where
T: Desc<'a, JClass<'c>>,
Create a new object using a constructor. Arguments aren’t checked
because
of the JMethodID
usage.
sourcepub fn get_list(&self, obj: JObject<'a>) -> Result<JList<'a, '_>>
pub fn get_list(&self, obj: JObject<'a>) -> Result<JList<'a, '_>>
Cast a JObject to a JList
. This won’t throw exceptions or return errors
in the event that the object isn’t actually a list, but the methods on
the resulting map object will.
sourcepub fn get_map(&self, obj: JObject<'a>) -> Result<JMap<'a, '_>>
pub fn get_map(&self, obj: JObject<'a>) -> Result<JMap<'a, '_>>
Cast a JObject to a JMap. This won’t throw exceptions or return errors in the event that the object isn’t actually a map, but the methods on the resulting map object will.
sourcepub fn get_string(&self, obj: JString<'a>) -> Result<JavaStr<'a, '_>>
pub fn get_string(&self, obj: JString<'a>) -> Result<JavaStr<'a, '_>>
Get a JavaStr from a JString. This allows conversions from java string objects to rust strings.
This entails a call to GetStringUTFChars
and only decodes java’s
modified UTF-8 format on conversion to a rust-compatible string.
sourcepub fn get_string_utf_chars(&self, obj: JString<'_>) -> Result<*const c_char>
pub fn get_string_utf_chars(&self, obj: JString<'_>) -> Result<*const c_char>
Get a pointer to the character array beneath a JString.
Array contains Java’s modified UTF-8.
Attention
This will leak memory if release_string_utf_chars
is never called.
sourcepub fn release_string_utf_chars(
&self,
obj: JString<'_>,
arr: *const c_char
) -> Result<()>
pub fn release_string_utf_chars(
&self,
obj: JString<'_>,
arr: *const c_char
) -> Result<()>
Unpin the array returned by get_string_utf_chars
.
sourcepub fn new_string<S: Into<JNIString>>(&self, from: S) -> Result<JString<'a>>
pub fn new_string<S: Into<JNIString>>(&self, from: S) -> Result<JString<'a>>
Create a new java string object from a rust string. This requires a re-encoding of rusts real UTF-8 strings to java’s modified UTF-8 format.
sourcepub fn get_array_length(&self, array: jarray) -> Result<jsize>
pub fn get_array_length(&self, array: jarray) -> Result<jsize>
Get the length of a java array
sourcepub fn new_object_array<'c, T, U>(
&self,
length: jsize,
element_class: T,
initial_element: U
) -> Result<jobjectArray> where
T: Desc<'a, JClass<'c>>,
U: Into<JObject<'a>>,
pub fn new_object_array<'c, T, U>(
&self,
length: jsize,
element_class: T,
initial_element: U
) -> Result<jobjectArray> where
T: Desc<'a, JClass<'c>>,
U: Into<JObject<'a>>,
Construct a new array holding objects in class element_class
.
All elements are initially set to initial_element
.
This function returns a local reference, that must not be allocated excessively. See Java documentation for details.
sourcepub fn get_object_array_element(
&self,
array: jobjectArray,
index: jsize
) -> Result<JObject<'a>>
pub fn get_object_array_element(
&self,
array: jobjectArray,
index: jsize
) -> Result<JObject<'a>>
Returns an element of the jobjectArray
array.
sourcepub fn set_object_array_element<O>(
&self,
array: jobjectArray,
index: jsize,
value: O
) -> Result<()> where
O: Into<JObject<'a>>,
pub fn set_object_array_element<O>(
&self,
array: jobjectArray,
index: jsize,
value: O
) -> Result<()> where
O: Into<JObject<'a>>,
Sets an element of the jobjectArray
array.
sourcepub fn byte_array_from_slice(&self, buf: &[u8]) -> Result<jbyteArray>
pub fn byte_array_from_slice(&self, buf: &[u8]) -> Result<jbyteArray>
Create a new java byte array from a rust byte slice.
sourcepub fn convert_byte_array(&self, array: jbyteArray) -> Result<Vec<u8>>
pub fn convert_byte_array(&self, array: jbyteArray) -> Result<Vec<u8>>
Converts a java byte array to a rust vector of bytes.
sourcepub fn new_boolean_array(&self, length: jsize) -> Result<jbooleanArray>
pub fn new_boolean_array(&self, length: jsize) -> Result<jbooleanArray>
Create a new java boolean array of supplied length.
sourcepub fn new_byte_array(&self, length: jsize) -> Result<jbyteArray>
pub fn new_byte_array(&self, length: jsize) -> Result<jbyteArray>
Create a new java byte array of supplied length.
sourcepub fn new_char_array(&self, length: jsize) -> Result<jcharArray>
pub fn new_char_array(&self, length: jsize) -> Result<jcharArray>
Create a new java char array of supplied length.
sourcepub fn new_short_array(&self, length: jsize) -> Result<jshortArray>
pub fn new_short_array(&self, length: jsize) -> Result<jshortArray>
Create a new java short array of supplied length.
sourcepub fn new_int_array(&self, length: jsize) -> Result<jintArray>
pub fn new_int_array(&self, length: jsize) -> Result<jintArray>
Create a new java int array of supplied length.
sourcepub fn new_long_array(&self, length: jsize) -> Result<jlongArray>
pub fn new_long_array(&self, length: jsize) -> Result<jlongArray>
Create a new java long array of supplied length.
sourcepub fn new_float_array(&self, length: jsize) -> Result<jfloatArray>
pub fn new_float_array(&self, length: jsize) -> Result<jfloatArray>
Create a new java float array of supplied length.
sourcepub fn new_double_array(&self, length: jsize) -> Result<jdoubleArray>
pub fn new_double_array(&self, length: jsize) -> Result<jdoubleArray>
Create a new java double array of supplied length.
sourcepub fn get_boolean_array_region(
&self,
array: jbooleanArray,
start: jsize,
buf: &mut [jboolean]
) -> Result<()>
pub fn get_boolean_array_region(
&self,
array: jbooleanArray,
start: jsize,
buf: &mut [jboolean]
) -> Result<()>
Copy elements of the java boolean array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_byte_array_region(
&self,
array: jbyteArray,
start: jsize,
buf: &mut [jbyte]
) -> Result<()>
pub fn get_byte_array_region(
&self,
array: jbyteArray,
start: jsize,
buf: &mut [jbyte]
) -> Result<()>
Copy elements of the java byte array from the start
index to the buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_char_array_region(
&self,
array: jcharArray,
start: jsize,
buf: &mut [jchar]
) -> Result<()>
pub fn get_char_array_region(
&self,
array: jcharArray,
start: jsize,
buf: &mut [jchar]
) -> Result<()>
Copy elements of the java char array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_short_array_region(
&self,
array: jshortArray,
start: jsize,
buf: &mut [jshort]
) -> Result<()>
pub fn get_short_array_region(
&self,
array: jshortArray,
start: jsize,
buf: &mut [jshort]
) -> Result<()>
Copy elements of the java short array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_int_array_region(
&self,
array: jintArray,
start: jsize,
buf: &mut [jint]
) -> Result<()>
pub fn get_int_array_region(
&self,
array: jintArray,
start: jsize,
buf: &mut [jint]
) -> Result<()>
Copy elements of the java int array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_long_array_region(
&self,
array: jlongArray,
start: jsize,
buf: &mut [jlong]
) -> Result<()>
pub fn get_long_array_region(
&self,
array: jlongArray,
start: jsize,
buf: &mut [jlong]
) -> Result<()>
Copy elements of the java long array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_float_array_region(
&self,
array: jfloatArray,
start: jsize,
buf: &mut [jfloat]
) -> Result<()>
pub fn get_float_array_region(
&self,
array: jfloatArray,
start: jsize,
buf: &mut [jfloat]
) -> Result<()>
Copy elements of the java float array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn get_double_array_region(
&self,
array: jdoubleArray,
start: jsize,
buf: &mut [jdouble]
) -> Result<()>
pub fn get_double_array_region(
&self,
array: jdoubleArray,
start: jsize,
buf: &mut [jdouble]
) -> Result<()>
Copy elements of the java double array from the start
index to the
buf
slice. The number of copied elements is equal to the buf
length.
Errors
If start
is negative or start + buf.len()
is greater than array.length
then no elements are copied, an ArrayIndexOutOfBoundsException
is thrown,
and Err
is returned.
sourcepub fn set_boolean_array_region(
&self,
array: jbooleanArray,
start: jsize,
buf: &[jboolean]
) -> Result<()>
pub fn set_boolean_array_region(
&self,
array: jbooleanArray,
start: jsize,
buf: &[jboolean]
) -> Result<()>
Copy the contents of the buf
slice to the java boolean array at the
start
index.
sourcepub fn set_byte_array_region(
&self,
array: jbyteArray,
start: jsize,
buf: &[jbyte]
) -> Result<()>
pub fn set_byte_array_region(
&self,
array: jbyteArray,
start: jsize,
buf: &[jbyte]
) -> Result<()>
Copy the contents of the buf
slice to the java byte array at the
start
index.
sourcepub fn set_char_array_region(
&self,
array: jcharArray,
start: jsize,
buf: &[jchar]
) -> Result<()>
pub fn set_char_array_region(
&self,
array: jcharArray,
start: jsize,
buf: &[jchar]
) -> Result<()>
Copy the contents of the buf
slice to the java char array at the
start
index.
sourcepub fn set_short_array_region(
&self,
array: jshortArray,
start: jsize,
buf: &[jshort]
) -> Result<()>
pub fn set_short_array_region(
&self,
array: jshortArray,
start: jsize,
buf: &[jshort]
) -> Result<()>
Copy the contents of the buf
slice to the java short array at the
start
index.
sourcepub fn set_int_array_region(
&self,
array: jintArray,
start: jsize,
buf: &[jint]
) -> Result<()>
pub fn set_int_array_region(
&self,
array: jintArray,
start: jsize,
buf: &[jint]
) -> Result<()>
Copy the contents of the buf
slice to the java int array at the
start
index.
sourcepub fn set_long_array_region(
&self,
array: jlongArray,
start: jsize,
buf: &[jlong]
) -> Result<()>
pub fn set_long_array_region(
&self,
array: jlongArray,
start: jsize,
buf: &[jlong]
) -> Result<()>
Copy the contents of the buf
slice to the java long array at the
start
index.
sourcepub fn set_float_array_region(
&self,
array: jfloatArray,
start: jsize,
buf: &[jfloat]
) -> Result<()>
pub fn set_float_array_region(
&self,
array: jfloatArray,
start: jsize,
buf: &[jfloat]
) -> Result<()>
Copy the contents of the buf
slice to the java float array at the
start
index.
sourcepub fn set_double_array_region(
&self,
array: jdoubleArray,
start: jsize,
buf: &[jdouble]
) -> Result<()>
pub fn set_double_array_region(
&self,
array: jdoubleArray,
start: jsize,
buf: &[jdouble]
) -> Result<()>
Copy the contents of the buf
slice to the java double array at the
start
index.
sourcepub fn get_field_unchecked<'f, O, T>(
&self,
obj: O,
field: T,
ty: JavaType
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
T: Desc<'a, JFieldID<'f>>,
pub fn get_field_unchecked<'f, O, T>(
&self,
obj: O,
field: T,
ty: JavaType
) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
T: Desc<'a, JFieldID<'f>>,
Get a field without checking the provided type against the actual field.
sourcepub fn set_field_unchecked<'f, O, T>(
&self,
obj: O,
field: T,
val: JValue<'_>
) -> Result<()> where
O: Into<JObject<'a>>,
T: Desc<'a, JFieldID<'f>>,
pub fn set_field_unchecked<'f, O, T>(
&self,
obj: O,
field: T,
val: JValue<'_>
) -> Result<()> where
O: Into<JObject<'a>>,
T: Desc<'a, JFieldID<'f>>,
Set a field without any type checking.
sourcepub fn get_field<O, S, T>(&self, obj: O, name: S, ty: T) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
pub fn get_field<O, S, T>(&self, obj: O, name: S, ty: T) -> Result<JValue<'a>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
Get a field. Requires an object class lookup and a field id lookup internally.
sourcepub fn set_field<O, S, T>(
&self,
obj: O,
name: S,
ty: T,
val: JValue<'_>
) -> Result<()> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
pub fn set_field<O, S, T>(
&self,
obj: O,
name: S,
ty: T,
val: JValue<'_>
) -> Result<()> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Into<JNIString> + AsRef<str>,
Set a field. Does the same lookups as get_field
and ensures that the
type matches the given value.
sourcepub fn get_static_field_unchecked<'c, 'f, T, U>(
&self,
class: T,
field: U,
ty: JavaType
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticFieldID<'f>>,
pub fn get_static_field_unchecked<'c, 'f, T, U>(
&self,
class: T,
field: U,
ty: JavaType
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticFieldID<'f>>,
Get a static field without checking the provided type against the actual field.
sourcepub fn get_static_field<'c, T, U, V>(
&self,
class: T,
field: U,
sig: V
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString> + AsRef<str>,
pub fn get_static_field<'c, T, U, V>(
&self,
class: T,
field: U,
sig: V
) -> Result<JValue<'a>> where
T: Desc<'a, JClass<'c>>,
U: Into<JNIString>,
V: Into<JNIString> + AsRef<str>,
Get a static field. Requires a class lookup and a field id lookup internally.
sourcepub fn set_static_field<'c, 'f, T, U>(
&self,
class: T,
field: U,
value: JValue<'_>
) -> Result<()> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticFieldID<'f>>,
pub fn set_static_field<'c, 'f, T, U>(
&self,
class: T,
field: U,
value: JValue<'_>
) -> Result<()> where
T: Desc<'a, JClass<'c>>,
U: Desc<'a, JStaticFieldID<'f>>,
Set a static field. Requires a class lookup and a field id lookup internally.
sourcepub fn set_rust_field<O, S, T>(
&self,
obj: O,
field: S,
rust_object: T
) -> Result<()> where
O: Into<JObject<'a>>,
S: AsRef<str>,
T: Send + 'static,
pub fn set_rust_field<O, S, T>(
&self,
obj: O,
field: S,
rust_object: T
) -> Result<()> where
O: Into<JObject<'a>>,
S: AsRef<str>,
T: Send + 'static,
Surrenders ownership of a rust object to Java. Requires an object with a
long
field to store the pointer. The Rust value will be wrapped in a
Mutex since Java will be controlling where it’ll be used thread-wise.
Unsafe because it leaks memory if take_rust_field
is never called (so
be sure to make a finalizer).
DO NOT make a copy of the object containing one of these fields. If you’ve set up a finalizer to pass it back to Rust upon being GC’d, it will point to invalid memory and will likely attempt to be deallocated again.
sourcepub fn get_rust_field<O, S, T>(
&self,
obj: O,
field: S
) -> Result<MutexGuard<'_, T>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Send + 'static,
pub fn get_rust_field<O, S, T>(
&self,
obj: O,
field: S
) -> Result<MutexGuard<'_, T>> where
O: Into<JObject<'a>>,
S: Into<JNIString>,
T: Send + 'static,
Gets a lock on a Rust value that’s been given to a Java object. Java
still retains ownership and take_rust_field
will still need to be
called at some point. Checks for a null pointer, but assumes that the
data it points to is valid for T.
sourcepub fn take_rust_field<O, S, T>(&self, obj: O, field: S) -> Result<T> where
O: Into<JObject<'a>>,
S: AsRef<str>,
T: Send + 'static,
pub fn take_rust_field<O, S, T>(&self, obj: O, field: S) -> Result<T> where
O: Into<JObject<'a>>,
S: AsRef<str>,
T: Send + 'static,
Take a Rust field back from Java. Makes sure that the pointer is non-null, but still assumes that the data it points to is valid for T. Sets the field to a null pointer to signal that it’s empty.
This will return an error in the event that there’s an outstanding lock on the object.
sourcepub fn lock_obj<O>(&self, obj: O) -> Result<MonitorGuard<'a>> where
O: Into<JObject<'a>>,
pub fn lock_obj<O>(&self, obj: O) -> Result<MonitorGuard<'a>> where
O: Into<JObject<'a>>,
Lock a Java object. The MonitorGuard that this returns is responsible for ensuring that it gets unlocked.
sourcepub fn get_native_interface(&self) -> *mut JNIEnv
pub fn get_native_interface(&self) -> *mut JNIEnv
Returns underlying sys::JNIEnv
interface.
sourcepub fn get_java_vm(&self) -> Result<JavaVM>
pub fn get_java_vm(&self) -> Result<JavaVM>
Returns the Java VM interface.
sourcepub fn ensure_local_capacity(&self, capacity: jint) -> Result<()>
pub fn ensure_local_capacity(&self, capacity: jint) -> Result<()>
Ensures that at least a given number of local references can be created in the current thread.
sourcepub fn register_native_methods<'c, T>(
&self,
class: T,
methods: &[NativeMethod]
) -> Result<()> where
T: Desc<'a, JClass<'c>>,
pub fn register_native_methods<'c, T>(
&self,
class: T,
methods: &[NativeMethod]
) -> Result<()> where
T: Desc<'a, JClass<'c>>,
Bind function pointers to native methods of class according to method name and signature. For details see documentation.
sourcepub fn unregister_native_methods<'c, T>(&self, class: T) -> Result<()> where
T: Desc<'a, JClass<'c>>,
pub fn unregister_native_methods<'c, T>(&self, class: T) -> Result<()> where
T: Desc<'a, JClass<'c>>,
Unbind all native methods of class.
sourcepub fn get_array_elements<T: TypeArray>(
&self,
array: jarray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, T>>
pub fn get_array_elements<T: TypeArray>(
&self,
array: jarray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, T>>
Return an AutoArray of the given Java array.
The result is valid until the AutoArray object goes out of scope, when the release happens automatically according to the mode parameter.
Since the returned array may be a copy of the Java array, changes made to the
returned array will not necessarily be reflected in the original array until
the corresponding Release*ArrayElements JNI method is called.
AutoArray has a commit() method, to force a copy of the array if needed (and without
releasing it).
Prefer to use the convenience wrappers:
get_int_array_elements
get_long_array_elements
get_byte_array_elements
get_boolean_array_elements
get_char_array_elements
get_short_array_elements
get_float_array_elements
get_double_array_elements
And the associated AutoArray
struct.
sourcepub fn get_int_array_elements(
&self,
array: jintArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jint>>
pub fn get_int_array_elements(
&self,
array: jintArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jint>>
See also get_array_elements
sourcepub fn get_long_array_elements(
&self,
array: jlongArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jlong>>
pub fn get_long_array_elements(
&self,
array: jlongArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jlong>>
See also get_array_elements
sourcepub fn get_byte_array_elements(
&self,
array: jbyteArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jbyte>>
pub fn get_byte_array_elements(
&self,
array: jbyteArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jbyte>>
See also get_array_elements
sourcepub fn get_boolean_array_elements(
&self,
array: jbooleanArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jboolean>>
pub fn get_boolean_array_elements(
&self,
array: jbooleanArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jboolean>>
See also get_array_elements
sourcepub fn get_char_array_elements(
&self,
array: jcharArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jchar>>
pub fn get_char_array_elements(
&self,
array: jcharArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jchar>>
See also get_array_elements
sourcepub fn get_short_array_elements(
&self,
array: jshortArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jshort>>
pub fn get_short_array_elements(
&self,
array: jshortArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jshort>>
See also get_array_elements
sourcepub fn get_float_array_elements(
&self,
array: jfloatArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jfloat>>
pub fn get_float_array_elements(
&self,
array: jfloatArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jfloat>>
See also get_array_elements
sourcepub fn get_double_array_elements(
&self,
array: jdoubleArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jdouble>>
pub fn get_double_array_elements(
&self,
array: jdoubleArray,
mode: ReleaseMode
) -> Result<AutoArray<'_, '_, jdouble>>
See also get_array_elements
sourcepub fn get_primitive_array_critical(
&self,
array: jarray,
mode: ReleaseMode
) -> Result<AutoPrimitiveArray<'_, '_>>
pub fn get_primitive_array_critical(
&self,
array: jarray,
mode: ReleaseMode
) -> Result<AutoPrimitiveArray<'_, '_>>
Return an AutoPrimitiveArray of the given Java primitive array.
The result is valid until the corresponding AutoPrimitiveArray object goes out of scope, when the release happens automatically according to the mode parameter.
Given that Critical sections must be as short as possible, and that they come with a number of important restrictions (see GetPrimitiveArrayCritical JNI doc), use this wrapper wisely, to avoid holding the array longer that strictly necessary. In any case, you can:
- Use std::mem::drop explicitly, to force / anticipate resource release.
- Use a nested scope, to release the array at the nested scope’s exit.
Since the returned array may be a copy of the Java array, changes made to the returned array will not necessarily be reflected in the original array until ReleasePrimitiveArrayCritical is called; which happens at AutoPrimitiveArray destruction.
If the given array is null
, an Error::NullPtr
is returned.
See also get_byte_array_elements
Trait Implementations
sourceimpl<'a> Deref for AttachGuard<'a>
impl<'a> Deref for AttachGuard<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for AttachGuard<'a>
impl<'a> !Send for AttachGuard<'a>
impl<'a> !Sync for AttachGuard<'a>
impl<'a> Unpin for AttachGuard<'a>
impl<'a> UnwindSafe for AttachGuard<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more