Struct 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>>§

Source

pub fn get_version(&self) -> Result<JNIVersion, Error>

Get the java version that we’re being executed from.

Source

pub fn define_class<S>( &self, name: S, loader: JObject<'a>, buf: &[u8], ) -> Result<JClass<'a>, Error>
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.

Source

pub fn define_unnamed_class<S>( &self, loader: JObject<'a>, buf: &[u8], ) -> Result<JClass<'a>, Error>
where S: Into<JNIString>,

Load a class from a buffer of raw class data. The name of the class is inferred from the buffer.

Source

pub fn find_class<S>(&self, name: S) -> Result<JClass<'a>, Error>
where S: Into<JNIString>,

Look up a class by name.

§Example
let class: JClass<'a> = env.find_class("java/lang/String");
Source

pub fn get_superclass<'c, T>(&self, class: T) -> Result<JClass<'a>, Error>
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.

Source

pub fn is_assignable_from<'t, 'u, T, U>( &self, class1: T, class2: U, ) -> Result<bool, Error>
where T: Desc<'a, JClass<'t>>, U: Desc<'a, JClass<'u>>,

Tests whether class1 is assignable from class2.

Source

pub fn is_instance_of<'c, O, T>( &self, object: O, class: T, ) -> Result<bool, Error>
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.

Source

pub fn is_same_object<'b, 'c, O, T>( &self, ref1: O, ref2: T, ) -> Result<bool, Error>
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.

Source

pub fn throw<'e, E>(&self, obj: E) -> Result<(), Error>
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");
Source

pub fn throw_new<'c, S, T>(&self, class: T, msg: S) -> Result<(), Error>
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");
Source

pub fn exception_occurred(&self) -> Result<JThrowable<'a>, Error>

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.

Source

pub fn exception_describe(&self) -> Result<(), Error>

Print exception information to the console.

Source

pub fn exception_clear(&self) -> Result<(), Error>

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.

Source

pub fn fatal_error<S>(&self, msg: S) -> !
where S: Into<JNIString>,

Abort the JVM with an error message.

Source

pub fn exception_check(&self) -> Result<bool, Error>

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.

Source

pub fn new_direct_byte_buffer( &self, data: &mut [u8], ) -> Result<JByteBuffer<'a>, Error>

Create a new instance of a direct java.nio.ByteBuffer.

Source

pub fn get_direct_buffer_address( &self, buf: JByteBuffer<'_>, ) -> Result<&mut [u8], Error>

Returns the starting address of the memory of the direct java.nio.ByteBuffer.

Source

pub fn get_direct_buffer_capacity( &self, buf: JByteBuffer<'_>, ) -> Result<i64, Error>

Returns the capacity of the direct java.nio.ByteBuffer.

Source

pub fn new_global_ref<O>(&self, obj: O) -> Result<GlobalRef, Error>
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.

Source

pub fn new_local_ref<T>(&self, obj: JObject<'a>) -> Result<JObject<'a>, Error>

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.

Source

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.).

Source

pub fn delete_local_ref(&self, obj: JObject<'_>) -> Result<(), Error>

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.

Source

pub fn push_local_frame(&self, capacity: i32) -> Result<(), Error>

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.

Source

pub fn pop_local_frame(&self, result: JObject<'a>) -> Result<JObject<'a>, Error>

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.

Source

pub fn with_local_frame<F>( &self, capacity: i32, f: F, ) -> Result<JObject<'a>, Error>
where F: FnOnce() -> Result<JObject<'a>, Error>,

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.

Source

pub fn alloc_object<'c, T>(&self, class: T) -> Result<JObject<'a>, Error>
where T: Desc<'a, JClass<'c>>,

Allocates a new object from a class descriptor without running a constructor.

Source

pub fn get_method_id<'c, T, U, V>( &self, class: T, name: U, sig: V, ) -> Result<JMethodID<'a>, Error>
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;");
Source

pub fn get_static_method_id<'c, T, U, V>( &self, class: T, name: U, sig: V, ) -> Result<JStaticMethodID<'a>, Error>
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;");
Source

pub fn get_field_id<'c, T, U, V>( &self, class: T, name: U, sig: V, ) -> Result<JFieldID<'a>, Error>
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");
Source

pub fn get_static_field_id<'c, T, U, V>( &self, class: T, name: U, sig: V, ) -> Result<JStaticFieldID<'a>, Error>
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");
Source

pub fn get_object_class<'b, O>(&self, obj: O) -> Result<JClass<'a>, Error>
where O: Into<JObject<'b>>,

Get the class for an object.

Source

pub fn call_static_method_unchecked<'c, 'm, T, U>( &self, class: T, method_id: U, ret: JavaType, args: &[JValue<'_>], ) -> Result<JValue<'a>, Error>
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.

Source

pub fn call_method_unchecked<'m, O, T>( &self, obj: O, method_id: T, ret: JavaType, args: &[JValue<'_>], ) -> Result<JValue<'a>, Error>
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.

Source

pub fn call_method<O, S, T>( &self, obj: O, name: S, sig: T, args: &[JValue<'_>], ) -> Result<JValue<'a>, Error>
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.

Source

pub fn call_static_method<'c, T, U, V>( &self, class: T, name: U, sig: V, args: &[JValue<'_>], ) -> Result<JValue<'a>, Error>
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.

Source

pub fn new_object<'c, T, U>( &self, class: T, ctor_sig: U, ctor_args: &[JValue<'_>], ) -> Result<JObject<'a>, Error>
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.

Source

pub fn new_object_unchecked<'c, T>( &self, class: T, ctor_id: JMethodID<'_>, ctor_args: &[JValue<'_>], ) -> Result<JObject<'a>, Error>
where T: Desc<'a, JClass<'c>>,

Create a new object using a constructor. Arguments aren’t checked because of the JMethodID usage.

Source

pub fn get_list(&self, obj: JObject<'a>) -> Result<JList<'a, '_>, Error>

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.

Source

pub fn get_map(&self, obj: JObject<'a>) -> Result<JMap<'a, '_>, Error>

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.

Source

pub fn get_string(&self, obj: JString<'a>) -> Result<JavaStr<'a, '_>, Error>

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.

Source

pub fn get_string_utf_chars(&self, obj: JString<'_>) -> Result<*const i8, Error>

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.

Source

pub fn release_string_utf_chars( &self, obj: JString<'_>, arr: *const i8, ) -> Result<(), Error>

Unpin the array returned by get_string_utf_chars.

Source

pub fn new_string<S>(&self, from: S) -> Result<JString<'a>, Error>
where S: Into<JNIString>,

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.

Source

pub fn get_array_length(&self, array: *mut _jobject) -> Result<i32, Error>

Get the length of a java array

Source

pub fn new_object_array<'c, T, U>( &self, length: i32, element_class: T, initial_element: U, ) -> Result<*mut _jobject, Error>
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.

Source

pub fn get_object_array_element( &self, array: *mut _jobject, index: i32, ) -> Result<JObject<'a>, Error>

Returns an element of the jobjectArray array.

Source

pub fn set_object_array_element<O>( &self, array: *mut _jobject, index: i32, value: O, ) -> Result<(), Error>
where O: Into<JObject<'a>>,

Sets an element of the jobjectArray array.

Source

pub fn byte_array_from_slice(&self, buf: &[u8]) -> Result<*mut _jobject, Error>

Create a new java byte array from a rust byte slice.

Source

pub fn convert_byte_array(&self, array: *mut _jobject) -> Result<Vec<u8>, Error>

Converts a java byte array to a rust vector of bytes.

Source

pub fn new_boolean_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java boolean array of supplied length.

Source

pub fn new_byte_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java byte array of supplied length.

Source

pub fn new_char_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java char array of supplied length.

Source

pub fn new_short_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java short array of supplied length.

Source

pub fn new_int_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java int array of supplied length.

Source

pub fn new_long_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java long array of supplied length.

Source

pub fn new_float_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java float array of supplied length.

Source

pub fn new_double_array(&self, length: i32) -> Result<*mut _jobject, Error>

Create a new java double array of supplied length.

Source

pub fn get_boolean_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [u8], ) -> Result<(), Error>

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.

Source

pub fn get_byte_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [i8], ) -> Result<(), Error>

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.

Source

pub fn get_char_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [u16], ) -> Result<(), Error>

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.

Source

pub fn get_short_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [i16], ) -> Result<(), Error>

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.

Source

pub fn get_int_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [i32], ) -> Result<(), Error>

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.

Source

pub fn get_long_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [i64], ) -> Result<(), Error>

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.

Source

pub fn get_float_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [f32], ) -> Result<(), Error>

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.

Source

pub fn get_double_array_region( &self, array: *mut _jobject, start: i32, buf: &mut [f64], ) -> Result<(), Error>

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.

Source

pub fn set_boolean_array_region( &self, array: *mut _jobject, start: i32, buf: &[u8], ) -> Result<(), Error>

Copy the contents of the buf slice to the java boolean array at the start index.

Source

pub fn set_byte_array_region( &self, array: *mut _jobject, start: i32, buf: &[i8], ) -> Result<(), Error>

Copy the contents of the buf slice to the java byte array at the start index.

Source

pub fn set_char_array_region( &self, array: *mut _jobject, start: i32, buf: &[u16], ) -> Result<(), Error>

Copy the contents of the buf slice to the java char array at the start index.

Source

pub fn set_short_array_region( &self, array: *mut _jobject, start: i32, buf: &[i16], ) -> Result<(), Error>

Copy the contents of the buf slice to the java short array at the start index.

Source

pub fn set_int_array_region( &self, array: *mut _jobject, start: i32, buf: &[i32], ) -> Result<(), Error>

Copy the contents of the buf slice to the java int array at the start index.

Source

pub fn set_long_array_region( &self, array: *mut _jobject, start: i32, buf: &[i64], ) -> Result<(), Error>

Copy the contents of the buf slice to the java long array at the start index.

Source

pub fn set_float_array_region( &self, array: *mut _jobject, start: i32, buf: &[f32], ) -> Result<(), Error>

Copy the contents of the buf slice to the java float array at the start index.

Source

pub fn set_double_array_region( &self, array: *mut _jobject, start: i32, buf: &[f64], ) -> Result<(), Error>

Copy the contents of the buf slice to the java double array at the start index.

Source

pub fn get_field_unchecked<'f, O, T>( &self, obj: O, field: T, ty: JavaType, ) -> Result<JValue<'a>, Error>
where O: Into<JObject<'a>>, T: Desc<'a, JFieldID<'f>>,

Get a field without checking the provided type against the actual field.

Source

pub fn set_field_unchecked<'f, O, T>( &self, obj: O, field: T, val: JValue<'_>, ) -> Result<(), Error>
where O: Into<JObject<'a>>, T: Desc<'a, JFieldID<'f>>,

Set a field without any type checking.

Source

pub fn get_field<O, S, T>( &self, obj: O, name: S, ty: T, ) -> Result<JValue<'a>, Error>
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.

Source

pub fn set_field<O, S, T>( &self, obj: O, name: S, ty: T, val: JValue<'_>, ) -> Result<(), Error>
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.

Source

pub fn get_static_field_unchecked<'c, 'f, T, U>( &self, class: T, field: U, ty: JavaType, ) -> Result<JValue<'a>, Error>
where T: Desc<'a, JClass<'c>>, U: Desc<'a, JStaticFieldID<'f>>,

Get a static field without checking the provided type against the actual field.

Source

pub fn get_static_field<'c, T, U, V>( &self, class: T, field: U, sig: V, ) -> Result<JValue<'a>, Error>
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.

Source

pub fn set_static_field<'c, 'f, T, U>( &self, class: T, field: U, value: JValue<'_>, ) -> Result<(), Error>
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.

Source

pub fn set_rust_field<O, S, T>( &self, obj: O, field: S, rust_object: T, ) -> Result<(), Error>
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.

Source

pub fn get_rust_field<O, S, T>( &self, obj: O, field: S, ) -> Result<MutexGuard<'_, T>, Error>
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.

Source

pub fn take_rust_field<O, S, T>(&self, obj: O, field: S) -> Result<T, Error>
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.

Source

pub fn lock_obj<O>(&self, obj: O) -> Result<MonitorGuard<'a>, Error>
where O: Into<JObject<'a>>,

Lock a Java object. The MonitorGuard that this returns is responsible for ensuring that it gets unlocked.

Source

pub fn get_native_interface(&self) -> *mut *const JNINativeInterface_

Returns underlying sys::JNIEnv interface.

Source

pub fn get_java_vm(&self) -> Result<JavaVM, Error>

Returns the Java VM interface.

Source

pub fn ensure_local_capacity(&self, capacity: i32) -> Result<(), Error>

Ensures that at least a given number of local references can be created in the current thread.

Source

pub fn register_native_methods<'c, T>( &self, class: T, methods: &[NativeMethod], ) -> Result<(), Error>
where T: Desc<'a, JClass<'c>>,

Bind function pointers to native methods of class according to method name and signature. For details see documentation.

Source

pub fn unregister_native_methods<'c, T>(&self, class: T) -> Result<(), Error>
where T: Desc<'a, JClass<'c>>,

Unbind all native methods of class.

Source

pub fn get_array_elements<T>( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, T>, Error>
where T: TypeArray,

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.

Source

pub fn get_int_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, i32>, Error>

Source

pub fn get_long_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, i64>, Error>

Source

pub fn get_byte_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, i8>, Error>

Source

pub fn get_boolean_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, u8>, Error>

Source

pub fn get_char_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, u16>, Error>

Source

pub fn get_short_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, i16>, Error>

Source

pub fn get_float_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, f32>, Error>

Source

pub fn get_double_array_elements( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoArray<'_, '_, f64>, Error>

Source

pub fn get_primitive_array_critical( &self, array: *mut _jobject, mode: ReleaseMode, ) -> Result<AutoPrimitiveArray<'_, '_>, Error>

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§

Source§

impl<'a> Deref for AttachGuard<'a>

Source§

type Target = JNIEnv<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<AttachGuard<'a> as Deref>::Target

Dereferences the value.
Source§

impl<'a> Drop for AttachGuard<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for AttachGuard<'a>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> Desc<'a, T> for T

Source§

fn lookup(self, _: &JNIEnv<'a>) -> Result<T, Error>

Look up the concrete type from the JVM.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.