Index: base/android/jni_android.cc |
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc |
index d3b4e79058381652baf17f512ce57c2c8b2fb0fe..7badb416b4fd072affe371be72c6abfcb178d31d 100644 |
--- a/base/android/jni_android.cc |
+++ b/base/android/jni_android.cc |
@@ -96,6 +96,37 @@ std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
return ConvertJavaStringToUTF8(exception_string); |
} |
+ |
+enum MethodType { |
+ METHODTYPE_STATIC, |
+ METHODTYPE_NORMAL, |
+}; |
+ |
+enum ExceptionCheck { |
+ EXCEPTIIONCHECK_YES, |
+ EXCEPTIIONCHECK_NO, |
+}; |
+ |
+jmethodID GetMethodIDInternal(JNIEnv* env, |
+ jclass clazz, |
+ const char* method_name, |
+ const char* jni_signature, |
+ MethodType method_type, |
+ ExceptionCheck exception_check) { |
joth
2012/10/01 16:15:27
for bonus points these enums could be template arg
bulach
2012/10/01 17:25:00
would that make any of this, or the caller code, c
joth
2012/10/01 17:57:19
Possibly. It clearly communicates which params we
|
+ jmethodID method_id = method_type == METHODTYPE_STATIC ? |
+ env->GetMethodID(clazz, method_name, jni_signature) : |
+ env->GetStaticMethodID(clazz, method_name, jni_signature); |
+ if (exception_check == EXCEPTIIONCHECK_YES) { |
+ CHECK(method_id && base::android::ClearException(env)) << |
+ "Failed to find " << |
+ (method_type == METHODTYPE_STATIC ? " static " : " ") << |
joth
2012/10/01 16:15:27
uber-nit: you don't need the space before 'static'
bulach
2012/10/01 17:25:00
Done.
|
+ "method " << method_name << " " << jni_signature; |
+ } else if (base::android::HasException(env)) { |
+ env->ExceptionClear(); |
+ } |
+ return method_id; |
+} |
+ |
} // namespace |
namespace base { |
@@ -165,11 +196,18 @@ jmethodID GetMethodID(JNIEnv* env, |
jclass clazz, |
const char* method_name, |
const char* jni_signature) { |
- jmethodID method_id = |
- env->GetMethodID(clazz, method_name, jni_signature); |
- CHECK(method_id && !ClearException(env)) << "Failed to find method " << |
- method_name << " " << jni_signature; |
- return method_id; |
+ return GetMethodIDInternal( |
+ env, clazz, method_name, jni_signature, |
+ METHODTYPE_NORMAL, EXCEPTIIONCHECK_YES); |
+} |
+ |
+jmethodID GetMethodIDOrNull(JNIEnv* env, |
+ jclass clazz, |
+ const char* method_name, |
+ const char* jni_signature) { |
+ return GetMethodIDInternal( |
+ env, clazz, method_name, jni_signature, |
+ METHODTYPE_NORMAL, EXCEPTIIONCHECK_NO); |
} |
jmethodID GetStaticMethodID(JNIEnv* env, |
@@ -184,11 +222,18 @@ jmethodID GetStaticMethodID(JNIEnv* env, |
jclass clazz, |
const char* method_name, |
const char* jni_signature) { |
- jmethodID method_id = |
- env->GetStaticMethodID(clazz, method_name, jni_signature); |
- CHECK(method_id && !ClearException(env)) << "Failed to find static method " << |
- method_name << " " << jni_signature; |
- return method_id; |
+ return GetMethodIDInternal( |
+ env, clazz, method_name, jni_signature, |
+ METHODTYPE_STATIC, EXCEPTIIONCHECK_YES); |
+} |
+ |
+jmethodID GetStaticMethodIDOrNull(JNIEnv* env, |
+ jclass clazz, |
+ const char* method_name, |
+ const char* jni_signature) { |
+ return GetMethodIDInternal( |
+ env, clazz, method_name, jni_signature, |
+ METHODTYPE_STATIC, EXCEPTIIONCHECK_NO); |
} |
bool HasMethod(JNIEnv* env, |