Update QJniObject documentation to refer to QtJniTypes

Change-Id: I5d0c27625eac3ef1c619cb591af110b7659b769a
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
bb10
Volker Hilsheimer 2024-05-31 14:07:25 +02:00
parent aad13ef59c
commit 93ae58fd3c
1 changed files with 31 additions and 17 deletions

View File

@ -34,19 +34,10 @@ using namespace Qt::StringLiterals;
\sa QJniEnvironment
\section1 General Notes
\list
\li Class names need to be fully-qualified, for example: \c "java/lang/String".
\li Method signatures are written as \c "(ArgumentsTypes)ReturnType", see \l {JNI Types}.
\li All object types are returned as a QJniObject.
\endlist
\section1 Method Signatures
QJniObject provides convenience functions that will use the correct signature based on the
provided template types. For functions that only return and take \l {JNI types}, the
signature can be generate at compile time:
provided or deduced template arguments.
\code
jint x = QJniObject::callMethod<jint>("getSize");
@ -54,13 +45,11 @@ using namespace Qt::StringLiterals;
jint ret = jString1.callMethod<jint>("compareToIgnoreCase", jString2.object<jstring>());
\endcode
These functions are variadic templates, and the compiler will deduce the template arguments
from the actual argument types. In many situations, only the return type needs to be provided
explicitly.
For functions that take other argument types, you need to supply the signature yourself. It is
important that the signature matches the function you want to call. The example below
demonstrates how to call different static functions:
These functions are variadic templates, and the compiler will deduce the
signature from the actual argument types. Only the return type needs to be
provided explicitly. QJniObject can deduce the signature string for
functions that take \l {JNI types}, and for types that have been declared
with the QtJniTypes type mapping.
\code
// Java class
@ -73,6 +62,31 @@ using namespace Qt::StringLiterals;
}
\endcode
\code
// C++ code
Q_DECLARE_JNI_CLASS(TestClass, "org/qtproject/qt/TestClass")
// ...
using namespace QtJniTypes;
TestClass testClass = TestClass::callStaticMethod<TestClass>("create");
\endcode
This allows working with arbitrary Java and Android types in C++ code, without having to
create JNI signature strings explicitly.
\section2 Explicit JNI Signatures
It is possible to supply the signature yourself. In that case, it is important
that the signature matches the function you want to call.
\list
\li Class names need to be fully-qualified, for example: \c "java/lang/String".
\li Method signatures are written as \c "(ArgumentsTypes)ReturnType", see \l {JNI Types}.
\li All object types are returned as a QJniObject.
\endlist
The example below demonstrates how to call different static functions:
The signature structure is \c "(ArgumentsTypes)ReturnType". Array types in the signature
must have the \c {[} prefix, and the fully-qualified \c Object type names must have the
\c L prefix and the \c ; suffix. The signature for the \c create function is