From f9109549280c62472597ba952f10711b8768aa4b Mon Sep 17 00:00:00 2001 From: Petri Virkkunen Date: Wed, 8 Jan 2025 16:03:49 +0200 Subject: [PATCH] Android: JNI: Use void as default return value type on call[Static]Method By using void as default template argument in callStaticMethod and callMethod, we can avoid having to type after every void method call, while still allowing the caller to remain explicit about the type if they want to. Change-Id: Id7143d1116364be7699712d1fead257f26f20420 Reviewed-by: Volker Hilsheimer --- src/corelib/kernel/qjniobject.h | 20 +++++++------- .../kernel/qjniobject/tst_qjniobject.cpp | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 79178d6b0ca..7ff30fc1d99 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -132,7 +132,7 @@ public: jclass objectClass() const; QByteArray className() const; - template = true #endif @@ -164,7 +164,7 @@ public: } } - template = true #endif @@ -191,7 +191,7 @@ public: QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const; - template + template static auto callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args) { JNIEnv *env = QJniEnvironment::getJniEnv(); @@ -199,7 +199,7 @@ public: return callStaticMethod(clazz, methodName, signature, std::forward(args)...); } - template + template static auto callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args) { JNIEnv *env = QJniEnvironment::getJniEnv(); @@ -208,7 +208,7 @@ public: return callStaticMethod(clazz, id, std::forward(args)...); } - template = true #endif @@ -239,7 +239,7 @@ public: } } - template = true #endif @@ -254,7 +254,7 @@ public: return callStaticMethod(clazz, id, std::forward(args)...); } - template = true #endif @@ -264,7 +264,7 @@ public: constexpr auto signature = QtJniTypes::methodSignature(); return callStaticMethod(clazz, methodName, signature.data(), std::forward(args)...); } - template = true #endif @@ -761,7 +761,7 @@ public: } // public API forwarding to QJniObject, with the implicit Class template parameter - template = true #endif @@ -791,7 +791,7 @@ public: } // keep only these overloads, the rest is made private - template = true #endif diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp index 6291c70fdeb..399f1a74deb 100644 --- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp +++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp @@ -113,6 +113,7 @@ private slots: void setStaticObjectField(); void templateApiCheck(); + void defaultTemplateApiCheck(); void isClassAvailable(); void fromLocalRef(); void largeObjectArray(); @@ -1875,6 +1876,31 @@ void tst_QJniObject::templateApiCheck() } +void tst_QJniObject::defaultTemplateApiCheck() +{ + // static QJniObject calls -------------------------------------------------------------------- + QJniObject::callStaticMethod(testClassName, "staticVoidMethod"); + QJniObject::callStaticMethod(testClassName, "staticVoidMethodWithArgs", "(IZC)V", 1, true, 'c'); + QJniObject::callStaticMethod(testClassName, "staticVoidMethodWithArgs", 1, true, 'c'); + + // instance QJniObject calls ------------------------------------------------------------------ + QJniObject testClass(testClassName); + QVERIFY(testClass.isValid()); + + testClass.callMethod("voidMethod"); + testClass.callMethod("voidMethodWithArgs", "(IZC)V", 1, true, 'c'); + testClass.callMethod("voidMethodWithArgs", 1, true, 'c'); + + // static QtJniType calls --------------------------------------------------------------------- + TestClass::callStaticMethod("staticVoidMethod"); + TestClass::callStaticMethod("staticVoidMethodWithArgs", 1, true, 'c'); + + // instance QtJniType calls ------------------------------------------------------------------- + TestClass instance; + instance.callMethod("voidMethod"); + instance.callMethod("voidMethodWithArgs", 1, true, 'c'); +} + void tst_QJniObject::isClassAvailable() { QVERIFY(QJniObject::isClassAvailable("java/lang/String"));