QJniArray Class

template <typename T> class QJniArray

The QJniArray class is a template class that represents an array in Java. More...

Header: #include <QJniArray>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Since: Qt 6.8
Inherits: QJniArrayBase

Public Types

Public Functions

QJniArray()
QJniArray(Container &&container)
QJniArray(QJniObject &&object)
QJniArray(const QJniObject &object)
QJniArray(jarray array)
QJniArray(std::initializer_list<T> &list)
QJniArray(const QJniArray<T> &other)
QJniArray(QJniArray<T> &&other)
~QJniArray()
auto arrayObject() const
QJniArray<T>::const_reference at(QJniArrayBase::size_type i) const
QJniArray<T>::const_iterator begin() const
QJniArray<T>::const_iterator cbegin() const
QJniArray<T>::const_iterator cend() const
QJniArray<T>::const_iterator constBegin() const
QJniArray<T>::const_iterator constEnd() const
QJniArray<T>::const_reverse_iterator crbegin() const
QJniArray<T>::const_reverse_iterator crend() const
QJniArray<T>::const_iterator end() const
QJniArray<T>::const_reverse_iterator rbegin() const
QJniArray<T>::const_reverse_iterator rend() const
auto toContainer() const
QJniArray<T> &operator=(QJniArray<T> &&other)
QJniArray<T> &operator=(const QJniArray<T> &other)
QJniArray<T>::const_reference operator[](QJniArrayBase::size_type i) const

Detailed Description

The QJniArray template makes it easy to work with arrays when interfacing with Java methods that return or take a Java array.

Note: Java arrays can hold primitive types and objects. The array itself can be treated like a Java Object, and the JNI framework provides explicit APIs to work with such arrays. In addition, the Java class library provides container types such as List or ArrayList. Objects of those types can not be represented by a QJniArray. Instead, use QJniObject to call the class-specific member functions.

To create a QJniArray instance, either construct it from a corresponding C++ container:

 QList<int> intList;
 QJniArray intArray = QJniArray(intList);

or from an initializer list:

 QJniArray intArray{1, 2, 3};

QJniArray will create a new Java array and copy the C++-side data into it.

When calling functions that return an array via QJniObject::callMethod, such as char[] toCharArray() in the Java String class, specify the return type as a C array (jchar[] in the following):

 auto charArray = stringObject.callMethod<jchar[]>("toCharArray");

The charArray variable will be of type QJniArray<jchar>, and hold a new global reference to the jcharArray JNI object.

Lastly, a QJniArray can be constructed from an existing jarray or QJniObject. However, note that no type checking is performed to verify that the jarray or QJniObject indeed represents an array holding elements of the specified type, and accessing a mismatching QJniArray results in undefined behavior.

The data in a QJniArray can either be accessed element by element using at() or operator[](), iterated over, or copied into a suitable C++ container via the toContainer() function.

 QList<jchar> characters = charArray.toContainer();

The return type of toContainer() depends on the type that QJniArray has been instantiated with. For QJniArray<T> this will typically be QList<T>, with the following exceptions:

SpecializationC++ type
QJniArray<jbyte>QByteArray
QJniArray<jstring>QStringList

The elements in the QJniArray cannot be modified through QJniArray APIs, and there is no mutating iterator.

Note: Java arrays are limited to 32 bits, and the size_type member type of QJniArray is jsize, which is a 32bit integer type. Trying to construct a QJniArray from a C++ container that holds more than 2^32 elements will cause a runtime assertion.

Member Type Documentation

[alias] QJniArray::const_iterator

A bi-directional, const iterator for QJniArray.

[alias] QJniArray::const_reverse_iterator

A reverse iterator for the QJniArray, synonym for std::reverse_iterator<const_iterator>.

Member Function Documentation

[noexcept] QJniArray<T>::const_iterator QJniArray::begin() const

[noexcept] QJniArray<T>::const_iterator QJniArray::cbegin() const

[noexcept] QJniArray<T>::const_iterator QJniArray::constBegin() const

Returns an STL-style iterator pointing to the first item in the array.

If the array is invalid, then this will return the same iterator as the corresponding end() function.

See also end() and rbegin().

[noexcept] QJniArray<T>::const_iterator QJniArray::cend() const

[noexcept] QJniArray<T>::const_iterator QJniArray::constEnd() const

