JNI: move private QJniObject helpers out of the public header

Functions that are not called by any of the inline code don't have to be
in the public header. Move them as static functions into the translation
unit. Remove some useless wrappers in the private, and move the code for
private and static functions together.

Some private helpers have to stay in the ABI as they used to be called
by at least one public inline function up to Qt 6.6. Remove them from
the API using QT_CORE_REMOVED_SINCE.

Change-Id: I7eb7b2ba994dfda9de11e2d090a70842dad17247
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-09-20 23:20:34 +02:00
parent 39294317e0
commit b5dd6d98bf
3 changed files with 50 additions and 34 deletions

View File

@ -618,6 +618,27 @@ QStringView QXmlStreamAttributes::value(QLatin1StringView qualifiedName) const
#if QT_CORE_REMOVED_SINCE(6, 7) #if QT_CORE_REMOVED_SINCE(6, 7)
#if defined(Q_OS_ANDROID)
#include "qjniobject.h"
jclass QJniObject::loadClass(const QByteArray &className, JNIEnv *env, bool /*binEncoded*/)
{
return QJniObject::loadClass(className, env);
}
QByteArray QJniObject::toBinaryEncClassName(const QByteArray &className)
{
return QByteArray(className).replace('/', '.');
}
void QJniObject::callVoidMethodV(JNIEnv *env, jmethodID id, va_list args) const
{
env->CallVoidMethodV(javaObject(), id, args);
}
#endif // Q_OS_ANDROID
#include "qlocale.h" #include "qlocale.h"
QStringList QLocale::uiLanguages() const QStringList QLocale::uiLanguages() const

View File

@ -309,9 +309,27 @@ static jclass getCachedClass(const QByteArray &className)
return it != cachedClasses->constEnd() ? it.value() : nullptr; return it != cachedClasses->constEnd() ? it.value() : nullptr;
} }
QByteArray QJniObject::toBinaryEncClassName(const QByteArray &className) /*!
\internal
Get a JNI object from a jobject variant and do the necessary
exception clearing and delete the local reference before returning.
The JNI object can be null if there was an exception.
*/
static QJniObject getCleanJniObject(jobject object)
{ {
return QByteArray(className).replace('/', '.'); if (!object)
return QJniObject();
QJniEnvironment env;
if (env.checkAndClearExceptions()) {
env->DeleteLocalRef(object);
return QJniObject();
}
QJniObject res(object);
env->DeleteLocalRef(object);
return res;
} }
/*! /*!
@ -370,7 +388,7 @@ jclass QtAndroidPrivate::findClass(const char *className, JNIEnv *env)
return clazz; return clazz;
} }
jclass QJniObject::loadClass(const QByteArray &className, JNIEnv *env, bool /*binEncoded*/) jclass QJniObject::loadClass(const QByteArray &className, JNIEnv *env)
{ {
return QtAndroidPrivate::findClass(className, env); return QtAndroidPrivate::findClass(className, env);
} }
@ -398,13 +416,8 @@ void QJniObject::callVoidMethodV(JNIEnv *env, jmethodID id, ...) const
{ {
va_list args; va_list args;
va_start(args, id); va_start(args, id);
callVoidMethodV(env, id, args);
va_end(args);
}
void QJniObject::callVoidMethodV(JNIEnv *env, jmethodID id, va_list args) const
{
env->CallVoidMethodV(d->m_jobject, id, args); env->CallVoidMethodV(d->m_jobject, id, args);
va_end(args);
} }
jmethodID QJniObject::getCachedMethodID(JNIEnv *env, jmethodID QJniObject::getCachedMethodID(JNIEnv *env,
@ -711,27 +724,6 @@ QJniObject::QJniObject(jobject object)
This function is only available if all \a args are known \l {JNI Types}. This function is only available if all \a args are known \l {JNI Types}.
*/ */
/*!
\brief Get a JNI object from a jobject variant and do the necessary
exception clearing and delete the local reference before returning.
The JNI object can be null if there was an exception.
*/
QJniObject QJniObject::getCleanJniObject(jobject object)
{
if (!object)
return QJniObject();
QJniEnvironment env;
if (env.checkAndClearExceptions()) {
env->DeleteLocalRef(object);
return QJniObject();
}
QJniObject res(object);
env->DeleteLocalRef(object);
return res;
}
/*! /*!
\fn QJniObject::~QJniObject() \fn QJniObject::~QJniObject()

View File

@ -528,9 +528,14 @@ public:
} }
private: private:
static jclass loadClass(const QByteArray &className, JNIEnv *env, bool binEncoded = false); static jclass loadClass(const QByteArray &className, JNIEnv *env);
#if QT_CORE_REMOVED_SINCE(6, 7)
// these need to stay in the ABI as they were used in inline methods before 6.7
static jclass loadClass(const QByteArray &className, JNIEnv *env, bool binEncoded);
static QByteArray toBinaryEncClassName(const QByteArray &className); static QByteArray toBinaryEncClassName(const QByteArray &className);
static QJniObject getCleanJniObject(jobject obj); void callVoidMethodV(JNIEnv *env, jmethodID id, va_list args) const;
#endif
static jfieldID getCachedFieldID(JNIEnv *env, jclass clazz, const QByteArray &className, static jfieldID getCachedFieldID(JNIEnv *env, jclass clazz, const QByteArray &className,
const char *name, const char *signature, const char *name, const char *signature,
@ -549,8 +554,6 @@ private:
const char *signature, bool isStatic = false); const char *signature, bool isStatic = false);
void callVoidMethodV(JNIEnv *env, jmethodID id, ...) const; void callVoidMethodV(JNIEnv *env, jmethodID id, ...) const;
// ### Qt 7: merge into ... overload
void callVoidMethodV(JNIEnv *env, jmethodID id, va_list args) const;
bool isSameObject(jobject obj) const; bool isSameObject(jobject obj) const;
bool isSameObject(const QJniObject &other) const; bool isSameObject(const QJniObject &other) const;