summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-05-22 00:55:28 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-05-23 21:12:06 +0200
commitdd8da7ebd9f9a2536c9c2925bc5638a29ffc8386 (patch)
tree87589b2257c5dbed31adbd9a852d5baf5e3779c9 /src/corelib
parentfb56a0f2ce34e95d955095c01ecf2943046be85e (diff)
Compile-time generate a JNINativeMethod from a C function
Add a template function that allows us to get the method signature string from a free function, which are used as native callbacks methods from Java. Provide a macro that defines a signature object, and a JNINativeMethod object based on it, in an internal namespace so that we don't pollute the namespace with generated names. Add another macro to get the generated JNINativeMethod object based on the free function name. Lastly, add overloads to QJniEnvironment::registerNativeMethods that take a std::initializer_list of JNINativeMethods. We can now declare a free function to be a JNI native method: static bool callbackFromJava(JNIEnv *e, jobject /*thiz*/, jstring p1) { // ... } Q_JNI_DECLARE_NATIVE_METHOD(callbackFromJava); and register it with the JNI environment like this: QJniEnvironment jni; jni.registerNativeMethods(clazz, { Q_JNI_NATIVE_METHOD(callbackFromJava) }); removing a significant amount of boiler plate code. Change-Id: Ie4007b24125879fed3dae1f4d232b4aa95999b44 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/kernel/qjnienvironment.h10
-rw-r--r--src/corelib/kernel/qjnitypes.h24
2 files changed, 34 insertions, 0 deletions
diff --git a/src/corelib/kernel/qjnienvironment.h b/src/corelib/kernel/qjnienvironment.h
index 0a32b19c8e..6cc8693227 100644
--- a/src/corelib/kernel/qjnienvironment.h
+++ b/src/corelib/kernel/qjnienvironment.h
@@ -52,6 +52,16 @@ public:
bool registerNativeMethods(const char *className, const JNINativeMethod methods[], int size);
bool registerNativeMethods(jclass clazz, const JNINativeMethod methods[], int size);
+ bool registerNativeMethods(const char *className, std::initializer_list<JNINativeMethod> methods)
+ {
+ return registerNativeMethods(className, std::data(methods), methods.size());
+ }
+
+ bool registerNativeMethods(jclass clazz, std::initializer_list<JNINativeMethod> methods)
+ {
+ return registerNativeMethods(clazz, std::data(methods), methods.size());
+ }
+
#if QT_DEPRECATED_SINCE(6, 2)
// ### Qt 7: remove
QT_DEPRECATED_VERSION_X_6_2("Use the overload with a const JNINativeMethod[] instead.")
diff --git a/src/corelib/kernel/qjnitypes.h b/src/corelib/kernel/qjnitypes.h
index e5306f3c54..0ebc1a6bc9 100644
--- a/src/corelib/kernel/qjnitypes.h
+++ b/src/corelib/kernel/qjnitypes.h
@@ -274,6 +274,18 @@ static constexpr auto constructorSignature()
return methodSignature<void, Args...>();
}
+template<typename Ret, typename ...Args>
+static constexpr auto nativeMethodSignature(Ret (*)(JNIEnv *, jobject, Args...))
+{
+ return methodSignature<Ret, Args...>();
+}
+
+template<typename Ret, typename ...Args>
+static constexpr auto nativeMethodSignature(Ret (*)(JNIEnv *, jclass, Args...))
+{
+ return methodSignature<Ret, Args...>();
+}
+
// A generic thin wrapper around jobject, convertible to jobject.
// We need this as a baseclass so that QJniObject can be implicitly
// constructed from the various subclasses - we can't provide an
@@ -303,6 +315,18 @@ constexpr auto QtJniTypes::typeSignature<QtJniTypes::Type>() \
return QtJniTypes::String(Signature); \
} \
+#define Q_JNI_DECLARE_NATIVE_METHOD(Method) \
+namespace QtJniMethods { \
+static constexpr auto Method##_signature = \
+ QtJniTypes::nativeMethodSignature(Method); \
+static const JNINativeMethod Method##_method = { \
+ #Method, Method##_signature.data(), \
+ reinterpret_cast<void *>(Method) \
+}; \
+} \
+
+#define Q_JNI_NATIVE_METHOD(Method) QtJniMethods::Method##_method
+
QT_END_NAMESPACE
#endif