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 <void> 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 <volker.hilsheimer@qt.io>
This commit is contained in:
Petri Virkkunen 2025-01-08 16:03:49 +02:00
parent 2defca4187
commit f910954928
2 changed files with 36 additions and 10 deletions

View File

@ -132,7 +132,7 @@ public:
jclass objectClass() const;
QByteArray className() const;
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidFieldType<Ret> = true
#endif
@ -164,7 +164,7 @@ public:
}
}
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif
@ -191,7 +191,7 @@ public:
QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const;
template <typename Ret, typename ...Args>
template <typename Ret = void, typename ...Args>
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<Ret>(clazz, methodName, signature, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args>
template <typename Ret = void, typename ...Args>
static auto callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
{
JNIEnv *env = QJniEnvironment::getJniEnv();
@ -208,7 +208,7 @@ public:
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidFieldType<Ret> = true
#endif
@ -239,7 +239,7 @@ public:
}
}
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif
@ -254,7 +254,7 @@ public:
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
}
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif
@ -264,7 +264,7 @@ public:
constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
return callStaticMethod<Ret>(clazz, methodName, signature.data(), std::forward<Args>(args)...);
}
template <typename Klass, typename Ret, typename ...Args
template <typename Klass, typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif
@ -761,7 +761,7 @@ public:
}
// public API forwarding to QJniObject, with the implicit Class template parameter
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif
@ -791,7 +791,7 @@ public:
}
// keep only these overloads, the rest is made private
template <typename Ret, typename ...Args
template <typename Ret = void, typename ...Args
#ifndef Q_QDOC
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
#endif

View File

@ -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"));