summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qjniobject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/kernel/qjniobject.cpp')
-rw-r--r--src/corelib/kernel/qjniobject.cpp52
1 files changed, 33 insertions, 19 deletions
diff --git a/src/corelib/kernel/qjniobject.cpp b/src/corelib/kernel/qjniobject.cpp
index 8244a4390f..d5b0cd5663 100644
--- a/src/corelib/kernel/qjniobject.cpp
+++ b/src/corelib/kernel/qjniobject.cpp
@@ -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
@@ -105,9 +119,9 @@ using namespace Qt::StringLiterals;
// C++ code
QJniObject string1 = QJniObject::fromString("String1");
QJniObject string2 = QJniObject::fromString("String2");
- QJniObject stringArray = QJniObject::callStaticObjectMethod<jstringArray>(
+ QJniObject stringArray = QJniObject::callStaticObjectMethod<jobjectArray>(
"org/qtproject/qt/TestClass",
- "stringArray"
+ "stringArray",
string1.object<jstring>(),
string2.object<jstring>());
\endcode