JNI: Add convenience overloads for registerNativeMethods

Add a template overload for the QJniEnvironment member function so that
a declared type can be used.

And add a static registerNativeMethods class member function to declared
types.

As a drive-by, document the initializer_list overloads. The
"template <typename Class>" convenience wrappers are currently all
undocumented, as we first need to document the QtJniTypes type system
and declaration macros.

Change-Id: I8ff9edc4e493694e6d2c26d4bc7b06bd8e05bf0c
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-11-13 12:12:23 +01:00
parent c706011dc0
commit 536fd29bdf
4 changed files with 36 additions and 8 deletions

View File

@ -351,6 +351,15 @@ bool QJniEnvironment::registerNativeMethods(const char *className, const JNINati
return registerNativeMethods(clazz, methods, size);
}
/*!
\fn bool QJniEnvironment::registerNativeMethods(const char *className, std::initializer_list<JNINativeMethod> methods)
\overload
Registers the native functions methods in \a methods for the Java class \a className.
Returns \c true if the registration is successful, otherwise \c false.
*/
#if QT_DEPRECATED_SINCE(6, 2)
/*!
\overload
@ -405,6 +414,14 @@ bool QJniEnvironment::registerNativeMethods(jclass clazz, const JNINativeMethod
return true;
}
/*!
\fn bool QJniEnvironment::registerNativeMethods(jclass clazz, std::initializer_list<JNINativeMethod> methods)
\overload
Registers the native functions methods in \a methods for the Java class \a clazz.
Returns \c true if the registration is successful, otherwise \c false.
*/
/*!
\enum QJniEnvironment::OutputMode

View File

@ -64,6 +64,12 @@ public:
return registerNativeMethods(clazz, std::data(methods), methods.size());
}
template<typename Class>
bool registerNativeMethods(std::initializer_list<JNINativeMethod> methods)
{
return registerNativeMethods(QtJniTypes::Traits<Class>::className().data(), methods);
}
#if QT_DEPRECATED_SINCE(6, 2)
// ### Qt 7: remove
QT_DEPRECATED_VERSION_X_6_2("Use the overload with a const JNINativeMethod[] instead.")

View File

@ -47,6 +47,12 @@ struct Object : QJniObject
template <typename ...Args>
static Object construct(Args &&...args) { return Object{std::forward<Args>(args)...}; }
static bool registerNativeMethods(std::initializer_list<JNINativeMethod> methods)
{
QJniEnvironment env;
return env.registerNativeMethods<Class>(methods);
}
// public API forwarding to QJniObject, with the implicit Class template parameter
template <typename Ret, typename ...Args
#ifndef Q_QDOC

View File

@ -1946,12 +1946,11 @@ void tst_QJniObject::callback()
QFETCH(const CallbackParameterType, parameterType);
TestClass testObject;
QJniEnvironment env;
int result = -1;
switch (parameterType) {
case CallbackParameterType::Object:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithObject)
}));
result = testObject.callMethod<int>("callMeBackWithObject", testObject);
@ -1959,7 +1958,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithObject.value(), testObject);
break;
case CallbackParameterType::ObjectRef:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithObjectRef)
}));
result = testObject.callMethod<int>("callMeBackWithObjectRef", testObject);
@ -1967,7 +1966,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithObject.value(), testObject);
break;
case CallbackParameterType::String:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithString)
}));
result = testObject.callMethod<int>("callMeBackWithString", QString::number(123));
@ -1975,7 +1974,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithString.value(), "123");
break;
case CallbackParameterType::Byte:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithByte)
}));
result = testObject.callMethod<int>("callbackWithByte", jbyte(123));
@ -1983,7 +1982,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithByte.value(), 123);
break;
case CallbackParameterType::Boolean:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithBoolean)
}));
result = testObject.callMethod<int>("callbackWithBoolean", true);
@ -1991,7 +1990,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithBoolean.value(), true);
break;
case CallbackParameterType::Int:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithInt)
}));
result = testObject.callMethod<int>("callbackWithInt", 12345);
@ -1999,7 +1998,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithInt.value(), 12345);
break;
case CallbackParameterType::Double:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
QVERIFY(TestClass::registerNativeMethods({
Q_JNI_NATIVE_METHOD(callbackWithDouble)
}));
result = testObject.callMethod<int>("callbackWithDouble", 1.2345);