Skip to content

Commit

Permalink
Remove redundant JNIEnv::get_<type>_array_elements methods
Browse files Browse the repository at this point in the history
Types like `JByteArray` are now simply aliases for `JPrimitiveArray<T>`
and can be passed to the generic `get_array_elements` instead.

For now the `test_get_array_elements` tests have been kept at least for
the sake of checking the data integrity from updating array elements of
different types/sizes, even though they now utilize the same
`get_array_elements` code.
  • Loading branch information
rib committed Jan 19, 2023
1 parent bfb61c8 commit 23b9009
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 119 deletions.
123 changes: 8 additions & 115 deletions src/wrapper/jnienv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,27 +2717,16 @@ impl<'local> JNIEnv<'local> {
jni_error_code_to_result(res)
}

/// Return an AutoArray of the given Java array.
/// 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.
/// 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`](struct.JNIEnv.html#method.get_int_array_elements)
/// [`get_long_array_elements`](struct.JNIEnv.html#method.get_long_array_elements)
/// [`get_byte_array_elements`](struct.JNIEnv.html#method.get_byte_array_elements)
/// [`get_boolean_array_elements`](struct.JNIEnv.html#method.get_boolean_array_elements)
/// [`get_char_array_elements`](struct.JNIEnv.html#method.get_char_array_elements)
/// [`get_short_array_elements`](struct.JNIEnv.html#method.get_short_array_elements)
/// [`get_float_array_elements`](struct.JNIEnv.html#method.get_float_array_elements)
/// [`get_double_array_elements`](struct.JNIEnv.html#method.get_double_array_elements)
/// And the associated [`AutoArray`](struct.objects.AutoArray) struct.
/// the corresponding `Release*ArrayElements` JNI method is called.
/// [`AutoArray`] has a commit() method, to force a copy back of pending
/// array changes if needed (and without releasing it).
pub fn get_array_elements<'ptr, 'other_local: 'ptr, T: TypeArray + 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, T>>,
Expand All @@ -2750,103 +2739,7 @@ impl<'local> JNIEnv<'local> {
AutoArray::new(self, array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_int_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jint>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jint>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_long_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jlong>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jlong>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_byte_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jbyte>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jbyte>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_boolean_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jboolean>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jboolean>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_char_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jchar>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jchar>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_short_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jshort>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jshort>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_float_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jfloat>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jfloat>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// See also [`get_array_elements`](struct.JNIEnv.html#method.get_array_elements)
pub fn get_double_array_elements<'ptr, 'other_local>(
&mut self,
array: impl AsRef<JPrimitiveArray<'other_local, jdouble>>,
mode: ReleaseMode,
) -> Result<AutoArray<'ptr, 'local, 'other_local, jdouble>>
where
'local: 'ptr,
{
self.get_array_elements(array, mode)
}

/// Return an AutoPrimitiveArray of the given Java primitive array.
/// 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.
Expand All @@ -2865,7 +2758,7 @@ impl<'local> JNIEnv<'local> {
///
/// If the given array is `null`, an `Error::NullPtr` is returned.
///
/// See also [`get_byte_array_elements`](struct.JNIEnv.html#method.get_array_elements)
/// See also [`get_array_elements`](Self::get_array_elements)
pub fn get_primitive_array_critical<'ptr, 'other_local, T: TypeArray + 'other_local>(
&'ptr mut self,
array: impl AsRef<JPrimitiveArray<'other_local, T>>,
Expand Down
8 changes: 4 additions & 4 deletions tests/jni_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ pub fn java_byte_array_from_slice() {
}

macro_rules! test_get_array_elements {
( $jni_get:tt, $jni_type:ty, $new_array:tt, $get_array:tt, $set_array:tt ) => {
( $test_name:tt, $jni_type:ty, $new_array:tt, $get_array:tt, $set_array:tt ) => {
#[test]
pub fn $jni_get() {
pub fn $test_name() {
let env = attach_current_thread();

// Create original Java array
Expand All @@ -494,7 +494,7 @@ macro_rules! test_get_array_elements {
// Make sure the lifetime is tied to the environment,
// not the particular JNIEnv reference
let mut temporary_env: JNIEnv = unsafe { env.unsafe_clone() };
temporary_env.$jni_get(&java_array, ReleaseMode::CopyBack).unwrap()
temporary_env.get_array_elements(&java_array, ReleaseMode::CopyBack).unwrap()
};

// Check array size
Expand Down Expand Up @@ -624,7 +624,7 @@ pub fn get_long_array_elements_commit() {

// Get long array elements auto wrapper
let mut auto_ptr = env
.get_long_array_elements(&java_array, ReleaseMode::CopyBack)
.get_array_elements(&java_array, ReleaseMode::CopyBack)
.unwrap();

// Copying the array depends on the VM vendor/version/GC combinations.
Expand Down

0 comments on commit 23b9009

Please sign in to comment.