diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-05-24 17:20:15 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-05-27 22:27:13 +0200 |
commit | 31f98957cf5c061f868588ef81059a907c9d30ad (patch) | |
tree | 0d6487bbed38198b4f49ee4776473bd0f813a730 /tests/auto/corelib/kernel/qjnitypes | |
parent | 9614f4d434474fe7dcca752b3f72a56f2d382230 (diff) |
Add compile-time generation of JNI class names
As with method signatures, register class names using template function
specialization in the QtJniTypes namespace, and then declare C++ types
as JNI classes with a class name string. Such classes implicitly get
registered as JNI types as well.
Add a QJniObject construct method (since C++ constructors that are
templates cannot be explicitly instantiated with a type), and a
QJniEnvironment::findClass overload.
Add test coverage, also for the recently added macros for native
methods.
As a drive-by, change the name of the Q_JNI_DECLARE_NATIVE_METHOD
macro to Q_DECLARE_JNI_NATIVE_METHOD for consistency.
Change-Id: Ic19562d78da726f202b3bdf4e9354e8ad24d8bd9
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qjnitypes')
-rw-r--r-- | tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp index c4abfe399a..395028b8cb 100644 --- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -14,6 +14,7 @@ public: private slots: void initTestCase(); + void nativeMethod(); }; struct QtJavaWrapper {}; @@ -45,6 +46,11 @@ static_assert(QtJniTypes::typeSignature<QtJniTypes::JavaType>() == "Lorg/qtproje Q_DECLARE_JNI_TYPE(ArrayType, "[Lorg/qtproject/qt/ArrayType;") static_assert(QtJniTypes::typeSignature<QtJniTypes::ArrayType>() == "[Lorg/qtproject/qt/ArrayType;"); +static_assert(QtJniTypes::className<jstring>() == "java/lang/String"); + +Q_DECLARE_JNI_CLASS(QtTextToSpeech, "org/qtproject/qt/android/speech/QtTextToSpeech") +static_assert(QtJniTypes::className<QtJniTypes::QtTextToSpeech>() == "org/qtproject/qt/android/speech/QtTextToSpeech"); + static_assert(QtJniTypes::fieldSignature<jint>() == "I"); static_assert(QtJniTypes::fieldSignature<jint>() != "X"); static_assert(QtJniTypes::fieldSignature<jint>() != "Ljava/lang/Object;"); @@ -97,6 +103,22 @@ void tst_QJniTypes::initTestCase() } +static bool nativeFunction(JNIEnv *, jclass, int, jstring, long) +{ + return true; +} +Q_DECLARE_JNI_NATIVE_METHOD(nativeFunction) + +static_assert(QtJniTypes::nativeMethodSignature(nativeFunction) == "(ILjava/lang/String;J)Z"); + +void tst_QJniTypes::nativeMethod() +{ + const auto method = Q_JNI_NATIVE_METHOD(nativeFunction); + QVERIFY(method.fnPtr == nativeFunction); + QCOMPARE(method.name, "nativeFunction"); + QCOMPARE(method.signature, "(ILjava/lang/String;J)Z"); +} + QTEST_MAIN(tst_QJniTypes) #include "tst_qjnitypes.moc" |