summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2023-09-14 12:47:09 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2023-09-26 19:58:25 +0200
commitf826d1615a3446dc070a639e99f09a877eff6c5d (patch)
tree311485b48f79f89b77bb50f41b45e47e8f7dd820 /tests/auto/corelib/kernel
parent4702208b406874974439ac12a198587caf3f4503 (diff)
JNI: Implicitly convert QString to jstring in API calls
Support this both for parameters and return types, in all template functions for calling methods or accessing fields. To manage the life-time of the temporary objects, create a local stack frame in the JVM around the calling function. Popping that implicilty releases all local refernces, so that we don't have to worry about doing so explicilty. Adding a local reference to the temporary jstring objects is then enough for the object to live as long as it's needed. The LocalFrame RAII-like type needs a QJniEnvironment to push and pop the frame in the JVM, so we don't need a local QJniEnvironment anymore. Reduce code duplication a bit as a drive-by, and add test coverage. Change-Id: I801a1b006eea5af02f57d8bb7cca089508dadd1d Reviewed-by: Tinja Paavoseppä <tinja.paavoseppa@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel')
-rw-r--r--tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp37
1 files changed, 33 insertions, 4 deletions
diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
index cc3ebc658e..3d8c15d0b0 100644
--- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
+++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
@@ -187,8 +187,10 @@ void tst_QJniObject::ctor()
void tst_QJniObject::callMethodTest()
{
{
- QJniObject jString1 = QJniObject::fromString(QLatin1String("Hello, Java"));
- QJniObject jString2 = QJniObject::fromString(QLatin1String("hELLO, jAVA"));
+ const QString qString1 = u"Hello, Java"_s;
+ const QString qString2 = u"hELLO, jAVA"_s;
+ QJniObject jString1 = QJniObject::fromString(qString1);
+ QJniObject jString2 = QJniObject::fromString(qString2);
QVERIFY(jString1 != jString2);
const jboolean isEmpty = jString1.callMethod<jboolean>("isEmpty");
@@ -201,6 +203,10 @@ void tst_QJniObject::callMethodTest()
ret = jString1.callMethod<jint>("compareToIgnoreCase", jString2.object<jstring>());
QVERIFY(0 == ret);
+
+ // as of Qt 6.7, we can pass QString directly
+ ret = jString1.callMethod<jint>("compareToIgnoreCase", qString2);
+ QVERIFY(0 == ret);
}
{
@@ -219,6 +225,13 @@ void tst_QJniObject::callMethodTest()
QJniObject subString = jString.callMethod<jstring>("substring", 0, 4);
QCOMPARE(subString.toString(), qString.mid(0, 4));
+
+ // and as of Qt 6.7, we can return and take QString directly
+ QCOMPARE(jString.callMethod<QString>("substring", 0, 4), qString.mid(0, 4));
+
+ QCOMPARE(jString.callMethod<jstring>("substring", 0, 7)
+ .callMethod<jstring>("toUpperCase")
+ .callMethod<QString>("concat", u"C++"_s), u"HELLO, C++"_s);
}
}
@@ -346,6 +359,11 @@ void tst_QJniObject::callStaticObjectMethod()
QVERIFY(returnValue.isValid());
QCOMPARE(returnValue.toString(), string);
+ // from 6.7 we can pass QString directly, both as parameters and return type
+ QString result = QJniObject::callStaticMethod<jstring, QString>("format",
+ string,
+ jobjectArray(0));
+ QCOMPARE(result, string);
}
void tst_QJniObject::callStaticObjectMethodById()
@@ -1053,11 +1071,16 @@ void tst_QJniObject::setObjectField()
QJniObject obj(testClassName);
QVERIFY(obj.isValid());
- QJniObject testValue = QJniObject::fromString(QStringLiteral("Hello"));
+ const QString qString = u"Hello"_s;
+ QJniObject testValue = QJniObject::fromString(qString);
obj.setField("STRING_OBJECT_VAR", testValue.object<jstring>());
QJniObject res = obj.getObjectField<jstring>("STRING_OBJECT_VAR");
QCOMPARE(res.toString(), testValue.toString());
+
+ // as of Qt 6.7, we can set and get strings directly
+ obj.setField("STRING_OBJECT_VAR", qString);
+ QCOMPARE(obj.getField<QString>("STRING_OBJECT_VAR"), qString);
}
template <typename T>
@@ -1127,11 +1150,17 @@ void tst_QJniObject::setStaticBooleanField()
void tst_QJniObject::setStaticObjectField()
{
- QJniObject testValue = QJniObject::fromString(QStringLiteral("Hello"));
+ const QString qString = u"Hello"_s;
+ QJniObject testValue = QJniObject::fromString(qString);
QJniObject::setStaticField(testClassName, "S_STRING_OBJECT_VAR", testValue.object<jstring>());
QJniObject res = QJniObject::getStaticObjectField<jstring>(testClassName, "S_STRING_OBJECT_VAR");
QCOMPARE(res.toString(), testValue.toString());
+
+ // as of Qt 6.7, we can set and get strings directly
+ using namespace QtJniTypes;
+ QJniObject::setStaticField<QtJniObjectTestClass>("S_STRING_OBJECT_VAR", qString);
+ QCOMPARE((QJniObject::getStaticField<QtJniObjectTestClass, QString>("S_STRING_OBJECT_VAR")), qString);
}
void tst_QJniObject::templateApiCheck()