Android: Refactor exception checking in qjni.

In places where we call java/jni functions that can throw exceptions,
we check and clear the exception, if necessary. This change moves
the "checking" code into a single (private) function.

Change-Id: Ic3de2be51305972b096e1ed0a477e341eb5d9404
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
bb10
Christian Strømme 2014-06-12 14:49:37 +02:00 committed by Christian Stromme
parent a2948e1387
commit f17494e9b8
1 changed files with 16 additions and 19 deletions

View File

@ -67,6 +67,19 @@ static QString qt_convertJString(jstring string)
return res;
}
static inline bool exceptionCheckAndClear(JNIEnv *env)
{
if (Q_UNLIKELY(env->ExceptionCheck())) {
#ifdef QT_DEBUG
env->ExceptionDescribe();
#endif // QT_DEBUG
env->ExceptionClear();
return true;
}
return false;
}
typedef QHash<QString, jclass> JClassHash;
Q_GLOBAL_STATIC(JClassHash, cachedClasses)
@ -85,14 +98,8 @@ static jclass getCachedClass(JNIEnv *env, const char *className)
QJNIObjectPrivate classObject = classLoader.callObjectMethod("loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;",
stringName.object());
if (env->ExceptionCheck()) {
#ifdef QT_DEBUG
env->ExceptionDescribe();
#endif // QT_DEBUG
env->ExceptionClear();
}
if (classObject.isValid())
if (!exceptionCheckAndClear(env) && classObject.isValid())
clazz = static_cast<jclass>(env->NewGlobalRef(classObject.object()));
cachedClasses->insert(key, clazz);
@ -121,13 +128,8 @@ static jmethodID getCachedMethodID(JNIEnv *env,
else
id = env->GetMethodID(clazz, name, sig);
if (env->ExceptionCheck()) {
if (exceptionCheckAndClear(env))
id = 0;
#ifdef QT_DEBUG
env->ExceptionDescribe();
#endif // QT_DEBUG
env->ExceptionClear();
}
cachedMethodID->insert(key, id);
} else {
@ -154,13 +156,8 @@ static jfieldID getCachedFieldID(JNIEnv *env,
else
id = env->GetFieldID(clazz, name, sig);
if (env->ExceptionCheck()) {
if (exceptionCheckAndClear(env))
id = 0;
#ifdef QT_DEBUG
env->ExceptionDescribe();
#endif // QT_DEBUG
env->ExceptionClear();
}
cachedFieldID->insert(key, id);
} else {