[noexcept] QJniArray<T>::const_iterator QJniArray::end() const

Returns an STL-style iterator pointing just after the last item in the list.

See also begin() and rend().

[noexcept] QJniArray<T>::const_reverse_iterator QJniArray::crbegin() const

[noexcept] QJniArray<T>::const_reverse_iterator QJniArray::rbegin() const

Returns an STL-style reverse iterator pointing to the first item in the array, in reverse order.

If the array is invalid, then this will return the same iterator as the corresponding rend() function.

See also rend() and begin().

[noexcept] QJniArray<T>::const_reverse_iterator QJniArray::crend() const

[noexcept] QJniArray<T>::const_reverse_iterator QJniArray::rend() const

Returns an STL-style reverse iterator pointing just after the last item in the list, in reverse order.

See also rbegin() and end().

QJniArray<T>::const_reference QJniArray::at(QJniArrayBase::size_type i) const

QJniArray<T>::const_reference QJniArray::operator[](QJniArrayBase::size_type i) const

Returns the value at position i in the wrapped Java array.

i must be a valid index position in the list (i.e., 0 <= i < size()).

See also size().

QJniArray::QJniArray()

Default constructor of QJniArray. This does not create a Java-side array, and the instance will be invalid.

See also isValid.

[explicit] template <typename Container, QJniArrayBase::if_contiguous_container<Container> = true> QJniArray::QJniArray(Container &&container)

Constructs a QJniArray that wraps a newly-created Java array for elements of type Container::value_type, and populates the Java array with the data from container.

This function only participates in overload resolution if Container is a contiguous container storing elements of a JNI type or equivalent C++ type.

The specialization of the constructed QJniArray depends on the value type of the container. For a Container<T> (such as e.g. QList<T>) it will typically be QJniArray<T>, with the following exceptions:

ContainerSpecialization
QByteArrayQJniArray<jbyte>
QStringListQJniArray<jstring>
Container::value_typeSpecialization
QJniObjectQJniArray<jobject>

See also QJniArrayBase::fromContainer() and toContainer().

[explicit noexcept] QJniArray::QJniArray(QJniObject &&object)

Constructs a QJniArray by moving from object. The QJniObject becomes invalid.

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

[explicit] QJniArray::QJniArray(const QJniObject &object)

Constructs a QJniArray that wraps the same Java array as object, creating a new global reference. To construct a QJniArray from an existing local reference, use a QJniObject constructed via fromLocalRef().

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

[explicit] QJniArray::QJniArray(jarray array)

Constructs a QJniArray that wraps the Java-side array array, creating a new global reference to array.

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

[default] QJniArray::QJniArray(std::initializer_list<T> &list)

Constructs a QJniArray that wraps a newly-created Java array for elements of type T, and populates the Java array with the data from list.

See also QJniArrayBase::fromContainer() and toContainer().

QJniArray::QJniArray(const QJniArray<T> &other)

Constructs a QJniArray that is a copy of other. Both QJniArray objects will reference the same Java array object.

[noexcept] QJniArray::QJniArray(QJniArray<T> &&other)

Constructs a QJniArray by moving from other. The other array becomes invalid.

QJniArray::~QJniArray()

Destroys the QJniArray object and releases any references held to the wrapped Java array.

auto QJniArray::arrayObject() const

Returns the wrapped Java object as the suitable jarray type that matches the element type T of this QJniArray object.

Tjarray type
jbytejbyteArray
jcharjcharArray
......
jobjectjobjectArray
QJniObjectjobjectArray
Q_DECLARE_JNI_CLASSjobjectArray

auto QJniArray::toContainer() const

Returns the data in the wrapped Java objects as a suitable C++ container, which will be empty if the array is invalid.

For QJniArray<T> this will typically be QList<T>, with the following exceptions:

SpecializationC++ type
QJniArray<jbyte>QByteArray
QJniArray<jstring>QStringList

See also QJniArrayBase::fromContainer().

[noexcept] QJniArray<T> &QJniArray::operator=(QJniArray<T> &&other)

Moves other into this QJniArray. The other array becomes invalid.

QJniArray<T> &QJniArray::operator=(const QJniArray<T> &other)

Assigns other to this QJniArray. Both QJniArray objects will reference the same Java array object.