JNI: remove implicit conversion of declared QtJniTypes to jobject

It's dangerous, as it results in implicit conversion to void * or bool,
while hiding the object life time implications.
Such objects are QJniObject subclasses, so make sure we don't end up
in the catch-all assignment operator when assigning. Constrain that
operator to types that are convertible to jobject.

Change-Id: If31761c00a678490eeb0b2b038e89e78d8a51747
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
bb10
Volker Hilsheimer 2023-10-26 18:38:51 +02:00
parent 8c9f5fba03
commit 180c7698e2
2 changed files with 2 additions and 3 deletions

View File

@ -534,7 +534,8 @@ public:
// This function takes ownership of the jobject and releases the local ref. before returning.
static QJniObject fromLocalRef(jobject lref);
template <typename T>
template <typename T,
std::enable_if_t<std::is_convertible_v<T, jobject>, bool> = true>
QJniObject &operator=(T obj)
{
assign(static_cast<T>(obj));

View File

@ -21,8 +21,6 @@ template<typename Type>
struct Object : QJniObject
{
using Class = Type;
operator jobject() const noexcept { return object(); }
Q_IMPLICIT Object(jobject object) : QJniObject(object) {}
Q_IMPLICIT Object(const QJniObject &object) : QJniObject(object) {}
Q_IMPLICIT Object(QJniObject &&object) : QJniObject(std::move(object)) {}