From a085a14d76553ebd1fa4a4a11a27110ee544a531 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 20 Apr 2022 17:07:42 +0200 Subject: Generate JNI signature strings at compile time Introduce an internal QtJniTypes namespace with types that allow us to concatenate string literals at compile time. This makes it possible to generate arbitrary strings based on types, which we can then use as signatures to JNI method calls. Move some of the private members of QJniObject into the QtJniTypes namespace for consistency, and to allow further template specialization by user code to make other types and their JNI signature string known. Remove the "Jni" prefix from names. Use the compile-time generated string in QJniObject methods that created the signature string at runtime, which involved a temporary memory allocation. Treat 'void' as a primitive type (with signature string 'V'), and remove redundant template specializations. Add a test case to verify the the strings are constructed correctly at compile time. Change-Id: I5e3895a97f7dc1b86961f7a7855b899d9203037d Reviewed-by: Assam Boudjelthia Reviewed-by: Marc Mutz Reviewed-by: Fabian Kosmale --- tests/auto/corelib/kernel/CMakeLists.txt | 1 + tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt | 4 + .../corelib/kernel/qjnitypes/tst_qjnitypes.cpp | 118 +++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt create mode 100644 tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp (limited to 'tests/auto/corelib/kernel') diff --git a/tests/auto/corelib/kernel/CMakeLists.txt b/tests/auto/corelib/kernel/CMakeLists.txt index 18f20bfadc..3083652945 100644 --- a/tests/auto/corelib/kernel/CMakeLists.txt +++ b/tests/auto/corelib/kernel/CMakeLists.txt @@ -49,4 +49,5 @@ endif() if(ANDROID) add_subdirectory(qjnienvironment) add_subdirectory(qjniobject) + add_subdirectory(qjnitypes) endif() diff --git a/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt b/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt new file mode 100644 index 0000000000..8184e91a11 --- /dev/null +++ b/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt @@ -0,0 +1,4 @@ +qt_internal_add_test(tst_qjnitypes + SOURCES + tst_qjnitypes.cpp +) diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp new file mode 100644 index 0000000000..559fcd47c9 --- /dev/null +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include + +class tst_QJniTypes : public QObject +{ + Q_OBJECT + +public: + tst_QJniTypes() = default; + +private slots: + void initTestCase(); +}; + +struct QtJavaWrapper {}; +template<> +constexpr auto QtJniTypes::typeSignature() +{ + return QtJniTypes::String("Lorg/qtproject/qt/android/QtJavaWrapper;"); +} + +template<> +constexpr auto QtJniTypes::typeSignature() +{ + return QtJniTypes::String("Ljava/lang/Object;"); +} + +struct QtCustomJniObject : QJniObject {}; +template<> +constexpr auto QtJniTypes::typeSignature() +{ + return QtJniTypes::String("Lorg/qtproject/qt/android/QtCustomJniObject;"); +} + +static_assert(QtJniTypes::typeSignature() == "Lorg/qtproject/qt/android/QtJavaWrapper;"); +static_assert(QtJniTypes::typeSignature() != "Ljava/lang/Object;"); +static_assert(!(QtJniTypes::typeSignature() == "X")); + +static_assert(QtJniTypes::fieldSignature() == "I"); +static_assert(QtJniTypes::fieldSignature() != "X"); +static_assert(QtJniTypes::fieldSignature() != "Ljava/lang/Object;"); +static_assert(QtJniTypes::fieldSignature() == "J"); +static_assert(QtJniTypes::fieldSignature() == "Ljava/lang/String;"); +static_assert(QtJniTypes::fieldSignature() == "Ljava/lang/Object;"); +static_assert(QtJniTypes::fieldSignature() == "[Ljava/lang/Object;"); +static_assert(QtJniTypes::fieldSignature() == "Ljava/lang/Object;"); +static_assert(QtJniTypes::fieldSignature() == "Lorg/qtproject/qt/android/QtJavaWrapper;"); +static_assert(QtJniTypes::fieldSignature() == "Lorg/qtproject/qt/android/QtCustomJniObject;"); + +static_assert(QtJniTypes::methodSignature() == "()V"); +static_assert(QtJniTypes::methodSignature() != "()X"); +static_assert(QtJniTypes::methodSignature() == "(I)V"); +static_assert(QtJniTypes::methodSignature() == "(ILjava/lang/String;)V"); +static_assert(QtJniTypes::methodSignature() == "(ILjava/lang/Class;)J"); +static_assert(QtJniTypes::methodSignature() == "(ILjava/lang/String;)Ljava/lang/Object;"); + +static_assert(QtJniTypes::isPrimitiveType()); +static_assert(QtJniTypes::isPrimitiveType()); +static_assert(!QtJniTypes::isPrimitiveType()); +static_assert(!QtJniTypes::isPrimitiveType()); + +static_assert(!QtJniTypes::isObjectType()); +static_assert(!QtJniTypes::isObjectType()); +static_assert(QtJniTypes::isObjectType()); +static_assert(QtJniTypes::isObjectType()); + +static_assert(QtJniTypes::String("ABCDE").startsWith("ABC")); +static_assert(QtJniTypes::String("ABCDE").startsWith("A")); +static_assert(QtJniTypes::String("ABCDE").startsWith("ABCDE")); +static_assert(!QtJniTypes::String("ABCDE").startsWith("ABCDEF")); +static_assert(!QtJniTypes::String("ABCDE").startsWith("9AB")); +static_assert(QtJniTypes::String("ABCDE").startsWith('A')); +static_assert(!QtJniTypes::String("ABCDE").startsWith('B')); + +static_assert(QtJniTypes::String("ABCDE").endsWith("CDE")); +static_assert(QtJniTypes::String("ABCDE").endsWith("E")); +static_assert(QtJniTypes::String("ABCDE").endsWith("ABCDE")); +static_assert(!QtJniTypes::String("ABCDE").endsWith("DEF")); +static_assert(!QtJniTypes::String("ABCDE").endsWith("ABCDEF")); +static_assert(QtJniTypes::String("ABCDE").endsWith('E')); +static_assert(!QtJniTypes::String("ABCDE").endsWith('F')); + +void tst_QJniTypes::initTestCase() +{ +} + +QTEST_MAIN(tst_QJniTypes) + +#include "tst_qjnitypes.moc" -- cgit v1.2.3