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 <qt_ci_bot@qt-project.org>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Volker Hilsheimer 2022-07-08 12:31:36 +02:00
parent 1c563035c7
commit 7b6350fa77
3 changed files with 35 additions and 0 deletions

View File

@ -1230,6 +1230,14 @@ QJniObject QJniObject::callStaticObjectMethod(jclass clazz, jmethodID methodId,
Retrieves the value from the static field \a fieldName on \a clazz. Retrieves the value from the static field \a fieldName on \a clazz.
*/ */
/*!
\fn template <typename Klass, typename T> 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 <typename T> void QJniObject::setStaticField(const char *className, const char *fieldName, T value) \fn template <typename T> 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. Sets the static field \a fieldName of the class \a clazz to \a value.
*/ */
/*!
\fn template <typename Klass, typename T> 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) \fn QJniObject QJniObject::getStaticObjectField(const char *className, const char *fieldName, const char *signature)

View File

@ -258,6 +258,12 @@ public:
} }
} }
template <typename Klass, typename T>
static auto getStaticField(const char *fieldName)
{
return getStaticField<T>(QtJniTypes::className<Klass>(), fieldName);
}
template <typename T> template <typename T>
QJniObject getObjectField(const char *fieldName) const QJniObject getObjectField(const char *fieldName) const
{ {
@ -380,6 +386,12 @@ public:
} }
} }
template <typename Klass, typename T>
static void setStaticField(const char *fieldName, T value)
{
setStaticField(QtJniTypes::className<Klass>(), fieldName, value);
}
static QJniObject fromString(const QString &string); static QJniObject fromString(const QString &string);
QString toString() const; QString toString() const;

View File

@ -9,6 +9,7 @@
#include <QtTest> #include <QtTest>
static const char testClassName[] = "org/qtproject/qt/android/testdatapackage/QtJniObjectTestClass"; 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 jbyte A_BYTE_VALUE = 127;
static const jshort A_SHORT_VALUE = 32767; static const jshort A_SHORT_VALUE = 32767;
@ -1035,6 +1036,12 @@ void setStaticField(const char *fieldName, T testValue)
T res = QJniObject::getStaticField<T>(testClassName, fieldName); T res = QJniObject::getStaticField<T>(testClassName, fieldName);
QCOMPARE(res, testValue); QCOMPARE(res, testValue);
// use template overload to reset to default
T defaultValue = {};
QJniObject::setStaticField<QtJniTypes::QtJniObjectTestClass, T>(fieldName, defaultValue);
res = QJniObject::getStaticField<QtJniTypes::QtJniObjectTestClass, T>(fieldName);
QCOMPARE(res, defaultValue);
} }
void tst_QJniObject::setStaticIntField() void tst_QJniObject::setStaticIntField()