diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-09-14 21:15:20 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-10-16 18:43:16 +0200 |
commit | 80d4d55e250af1d643805bde3821987112cf09c1 (patch) | |
tree | 985611d8fcddadcdc7890479c83d8abdb9f3a5d5 /tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt | |
parent | 53f9768e6424f7b00946005f82f11d82facee35a (diff) |
JNI: add QJniArray class for easier working with arrays
Implements an iterator API and other standard member access functions
for sequential containers so that we can use ranged-for over an object
that is a jarray. Provides read-only access to individual elements
(which is mostly relevant for arrays of objects), or the entire data()
as a contiguous memory block (which is useful for arrays of primitive
types).
QJniObject call functions can return QJniArray<T> when the return type
is either explicitly QJniArray<T> or T[], or their Qt equivalent (e.g.
a jbyteArray can be taken or returned as a QByteArray). If the return
type is a jarray type, then a QJniObject is returned as before.
Arrays can be created from a Qt container through a constructor or the
generic fromData named constructor in the QJniArrayBase class, which
implements the generic logic.
Not documented as public API yet.
Added a compile-time test to verify that types are mapped correctly.
The function test coverage is added to the QJniObject auto-test, as
that already provides the Java test class with functions taking and
returning arrays of different types.
Change-Id: I0750fc4f4cce7314df3b10e122eafbcfd68297b6
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt')
-rw-r--r-- | tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java index d1b4ea480e..b56447198f 100644 --- a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java +++ b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java @@ -135,44 +135,143 @@ public class QtJniObjectTestClass public static Object[] staticObjectArrayMethod() { Object[] array = { new Object(), new Object(), new Object() }; return array; } public Object[] objectArrayMethod() { return staticObjectArrayMethod(); } + public static Object[] staticReverseObjectArray(Object[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + Object old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public Object[] reverseObjectArray(Object[] array) + { return staticReverseObjectArray(array); } // -------------------------------------------------------------------------------------------- public static boolean[] staticBooleanArrayMethod() { boolean[] array = { true, true, true }; return array; } public boolean[] booleanArrayMethod() { return staticBooleanArrayMethod(); } + public static boolean[] staticReverseBooleanArray(boolean[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + boolean old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public boolean[] reverseBooleanArray(boolean[] array) + { return staticReverseBooleanArray(array); } // -------------------------------------------------------------------------------------------- public static byte[] staticByteArrayMethod() { byte[] array = { 'a', 'b', 'c' }; return array; } public byte[] byteArrayMethod() { return staticByteArrayMethod(); } + public static byte[] staticReverseByteArray(byte[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + byte old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public byte[] reverseByteArray(byte[] array) + { return staticReverseByteArray(array); } // -------------------------------------------------------------------------------------------- public static char[] staticCharArrayMethod() { char[] array = { 'a', 'b', 'c' }; return array; } public char[] charArrayMethod() { return staticCharArrayMethod(); } + public static char[] staticReverseCharArray(char[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + char old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public char[] reverseCharArray(char[] array) + { return staticReverseCharArray(array); } // -------------------------------------------------------------------------------------------- public static short[] staticShortArrayMethod() { short[] array = { 3, 2, 1 }; return array; } public short[] shortArrayMethod() { return staticShortArrayMethod(); } + public static short[] staticReverseShortArray(short[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + short old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public short[] reverseShortArray(short[] array) + { return staticReverseShortArray(array); } // -------------------------------------------------------------------------------------------- public static int[] staticIntArrayMethod() { int[] array = { 3, 2, 1 }; return array; } public int[] intArrayMethod() { return staticIntArrayMethod(); } + public static int[] staticReverseIntArray(int[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + int old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public int[] reverseIntArray(int[] array) + { return staticReverseIntArray(array); } // -------------------------------------------------------------------------------------------- public static long[] staticLongArrayMethod() { long[] array = { 3, 2, 1 }; return array; } public long[] longArrayMethod() { return staticLongArrayMethod(); } + public static long[] staticReverseLongArray(long[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + long old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public long[] reverseLongArray(long[] array) + { return staticReverseLongArray(array); } // -------------------------------------------------------------------------------------------- public static float[] staticFloatArrayMethod() { float[] array = { 1.0f, 2.0f, 3.0f }; return array; } public float[] floatArrayMethod() { return staticFloatArrayMethod(); } + public static float[] staticReverseFloatArray(float[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + float old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public float[] reverseFloatArray(float[] array) + { return staticReverseFloatArray(array); } // -------------------------------------------------------------------------------------------- public static double[] staticDoubleArrayMethod() { double[] array = { 3.0, 2.0, 1.0 }; return array; } public double[] doubleArrayMethod() { return staticDoubleArrayMethod(); } + public static double[] staticReverseDoubleArray(double[] array) + { + for (int i = 0; i < array.length / 2; ++i) { + double old = array[array.length - i - 1]; + array[array.length - i - 1] = array[i]; + array[i] = old; + } + return array; + } + public double[] reverseDoubleArray(double[] array) + { return staticReverseDoubleArray(array); } // -------------------------------------------------------------------------------------------- native public int callbackWithObject(QtJniObjectTestClass that); |