From 7b6350fa7743fa4ca10f5aebe0962b6544604e33 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 8 Jul 2022 12:31:36 +0200 Subject: [PATCH] QJniObject: Add template overloads for get/setStaticField Allow specifying the Java class on which to set/get the field via its corresponding C++ type, removing the need to explicitly provide the Java type string. Those were missing from a085a14d76553ebd1fa4a4a11a27110ee544a531, which was noticed when porting QtConnectivity over to the new template APIs. Pick-to: 6.4 Change-Id: I8f324c9fcc486b4c6c2f2b9051f7eca0cbec0e91 Reviewed-by: Qt CI Bot Reviewed-by: Ivan Solovev --- src/corelib/kernel/qjniobject.cpp | 16 ++++++++++++++++ src/corelib/kernel/qjniobject.h | 12 ++++++++++++ .../corelib/kernel/qjniobject/tst_qjniobject.cpp | 7 +++++++ 3 files changed, 35 insertions(+) diff --git a/src/corelib/kernel/qjniobject.cpp b/src/corelib/kernel/qjniobject.cpp index dda4f474029..62a5993559b 100644 --- a/src/corelib/kernel/qjniobject.cpp +++ b/src/corelib/kernel/qjniobject.cpp @@ -1230,6 +1230,14 @@ QJniObject QJniObject::callStaticObjectMethod(jclass clazz, jmethodID methodId, Retrieves the value from the static field \a fieldName on \a clazz. */ +/*! + \fn template auto QJniObject::getStaticField(const char *fieldName) + + Retrieves the value from the static field \a fieldName for the class \c Klass. + + \c Klass needs to be a C++ type with a registered type mapping to a Java type. +*/ + /*! \fn template void QJniObject::setStaticField(const char *className, const char *fieldName, T value) @@ -1242,6 +1250,14 @@ QJniObject QJniObject::callStaticObjectMethod(jclass clazz, jmethodID methodId, Sets the static field \a fieldName of the class \a clazz to \a value. */ +/*! + \fn template auto QJniObject::setStaticField(const char *fieldName, T value) + + Sets the static field \a fieldName of the class \c Klass to \a value. + + \c Klass needs to be a C++ type with a registered type mapping to a Java type. +*/ + /*! \fn QJniObject QJniObject::getStaticObjectField(const char *className, const char *fieldName, const char *signature) diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 3c7ca13ff29..2ae4c03dca0 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -258,6 +258,12 @@ public: } } + template + static auto getStaticField(const char *fieldName) + { + return getStaticField(QtJniTypes::className(), fieldName); + } + template QJniObject getObjectField(const char *fieldName) const { @@ -380,6 +386,12 @@ public: } } + template + static void setStaticField(const char *fieldName, T value) + { + setStaticField(QtJniTypes::className(), fieldName, value); + } + static QJniObject fromString(const QString &string); QString toString() const; diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp index e57785c5c5c..7ea6b1e121f 100644 --- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp +++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp @@ -9,6 +9,7 @@ #include static const char testClassName[] = "org/qtproject/qt/android/testdatapackage/QtJniObjectTestClass"; +Q_DECLARE_JNI_CLASS(QtJniObjectTestClass, testClassName) static const jbyte A_BYTE_VALUE = 127; static const jshort A_SHORT_VALUE = 32767; @@ -1035,6 +1036,12 @@ void setStaticField(const char *fieldName, T testValue) T res = QJniObject::getStaticField(testClassName, fieldName); QCOMPARE(res, testValue); + + // use template overload to reset to default + T defaultValue = {}; + QJniObject::setStaticField(fieldName, defaultValue); + res = QJniObject::getStaticField(fieldName); + QCOMPARE(res, defaultValue); } void tst_QJniObject::setStaticIntField()