diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2024-03-04 18:33:07 +0100 |
---|---|---|
committer | Rami Potinkara <rami.potinkara@qt.io> | 2024-03-06 13:00:19 +0000 |
commit | 10afa38aa44231b3617984fdbca66d9699e2825f (patch) | |
tree | b06218609e4eccde673013a01f703ca44a244a5d /tests/auto/corelib/kernel/qjnitypes | |
parent | 8103d29e94f90131ee0ad69fc312c1e62e74a6a9 (diff) |
JNI: Fix error with overload resolution when passing string types
The variadic templates are supposed to be removed from the
overload set when any of the parameters is a literal string type,
as otherwise we get conflicts with the legacy overload taking
class names and signatures as const char *. The detection of
a literal string types was missing a few specializations, so that
we ended up with the wrong overload being called, and class
names getting interpreted as method names instead.
Add the missing specializations, and add more test coverage
for using the old overloads.
Task-number: QTBUG-122235
Pick-to: 6.7
Change-Id: I5488f2009c8f62d74fac6754844f57cf64011414
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Rami Potinkara <rami.potinkara@qt.io>
Reviewed-by: Lauri Pohjanheimo <lauri.pohjanheimo@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qjnitypes')
-rw-r--r-- | tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp index f5a0ae7d75..bf582041f3 100644 --- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -22,6 +22,7 @@ private slots: void initTestCase(); void nativeMethod(); void construct(); + void stringTypeCantBeArgument(); }; struct QtJavaWrapper {}; @@ -227,6 +228,57 @@ void tst_QJniTypes::construct() QCOMPARE(str3.toString(), text); } +template <typename ...Arg> +static constexpr bool isValidArgument(Arg &&...) noexcept +{ + return QtJniTypes::ValidSignatureTypesDetail<q20::remove_cvref_t<Arg>...>; +} + +enum class Overload +{ + ClassNameAndMethod, + OnlyMethod, +}; + +template <typename Ret, typename ...Args +#ifndef Q_QDOC + , QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true +#endif +> +static constexpr auto callStaticMethod(const char *className, const char *methodName, Args &&...) +{ + Q_UNUSED(className); + Q_UNUSED(methodName); + return Overload::ClassNameAndMethod; +} + +template <typename Klass, typename Ret, typename ...Args +#ifndef Q_QDOC + , QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true +#endif +> +static constexpr auto callStaticMethod(const char *methodName, Args &&...) +{ + Q_UNUSED(methodName); + return Overload::OnlyMethod; +} + +void tst_QJniTypes::stringTypeCantBeArgument() +{ + const char *methodName = "staticEchoMethod"; + + static_assert(!isValidArgument(QtJniTypes::Traits<QtJniTypes::JavaType>::className())); + static_assert(!isValidArgument("someFunctionName")); + static_assert(!isValidArgument(methodName)); + static_assert(!isValidArgument(QtJniTypes::Traits<QtJniTypes::JavaType>::className(), + "someFunctionName", methodName, 42)); + + static_assert(callStaticMethod<jstring, jint>("class name", "method name", 42) + == Overload::ClassNameAndMethod); + static_assert(callStaticMethod<QtJniTypes::JavaType, jint>("method name", 42) + == Overload::OnlyMethod); +} + QTEST_MAIN(tst_QJniTypes) #include "tst_qjnitypes.moc" |