summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2022-06-30 18:53:22 +0300
committerJuha Vuolle <juha.vuolle@insta.fi>2022-07-05 11:50:25 +0300
commit58fd12a487cd2c628d713d1c42f8b53f8c4e9550 (patch)
treee609e3a01aaf7a0528ec99b74f3bda358db5cfcb
parent217c47eac4fca0594c7e7669e7d2cd267c15f29c (diff)
Add support for naming JNI functions with the unstringify macro
The manually defined native JNI function tables allow defining a name that may be different from the actual function name; this name is then used from the Java-side. This can be useful to provide also as an option with the new "unstringifying" macros which can help for example in porting code to these new macros. Pick-to: 6.4 Change-Id: Icfebfb351cb8dfb122795d20b37e2eac167a41bf Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r--src/corelib/kernel/qjnitypes.h9
-rw-r--r--tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java6
-rw-r--r--tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp21
3 files changed, 34 insertions, 2 deletions
diff --git a/src/corelib/kernel/qjnitypes.h b/src/corelib/kernel/qjnitypes.h
index ecb8ae02f9..bbe0bb2cc7 100644
--- a/src/corelib/kernel/qjnitypes.h
+++ b/src/corelib/kernel/qjnitypes.h
@@ -349,17 +349,22 @@ constexpr auto QtJniTypes::typeSignature<QtJniTypes::Type>() \
+ QtJniTypes::String(";"); \
} \
+#define Q_DECLARE_JNI_NATIVE_METHOD(...) \
+ QT_OVERLOADED_MACRO(QT_DECLARE_JNI_NATIVE_METHOD, __VA_ARGS__) \
-#define Q_DECLARE_JNI_NATIVE_METHOD(Method) \
+#define QT_DECLARE_JNI_NATIVE_METHOD_2(Method, Name) \
namespace QtJniMethods { \
static constexpr auto Method##_signature = \
QtJniTypes::nativeMethodSignature(Method); \
static const JNINativeMethod Method##_method = { \
- #Method, Method##_signature.data(), \
+ #Name, Method##_signature.data(), \
reinterpret_cast<void *>(Method) \
}; \
} \
+#define QT_DECLARE_JNI_NATIVE_METHOD_1(Method) \
+ QT_DECLARE_JNI_NATIVE_METHOD_2(Method, Method) \
+
#define Q_JNI_NATIVE_METHOD(Method) QtJniMethods::Method##_method
QT_END_NAMESPACE
diff --git a/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java b/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
index 0fc70f2567..342342c548 100644
--- a/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
+++ b/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
@@ -6,6 +6,7 @@ package org.qtproject.qt.android.testdatapackage;
public class QtJniEnvironmentTestClass
{
private static native void callbackFromJava(String message);
+ private static native void namedCallbackFromJava(String message);
private static native void intCallbackFromJava(int value);
public final int INT_FIELD = 123;
@@ -18,6 +19,11 @@ public class QtJniEnvironmentTestClass
callbackFromJava("From Java: " + message);
}
+ public static void namedAppendJavaToString(String message)
+ {
+ namedCallbackFromJava("From Java (named): " + message);
+ }
+
public static void convertToInt(String message)
{
intCallbackFromJava(Integer.parseInt(message));
diff --git a/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp b/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
index 76cad4ddc0..4809505f47 100644
--- a/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
+++ b/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
@@ -101,6 +101,13 @@ static void callbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
}
Q_DECLARE_JNI_NATIVE_METHOD(callbackFromJava);
+static void tediouslyLongNamed_callbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
+{
+ Q_UNUSED(env)
+ registerNativesString = QJniObject(value).toString();
+}
+Q_DECLARE_JNI_NATIVE_METHOD(tediouslyLongNamed_callbackFromJava, namedCallbackFromJava)
+
static void callbackFromJavaNoCtor(JNIEnv *env, jobject /*thiz*/, jstring value)
{
Q_UNUSED(env)
@@ -126,6 +133,20 @@ void tst_QJniEnvironment::registerNativeMethods()
QVERIFY(registerNativesString == QStringLiteral("From Java: Qt"));
}
+ // Named native function
+ {
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_METHOD(tediouslyLongNamed_callbackFromJava)
+ }));
+
+ QJniObject::callStaticMethod<void>(javaTestClass,
+ "namedAppendJavaToString",
+ "(Ljava/lang/String;)V",
+ QtString.object<jstring>());
+ QTest::qWait(200);
+ QVERIFY(registerNativesString == QStringLiteral("From Java (named): Qt"));
+ }
+
// No default constructor in class
{
QVERIFY(env.registerNativeMethods(javaTestClassNoCtor, {