From f17494e9b829f079bf632eed7757c1a30f360b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Thu, 12 Jun 2014 14:49:37 +0200 Subject: 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 --- src/corelib/kernel/qjni.cpp | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qjni.cpp b/src/corelib/kernel/qjni.cpp index 437205bf92..aa9b196e62 100644 --- a/src/corelib/kernel/qjni.cpp +++ b/src/corelib/kernel/qjni.cpp @@ -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 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(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 { -- cgit v1.2.3