Frameworks/base: Replace LOG_FATAL_IF in core/jni
Do not use LOG_FATAL_IF in JNI setup. This is one-time on startup
and important enough to always check.
Add a header with common helper definitions. Move to inlined functions
instead of macros to clean up the code.
Change-Id: Ib12d0eed61b110c45d748e80ec36c563e9dec7e5
diff --git a/core/jni/android_app_NativeActivity.cpp b/core/jni/android_app_NativeActivity.cpp
index 633a207..a65b690 100644
--- a/core/jni/android_app_NativeActivity.cpp
+++ b/core/jni/android_app_NativeActivity.cpp
@@ -40,6 +40,8 @@
#include "nativebridge/native_bridge.h"
+#include "core_jni_helpers.h"
+
#define LOG_TRACE(...)
//#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
@@ -632,39 +634,20 @@
static const char* const kNativeActivityPathName = "android/app/NativeActivity";
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class %s", className);
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName);
-
int register_android_app_NativeActivity(JNIEnv* env)
{
//ALOGD("register_android_app_NativeActivity");
- jclass clazz;
- FIND_CLASS(clazz, kNativeActivityPathName);
+ jclass clazz = FindClassOrDie(env, kNativeActivityPathName);
- GET_METHOD_ID(gNativeActivityClassInfo.finish,
- clazz,
- "finish", "()V");
- GET_METHOD_ID(gNativeActivityClassInfo.setWindowFlags,
- clazz,
- "setWindowFlags", "(II)V");
- GET_METHOD_ID(gNativeActivityClassInfo.setWindowFormat,
- clazz,
- "setWindowFormat", "(I)V");
- GET_METHOD_ID(gNativeActivityClassInfo.showIme,
- clazz,
- "showIme", "(I)V");
- GET_METHOD_ID(gNativeActivityClassInfo.hideIme,
- clazz,
- "hideIme", "(I)V");
+ gNativeActivityClassInfo.finish = GetMethodIDOrDie(env, clazz, "finish", "()V");
+ gNativeActivityClassInfo.setWindowFlags = GetMethodIDOrDie(env, clazz, "setWindowFlags",
+ "(II)V");
+ gNativeActivityClassInfo.setWindowFormat = GetMethodIDOrDie(env, clazz, "setWindowFormat",
+ "(I)V");
+ gNativeActivityClassInfo.showIme = GetMethodIDOrDie(env, clazz, "showIme", "(I)V");
+ gNativeActivityClassInfo.hideIme = GetMethodIDOrDie(env, clazz, "hideIme", "(I)V");
- return AndroidRuntime::registerNativeMethods(
- env, kNativeActivityPathName,
- g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, kNativeActivityPathName, g_methods, NELEM(g_methods));
}
} // namespace android
diff --git a/core/jni/android_app_backup_FullBackup.cpp b/core/jni/android_app_backup_FullBackup.cpp
index 5e334c9..2c02b37 100644
--- a/core/jni/android_app_backup_FullBackup.cpp
+++ b/core/jni/android_app_backup_FullBackup.cpp
@@ -23,6 +23,8 @@
#include <androidfw/BackupHelpers.h>
+#include "core_jni_helpers.h"
+
#include <string.h>
namespace android
@@ -116,15 +118,11 @@
int register_android_app_backup_FullBackup(JNIEnv* env)
{
- jclass clazz = env->FindClass("android/app/backup/BackupDataOutput");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.app.backup.BackupDataOutput");
+ jclass clazz = FindClassOrDie(env, "android/app/backup/BackupDataOutput");
- sBackupDataOutput.mBackupWriter = env->GetFieldID(clazz, "mBackupWriter", "J");
- LOG_FATAL_IF(sBackupDataOutput.mBackupwriter == NULL,
- "Unable to find mBackupWriter field in android.app.backup.BackupDataOutput");
+ sBackupDataOutput.mBackupWriter = GetFieldIDOrDie(env, clazz, "mBackupWriter", "J");
- return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FullBackup",
- g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, "android/app/backup/FullBackup", g_methods, NELEM(g_methods));
}
}
diff --git a/core/jni/android_backup_BackupDataInput.cpp b/core/jni/android_backup_BackupDataInput.cpp
index 90763b0..096a784 100644
--- a/core/jni/android_backup_BackupDataInput.cpp
+++ b/core/jni/android_backup_BackupDataInput.cpp
@@ -22,6 +22,8 @@
#include <androidfw/BackupHelpers.h>
+#include "core_jni_helpers.h"
+
namespace android
{
@@ -135,17 +137,12 @@
{
//ALOGD("register_android_backup_BackupDataInput");
- jclass clazz = env->FindClass("android/app/backup/BackupDataInput$EntityHeader");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.app.backup.BackupDataInput.EntityHeader");
- s_keyField = env->GetFieldID(clazz, "key", "Ljava/lang/String;");
- LOG_FATAL_IF(s_keyField == NULL,
- "Unable to find key field in android.app.backup.BackupDataInput.EntityHeader");
- s_dataSizeField = env->GetFieldID(clazz, "dataSize", "I");
- LOG_FATAL_IF(s_dataSizeField == NULL,
- "Unable to find dataSize field in android.app.backup.BackupDataInput.EntityHeader");
+ jclass clazz = FindClassOrDie(env, "android/app/backup/BackupDataInput$EntityHeader");
+ s_keyField = GetFieldIDOrDie(env, clazz, "key", "Ljava/lang/String;");
+ s_dataSizeField = GetFieldIDOrDie(env, clazz, "dataSize", "I");
- return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataInput",
- g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, "android/app/backup/BackupDataInput", g_methods,
+ NELEM(g_methods));
}
}
diff --git a/core/jni/android_backup_BackupHelperDispatcher.cpp b/core/jni/android_backup_BackupHelperDispatcher.cpp
index a8b7d44..80bdaf8 100644
--- a/core/jni/android_backup_BackupHelperDispatcher.cpp
+++ b/core/jni/android_backup_BackupHelperDispatcher.cpp
@@ -24,6 +24,7 @@
#include <sys/uio.h>
#include <unistd.h>
+#include "core_jni_helpers.h"
#define VERSION_1_HEADER 0x01706c48 // 'Hlp'1 little endian
@@ -227,18 +228,12 @@
int register_android_backup_BackupHelperDispatcher(JNIEnv* env)
{
- jclass clazz = env->FindClass("android/app/backup/BackupHelperDispatcher$Header");
- LOG_FATAL_IF(clazz == NULL,
- "Unable to find class android.app.backup.BackupHelperDispatcher.Header");
- s_chunkSizeField = env->GetFieldID(clazz, "chunkSize", "I");
- LOG_FATAL_IF(s_chunkSizeField == NULL,
- "Unable to find chunkSize field in android.app.backup.BackupHelperDispatcher.Header");
- s_keyPrefixField = env->GetFieldID(clazz, "keyPrefix", "Ljava/lang/String;");
- LOG_FATAL_IF(s_keyPrefixField == NULL,
- "Unable to find keyPrefix field in android.app.backup.BackupHelperDispatcher.Header");
+ jclass clazz = FindClassOrDie(env, "android/app/backup/BackupHelperDispatcher$Header");
+ s_chunkSizeField = GetFieldIDOrDie(env, clazz, "chunkSize", "I");
+ s_keyPrefixField = GetFieldIDOrDie(env, clazz, "keyPrefix", "Ljava/lang/String;");
- return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupHelperDispatcher",
- g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, "android/app/backup/BackupHelperDispatcher", g_methods,
+ NELEM(g_methods));
}
}
diff --git a/core/jni/android_content_res_Configuration.cpp b/core/jni/android_content_res_Configuration.cpp
index 201ffe8..3b45e72 100644
--- a/core/jni/android_content_res_Configuration.cpp
+++ b/core/jni/android_content_res_Configuration.cpp
@@ -23,6 +23,8 @@
#include <android_runtime/android_content_res_Configuration.h>
#include "android_runtime/AndroidRuntime.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -70,49 +72,27 @@
gConfigurationClassInfo.smallestScreenWidthDp);
}
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_content_res_Configuration(JNIEnv* env)
{
- jclass clazz;
- FIND_CLASS(clazz, "android/content/res/Configuration");
+ jclass clazz = FindClassOrDie(env, "android/content/res/Configuration");
- GET_FIELD_ID(gConfigurationClassInfo.mcc, clazz,
- "mcc", "I");
- GET_FIELD_ID(gConfigurationClassInfo.mnc, clazz,
- "mnc", "I");
- GET_FIELD_ID(gConfigurationClassInfo.locale, clazz,
- "locale", "Ljava/util/Locale;");
- GET_FIELD_ID(gConfigurationClassInfo.screenLayout, clazz,
- "screenLayout", "I");
- GET_FIELD_ID(gConfigurationClassInfo.touchscreen, clazz,
- "touchscreen", "I");
- GET_FIELD_ID(gConfigurationClassInfo.keyboard, clazz,
- "keyboard", "I");
- GET_FIELD_ID(gConfigurationClassInfo.keyboardHidden, clazz,
- "keyboardHidden", "I");
- GET_FIELD_ID(gConfigurationClassInfo.hardKeyboardHidden, clazz,
- "hardKeyboardHidden", "I");
- GET_FIELD_ID(gConfigurationClassInfo.navigation, clazz,
- "navigation", "I");
- GET_FIELD_ID(gConfigurationClassInfo.navigationHidden, clazz,
- "navigationHidden", "I");
- GET_FIELD_ID(gConfigurationClassInfo.orientation, clazz,
- "orientation", "I");
- GET_FIELD_ID(gConfigurationClassInfo.uiMode, clazz,
- "uiMode", "I");
- GET_FIELD_ID(gConfigurationClassInfo.screenWidthDp, clazz,
- "screenWidthDp", "I");
- GET_FIELD_ID(gConfigurationClassInfo.screenHeightDp, clazz,
- "screenHeightDp", "I");
- GET_FIELD_ID(gConfigurationClassInfo.smallestScreenWidthDp, clazz,
- "smallestScreenWidthDp", "I");
+ gConfigurationClassInfo.mcc = GetFieldIDOrDie(env, clazz, "mcc", "I");
+ gConfigurationClassInfo.mnc = GetFieldIDOrDie(env, clazz, "mnc", "I");
+ gConfigurationClassInfo.locale = GetFieldIDOrDie(env, clazz, "locale", "Ljava/util/Locale;");
+ gConfigurationClassInfo.screenLayout = GetFieldIDOrDie(env, clazz, "screenLayout", "I");
+ gConfigurationClassInfo.touchscreen = GetFieldIDOrDie(env, clazz, "touchscreen", "I");
+ gConfigurationClassInfo.keyboard = GetFieldIDOrDie(env, clazz, "keyboard", "I");
+ gConfigurationClassInfo.keyboardHidden = GetFieldIDOrDie(env, clazz, "keyboardHidden", "I");
+ gConfigurationClassInfo.hardKeyboardHidden = GetFieldIDOrDie(env, clazz, "hardKeyboardHidden",
+ "I");
+ gConfigurationClassInfo.navigation = GetFieldIDOrDie(env, clazz, "navigation", "I");
+ gConfigurationClassInfo.navigationHidden = GetFieldIDOrDie(env, clazz, "navigationHidden", "I");
+ gConfigurationClassInfo.orientation = GetFieldIDOrDie(env, clazz, "orientation", "I");
+ gConfigurationClassInfo.uiMode = GetFieldIDOrDie(env, clazz, "uiMode", "I");
+ gConfigurationClassInfo.screenWidthDp = GetFieldIDOrDie(env, clazz, "screenWidthDp", "I");
+ gConfigurationClassInfo.screenHeightDp = GetFieldIDOrDie(env, clazz, "screenHeightDp", "I");
+ gConfigurationClassInfo.smallestScreenWidthDp = GetFieldIDOrDie(env, clazz,
+ "smallestScreenWidthDp", "I");
return 0;
}
diff --git a/core/jni/android_content_res_ObbScanner.cpp b/core/jni/android_content_res_ObbScanner.cpp
index 5d51ee2..ef17db6 100644
--- a/core/jni/android_content_res_ObbScanner.cpp
+++ b/core/jni/android_content_res_ObbScanner.cpp
@@ -25,6 +25,8 @@
#include "utils/misc.h"
#include "android_runtime/AndroidRuntime.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -80,30 +82,17 @@
(void*) android_content_res_ObbScanner_getObbInfo },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_content_res_ObbScanner(JNIEnv* env)
{
- jclass clazz;
- FIND_CLASS(clazz, "android/content/res/ObbInfo");
+ jclass clazz = FindClassOrDie(env, "android/content/res/ObbInfo");
- GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz,
- "packageName", "Ljava/lang/String;");
- GET_FIELD_ID(gObbInfoClassInfo.version, clazz,
- "version", "I");
- GET_FIELD_ID(gObbInfoClassInfo.flags, clazz,
- "flags", "I");
- GET_FIELD_ID(gObbInfoClassInfo.salt, clazz,
- "salt", "[B");
+ gObbInfoClassInfo.packageName = GetFieldIDOrDie(env, clazz, "packageName",
+ "Ljava/lang/String;");
+ gObbInfoClassInfo.version = GetFieldIDOrDie(env, clazz, "version", "I");
+ gObbInfoClassInfo.flags = GetFieldIDOrDie(env, clazz, "flags", "I");
+ gObbInfoClassInfo.salt = GetFieldIDOrDie(env, clazz, "salt", "[B");
- return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods,
- NELEM(gMethods));
+ return RegisterMethodsOrDie(env, "android/content/res/ObbScanner", gMethods, NELEM(gMethods));
}
}; // namespace android
diff --git a/core/jni/android_database_CursorWindow.cpp b/core/jni/android_database_CursorWindow.cpp
index af6cc72..580ac02 100644
--- a/core/jni/android_database_CursorWindow.cpp
+++ b/core/jni/android_database_CursorWindow.cpp
@@ -36,6 +36,8 @@
#include "android_util_Binder.h"
#include "android_database_SQLiteCommon.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -522,29 +524,16 @@
(void*)nativePutNull },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
-int register_android_database_CursorWindow(JNIEnv * env)
+int register_android_database_CursorWindow(JNIEnv* env)
{
- jclass clazz;
- FIND_CLASS(clazz, "android/database/CharArrayBuffer");
+ jclass clazz = FindClassOrDie(env, "android/database/CharArrayBuffer");
- GET_FIELD_ID(gCharArrayBufferClassInfo.data, clazz,
- "data", "[C");
- GET_FIELD_ID(gCharArrayBufferClassInfo.sizeCopied, clazz,
- "sizeCopied", "I");
+ gCharArrayBufferClassInfo.data = GetFieldIDOrDie(env, clazz, "data", "[C");
+ gCharArrayBufferClassInfo.sizeCopied = GetFieldIDOrDie(env, clazz, "sizeCopied", "I");
- gEmptyString = jstring(env->NewGlobalRef(env->NewStringUTF("")));
- LOG_FATAL_IF(!gEmptyString, "Unable to create empty string");
+ gEmptyString = MakeGlobalRefOrDie(env, env->NewStringUTF(""));
- return AndroidRuntime::registerNativeMethods(env, "android/database/CursorWindow",
- sMethods, NELEM(sMethods));
+ return RegisterMethodsOrDie(env, "android/database/CursorWindow", sMethods, NELEM(sMethods));
}
} // namespace android
diff --git a/core/jni/android_database_SQLiteConnection.cpp b/core/jni/android_database_SQLiteConnection.cpp
index 4378d1c..7a3cdf6 100644
--- a/core/jni/android_database_SQLiteConnection.cpp
+++ b/core/jni/android_database_SQLiteConnection.cpp
@@ -37,6 +37,8 @@
#include "android_database_SQLiteCommon.h"
+#include "core_jni_helpers.h"
+
// Set to 1 to use UTF16 storage for localized indexes.
#define UTF16_STORAGE 0
@@ -841,35 +843,20 @@
(void*)nativeResetCancel },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_database_SQLiteConnection(JNIEnv *env)
{
- jclass clazz;
- FIND_CLASS(clazz, "android/database/sqlite/SQLiteCustomFunction");
+ jclass clazz = FindClassOrDie(env, "android/database/sqlite/SQLiteCustomFunction");
- GET_FIELD_ID(gSQLiteCustomFunctionClassInfo.name, clazz,
- "name", "Ljava/lang/String;");
- GET_FIELD_ID(gSQLiteCustomFunctionClassInfo.numArgs, clazz,
- "numArgs", "I");
- GET_METHOD_ID(gSQLiteCustomFunctionClassInfo.dispatchCallback,
- clazz, "dispatchCallback", "([Ljava/lang/String;)V");
+ gSQLiteCustomFunctionClassInfo.name = GetFieldIDOrDie(env, clazz, "name", "Ljava/lang/String;");
+ gSQLiteCustomFunctionClassInfo.numArgs = GetFieldIDOrDie(env, clazz, "numArgs", "I");
+ gSQLiteCustomFunctionClassInfo.dispatchCallback = GetMethodIDOrDie(env, clazz,
+ "dispatchCallback", "([Ljava/lang/String;)V");
- FIND_CLASS(clazz, "java/lang/String");
- gStringClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
+ clazz = FindClassOrDie(env, "java/lang/String");
+ gStringClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteConnection",
- sMethods, NELEM(sMethods));
+ return RegisterMethodsOrDie(env, "android/database/sqlite/SQLiteConnection", sMethods,
+ NELEM(sMethods));
}
} // namespace android
diff --git a/core/jni/android_database_SQLiteDebug.cpp b/core/jni/android_database_SQLiteDebug.cpp
index c1e7305..26e13cf 100644
--- a/core/jni/android_database_SQLiteDebug.cpp
+++ b/core/jni/android_database_SQLiteDebug.cpp
@@ -28,6 +28,8 @@
#include <sqlite3.h>
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -62,27 +64,17 @@
(void*) nativeGetPagerStats },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_database_SQLiteDebug(JNIEnv *env)
{
- jclass clazz;
- FIND_CLASS(clazz, "android/database/sqlite/SQLiteDebug$PagerStats");
+ jclass clazz = FindClassOrDie(env, "android/database/sqlite/SQLiteDebug$PagerStats");
- GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.memoryUsed, clazz,
- "memoryUsed", "I");
- GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, clazz,
+ gSQLiteDebugPagerStatsClassInfo.memoryUsed = GetFieldIDOrDie(env, clazz, "memoryUsed", "I");
+ gSQLiteDebugPagerStatsClassInfo.largestMemAlloc = GetFieldIDOrDie(env, clazz,
"largestMemAlloc", "I");
- GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow, clazz,
+ gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow = GetFieldIDOrDie(env, clazz,
"pageCacheOverflow", "I");
- return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteDebug",
+ return RegisterMethodsOrDie(env, "android/database/sqlite/SQLiteDebug",
gMethods, NELEM(gMethods));
}
diff --git a/core/jni/android_hardware_SensorManager.cpp b/core/jni/android_hardware_SensorManager.cpp
index ec2bd84..d1b1a1a 100644
--- a/core/jni/android_hardware_SensorManager.cpp
+++ b/core/jni/android_hardware_SensorManager.cpp
@@ -28,6 +28,8 @@
#include "android_os_MessageQueue.h"
#include <android_runtime/AndroidRuntime.h>
+#include "core_jni_helpers.h"
+
static struct {
jclass clazz;
jmethodID dispatchSensorEvent;
@@ -294,32 +296,22 @@
using namespace android;
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_hardware_SensorManager(JNIEnv *env)
{
- jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager",
+ RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager",
gSystemSensorManagerMethods, NELEM(gSystemSensorManagerMethods));
- jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager$BaseEventQueue",
+ RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager$BaseEventQueue",
gBaseEventQueueMethods, NELEM(gBaseEventQueueMethods));
- FIND_CLASS(gBaseEventQueueClassInfo.clazz, "android/hardware/SystemSensorManager$BaseEventQueue");
+ gBaseEventQueueClassInfo.clazz = FindClassOrDie(env,
+ "android/hardware/SystemSensorManager$BaseEventQueue");
- GET_METHOD_ID(gBaseEventQueueClassInfo.dispatchSensorEvent,
- gBaseEventQueueClassInfo.clazz,
- "dispatchSensorEvent", "(I[FIJ)V");
+ gBaseEventQueueClassInfo.dispatchSensorEvent = GetMethodIDOrDie(env,
+ gBaseEventQueueClassInfo.clazz, "dispatchSensorEvent", "(I[FIJ)V");
- GET_METHOD_ID(gBaseEventQueueClassInfo.dispatchFlushCompleteEvent,
- gBaseEventQueueClassInfo.clazz,
- "dispatchFlushCompleteEvent", "(I)V");
+ gBaseEventQueueClassInfo.dispatchFlushCompleteEvent = GetMethodIDOrDie(env,
+ gBaseEventQueueClassInfo.clazz, "dispatchFlushCompleteEvent", "(I)V");
return 0;
}
diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp
index 8b9f574..991b43f 100644
--- a/core/jni/android_net_NetUtils.cpp
+++ b/core/jni/android_net_NetUtils.cpp
@@ -26,6 +26,8 @@
#include <arpa/inet.h>
#include <cutils/properties.h>
+#include "core_jni_helpers.h"
+
extern "C" {
int ifc_enable(const char *ifname);
int ifc_disable(const char *ifname);
@@ -269,27 +271,26 @@
int register_android_net_NetworkUtils(JNIEnv* env)
{
- jclass dhcpResultsClass = env->FindClass("android/net/DhcpResults");
- LOG_FATAL_IF(dhcpResultsClass == NULL, "Unable to find class android/net/DhcpResults");
- dhcpResultsFieldIds.clear =
- env->GetMethodID(dhcpResultsClass, "clear", "()V");
- dhcpResultsFieldIds.setIpAddress =
- env->GetMethodID(dhcpResultsClass, "setIpAddress", "(Ljava/lang/String;I)Z");
- dhcpResultsFieldIds.setGateway =
- env->GetMethodID(dhcpResultsClass, "setGateway", "(Ljava/lang/String;)Z");
- dhcpResultsFieldIds.addDns =
- env->GetMethodID(dhcpResultsClass, "addDns", "(Ljava/lang/String;)Z");
- dhcpResultsFieldIds.setDomains =
- env->GetMethodID(dhcpResultsClass, "setDomains", "(Ljava/lang/String;)V");
- dhcpResultsFieldIds.setServerAddress =
- env->GetMethodID(dhcpResultsClass, "setServerAddress", "(Ljava/lang/String;)Z");
- dhcpResultsFieldIds.setLeaseDuration =
- env->GetMethodID(dhcpResultsClass, "setLeaseDuration", "(I)V");
- dhcpResultsFieldIds.setVendorInfo =
- env->GetMethodID(dhcpResultsClass, "setVendorInfo", "(Ljava/lang/String;)V");
+ jclass dhcpResultsClass = FindClassOrDie(env, "android/net/DhcpResults");
- return AndroidRuntime::registerNativeMethods(env,
- NETUTILS_PKG_NAME, gNetworkUtilMethods, NELEM(gNetworkUtilMethods));
+ dhcpResultsFieldIds.clear = GetMethodIDOrDie(env, dhcpResultsClass, "clear", "()V");
+ dhcpResultsFieldIds.setIpAddress =GetMethodIDOrDie(env, dhcpResultsClass, "setIpAddress",
+ "(Ljava/lang/String;I)Z");
+ dhcpResultsFieldIds.setGateway = GetMethodIDOrDie(env, dhcpResultsClass, "setGateway",
+ "(Ljava/lang/String;)Z");
+ dhcpResultsFieldIds.addDns = GetMethodIDOrDie(env, dhcpResultsClass, "addDns",
+ "(Ljava/lang/String;)Z");
+ dhcpResultsFieldIds.setDomains = GetMethodIDOrDie(env, dhcpResultsClass, "setDomains",
+ "(Ljava/lang/String;)V");
+ dhcpResultsFieldIds.setServerAddress = GetMethodIDOrDie(env, dhcpResultsClass,
+ "setServerAddress", "(Ljava/lang/String;)Z");
+ dhcpResultsFieldIds.setLeaseDuration = GetMethodIDOrDie(env, dhcpResultsClass,
+ "setLeaseDuration", "(I)V");
+ dhcpResultsFieldIds.setVendorInfo = GetMethodIDOrDie(env, dhcpResultsClass, "setVendorInfo",
+ "(Ljava/lang/String;)V");
+
+ return RegisterMethodsOrDie(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
+ NELEM(gNetworkUtilMethods));
}
}; // namespace android
diff --git a/core/jni/android_nio_utils.cpp b/core/jni/android_nio_utils.cpp
index 64597cc..ed8c603 100644
--- a/core/jni/android_nio_utils.cpp
+++ b/core/jni/android_nio_utils.cpp
@@ -16,6 +16,8 @@
#include "android_nio_utils.h"
+#include "core_jni_helpers.h"
+
struct NioJNIData {
jclass nioAccessClass;
@@ -73,32 +75,19 @@
///////////////////////////////////////////////////////////////////////////////
-static jclass findClass(JNIEnv* env, const char name[]) {
- jclass c = env->FindClass(name);
- LOG_FATAL_IF(!c, "Unable to find class %s", name);
- return c;
-}
-
-static jmethodID findStaticMethod(JNIEnv* env, jclass c, const char method[],
- const char params[]) {
- jmethodID m = env->GetStaticMethodID(c, method, params);
- LOG_FATAL_IF(!m, "Unable to find method %s", method);
- return m;
-}
-
namespace android {
int register_android_nio_utils(JNIEnv* env) {
- jclass localClass = findClass(env, "java/nio/NIOAccess");
- gNioJNI.getBasePointerID = findStaticMethod(env, localClass,
- "getBasePointer", "(Ljava/nio/Buffer;)J");
- gNioJNI.getBaseArrayID = findStaticMethod(env, localClass,
- "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
- gNioJNI.getBaseArrayOffsetID = findStaticMethod(env, localClass,
- "getBaseArrayOffset", "(Ljava/nio/Buffer;)I");
+ jclass localClass = FindClassOrDie(env, "java/nio/NIOAccess");
+ gNioJNI.getBasePointerID = GetStaticMethodIDOrDie(env, localClass, "getBasePointer",
+ "(Ljava/nio/Buffer;)J");
+ gNioJNI.getBaseArrayID = GetStaticMethodIDOrDie(env, localClass, "getBaseArray",
+ "(Ljava/nio/Buffer;)Ljava/lang/Object;");
+ gNioJNI.getBaseArrayOffsetID = GetStaticMethodIDOrDie(env, localClass, "getBaseArrayOffset",
+ "(Ljava/nio/Buffer;)I");
// now record a permanent version of the class ID
- gNioJNI.nioAccessClass = (jclass) env->NewGlobalRef(localClass);
+ gNioJNI.nioAccessClass = MakeGlobalRefOrDie(env, localClass);
return 0;
}
diff --git a/core/jni/android_os_MessageQueue.cpp b/core/jni/android_os_MessageQueue.cpp
index 96a3e37..5d7877b 100644
--- a/core/jni/android_os_MessageQueue.cpp
+++ b/core/jni/android_os_MessageQueue.cpp
@@ -23,6 +23,8 @@
#include <utils/Log.h>
#include "android_os_MessageQueue.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -157,27 +159,15 @@
{ "nativeIsIdling", "(J)Z", (void*)android_os_MessageQueue_nativeIsIdling }
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_os_MessageQueue(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/os/MessageQueue",
- gMessageQueueMethods, NELEM(gMessageQueueMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/os/MessageQueue", gMessageQueueMethods,
+ NELEM(gMessageQueueMethods));
- jclass clazz;
- FIND_CLASS(clazz, "android/os/MessageQueue");
+ jclass clazz = FindClassOrDie(env, "android/os/MessageQueue");
- GET_FIELD_ID(gMessageQueueClassInfo.mPtr, clazz,
- "mPtr", "J");
-
- return 0;
+ gMessageQueueClassInfo.mPtr = GetFieldIDOrDie(env, clazz, "mPtr", "J");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/android_os_Parcel.cpp b/core/jni/android_os_Parcel.cpp
index 884cbf1..896faa6 100644
--- a/core/jni/android_os_Parcel.cpp
+++ b/core/jni/android_os_Parcel.cpp
@@ -46,6 +46,8 @@
#include <android_runtime/AndroidRuntime.h>
+#include "core_jni_helpers.h"
+
//#undef ALOGV
//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
@@ -741,20 +743,14 @@
int register_android_os_Parcel(JNIEnv* env)
{
- jclass clazz;
+ jclass clazz = FindClassOrDie(env, kParcelPathName);
- clazz = env->FindClass(kParcelPathName);
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.Parcel");
+ gParcelOffsets.clazz = MakeGlobalRefOrDie(env, clazz);
+ gParcelOffsets.mNativePtr = GetFieldIDOrDie(env, clazz, "mNativePtr", "J");
+ gParcelOffsets.obtain = GetStaticMethodIDOrDie(env, clazz, "obtain", "()Landroid/os/Parcel;");
+ gParcelOffsets.recycle = GetMethodIDOrDie(env, clazz, "recycle", "()V");
- gParcelOffsets.clazz = (jclass) env->NewGlobalRef(clazz);
- gParcelOffsets.mNativePtr = env->GetFieldID(clazz, "mNativePtr", "J");
- gParcelOffsets.obtain = env->GetStaticMethodID(clazz, "obtain",
- "()Landroid/os/Parcel;");
- gParcelOffsets.recycle = env->GetMethodID(clazz, "recycle", "()V");
-
- return AndroidRuntime::registerNativeMethods(
- env, kParcelPathName,
- gParcelMethods, NELEM(gParcelMethods));
+ return RegisterMethodsOrDie(env, kParcelPathName, gParcelMethods, NELEM(gParcelMethods));
}
};
diff --git a/core/jni/android_os_Trace.cpp b/core/jni/android_os_Trace.cpp
index 37d51cc..7735464 100644
--- a/core/jni/android_os_Trace.cpp
+++ b/core/jni/android_os_Trace.cpp
@@ -134,8 +134,7 @@
int register_android_os_Trace(JNIEnv* env) {
int res = jniRegisterNativeMethods(env, "android/os/Trace",
gTraceMethods, NELEM(gTraceMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
return 0;
}
diff --git a/core/jni/android_server_FingerprintManager.cpp b/core/jni/android_server_FingerprintManager.cpp
index cb17481..c8aa22f7 100644
--- a/core/jni/android_server_FingerprintManager.cpp
+++ b/core/jni/android_server_FingerprintManager.cpp
@@ -24,22 +24,7 @@
#include <hardware/fingerprint.h>
#include <utils/Log.h>
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_STATIC_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetStaticMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find static method" methodName);
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
+#include "core_jni_helpers.h"
namespace android {
@@ -104,10 +89,12 @@
static void nativeInit(JNIEnv *env, jobject clazz, jobject callbackObj) {
ALOG(LOG_VERBOSE, LOG_TAG, "nativeInit()\n");
- FIND_CLASS(gFingerprintServiceClassInfo.clazz, FINGERPRINT_SERVICE);
- GET_METHOD_ID(gFingerprintServiceClassInfo.notify, gFingerprintServiceClassInfo.clazz,
+ gFingerprintServiceClassInfo.clazz = FindClassOrDie(env, FINGERPRINT_SERVICE);
+ gFingerprintServiceClassInfo.clazz = MakeGlobalRefOrDie(env,
+ gFingerprintServiceClassInfo.clazz);
+ gFingerprintServiceClassInfo.notify = GetMethodIDOrDie(env, gFingerprintServiceClassInfo.clazz,
"notify", "(III)V");
- gFingerprintServiceClassInfo.callbackObject = env->NewGlobalRef(callbackObj);
+ gFingerprintServiceClassInfo.callbackObject = MakeGlobalRefOrDie(env, callbackObj);
}
static jint nativeEnroll(JNIEnv* env, jobject clazz, jint timeout) {
@@ -193,11 +180,11 @@
};
int register_android_server_fingerprint_FingerprintService(JNIEnv* env) {
- FIND_CLASS(gFingerprintServiceClassInfo.clazz, FINGERPRINT_SERVICE);
- GET_METHOD_ID(gFingerprintServiceClassInfo.notify, gFingerprintServiceClassInfo.clazz, "notify",
- "(III)V");
- int result = AndroidRuntime::registerNativeMethods(
- env, FINGERPRINT_SERVICE, g_methods, NELEM(g_methods));
+ jclass clazz = FindClassOrDie(env, FINGERPRINT_SERVICE);
+ gFingerprintServiceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
+ gFingerprintServiceClassInfo.notify = GetMethodIDOrDie(env, gFingerprintServiceClassInfo.clazz,
+ "notify", "(III)V");
+ int result = RegisterMethodsOrDie(env, FINGERPRINT_SERVICE, g_methods, NELEM(g_methods));
ALOG(LOG_VERBOSE, LOG_TAG, "FingerprintManager JNI ready.\n");
return result;
}
diff --git a/core/jni/android_util_AssetManager.cpp b/core/jni/android_util_AssetManager.cpp
index b1c0738..5fb2e42 100644
--- a/core/jni/android_util_AssetManager.cpp
+++ b/core/jni/android_util_AssetManager.cpp
@@ -34,6 +34,8 @@
#include <private/android_filesystem_config.h> // for AID_SYSTEM
+#include "core_jni_helpers.h"
+
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -2115,64 +2117,38 @@
int register_android_content_AssetManager(JNIEnv* env)
{
- jclass typedValue = env->FindClass("android/util/TypedValue");
- LOG_FATAL_IF(typedValue == NULL, "Unable to find class android/util/TypedValue");
- gTypedValueOffsets.mType
- = env->GetFieldID(typedValue, "type", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mType == NULL, "Unable to find TypedValue.type");
- gTypedValueOffsets.mData
- = env->GetFieldID(typedValue, "data", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mData == NULL, "Unable to find TypedValue.data");
- gTypedValueOffsets.mString
- = env->GetFieldID(typedValue, "string", "Ljava/lang/CharSequence;");
- LOG_FATAL_IF(gTypedValueOffsets.mString == NULL, "Unable to find TypedValue.string");
- gTypedValueOffsets.mAssetCookie
- = env->GetFieldID(typedValue, "assetCookie", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mAssetCookie == NULL, "Unable to find TypedValue.assetCookie");
- gTypedValueOffsets.mResourceId
- = env->GetFieldID(typedValue, "resourceId", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mResourceId == NULL, "Unable to find TypedValue.resourceId");
- gTypedValueOffsets.mChangingConfigurations
- = env->GetFieldID(typedValue, "changingConfigurations", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mChangingConfigurations == NULL, "Unable to find TypedValue.changingConfigurations");
- gTypedValueOffsets.mDensity = env->GetFieldID(typedValue, "density", "I");
- LOG_FATAL_IF(gTypedValueOffsets.mDensity == NULL, "Unable to find TypedValue.density");
+ jclass typedValue = FindClassOrDie(env, "android/util/TypedValue");
+ gTypedValueOffsets.mType = GetFieldIDOrDie(env, typedValue, "type", "I");
+ gTypedValueOffsets.mData = GetFieldIDOrDie(env, typedValue, "data", "I");
+ gTypedValueOffsets.mString = GetFieldIDOrDie(env, typedValue, "string",
+ "Ljava/lang/CharSequence;");
+ gTypedValueOffsets.mAssetCookie = GetFieldIDOrDie(env, typedValue, "assetCookie", "I");
+ gTypedValueOffsets.mResourceId = GetFieldIDOrDie(env, typedValue, "resourceId", "I");
+ gTypedValueOffsets.mChangingConfigurations = GetFieldIDOrDie(env, typedValue,
+ "changingConfigurations", "I");
+ gTypedValueOffsets.mDensity = GetFieldIDOrDie(env, typedValue, "density", "I");
- jclass assetFd = env->FindClass("android/content/res/AssetFileDescriptor");
- LOG_FATAL_IF(assetFd == NULL, "Unable to find class android/content/res/AssetFileDescriptor");
- gAssetFileDescriptorOffsets.mFd
- = env->GetFieldID(assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;");
- LOG_FATAL_IF(gAssetFileDescriptorOffsets.mFd == NULL, "Unable to find AssetFileDescriptor.mFd");
- gAssetFileDescriptorOffsets.mStartOffset
- = env->GetFieldID(assetFd, "mStartOffset", "J");
- LOG_FATAL_IF(gAssetFileDescriptorOffsets.mStartOffset == NULL, "Unable to find AssetFileDescriptor.mStartOffset");
- gAssetFileDescriptorOffsets.mLength
- = env->GetFieldID(assetFd, "mLength", "J");
- LOG_FATAL_IF(gAssetFileDescriptorOffsets.mLength == NULL, "Unable to find AssetFileDescriptor.mLength");
+ jclass assetFd = FindClassOrDie(env, "android/content/res/AssetFileDescriptor");
+ gAssetFileDescriptorOffsets.mFd = GetFieldIDOrDie(env, assetFd, "mFd",
+ "Landroid/os/ParcelFileDescriptor;");
+ gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset", "J");
+ gAssetFileDescriptorOffsets.mLength = GetFieldIDOrDie(env, assetFd, "mLength", "J");
- jclass assetManager = env->FindClass("android/content/res/AssetManager");
- LOG_FATAL_IF(assetManager == NULL, "Unable to find class android/content/res/AssetManager");
- gAssetManagerOffsets.mObject
- = env->GetFieldID(assetManager, "mObject", "J");
- LOG_FATAL_IF(gAssetManagerOffsets.mObject == NULL, "Unable to find AssetManager.mObject");
+ jclass assetManager = FindClassOrDie(env, "android/content/res/AssetManager");
+ gAssetManagerOffsets.mObject = GetFieldIDOrDie(env, assetManager, "mObject", "J");
- jclass stringClass = env->FindClass("java/lang/String");
- LOG_FATAL_IF(stringClass == NULL, "Unable to find class java/lang/String");
- g_stringClass = (jclass)env->NewGlobalRef(stringClass);
- LOG_FATAL_IF(g_stringClass == NULL, "Unable to create global reference for class java/lang/String");
+ jclass stringClass = FindClassOrDie(env, "java/lang/String");
+ g_stringClass = MakeGlobalRefOrDie(env, stringClass);
- jclass sparseArrayClass = env->FindClass("android/util/SparseArray");
- LOG_FATAL_IF(sparseArrayClass == NULL, "Unable to find class android/util/SparseArray");
- gSparseArrayOffsets.classObject = (jclass) env->NewGlobalRef(sparseArrayClass);
- gSparseArrayOffsets.constructor =
- env->GetMethodID(gSparseArrayOffsets.classObject, "<init>", "()V");
- LOG_FATAL_IF(gSparseArrayOffsets.constructor == NULL, "Unable to find SparseArray.<init>()");
- gSparseArrayOffsets.put =
- env->GetMethodID(gSparseArrayOffsets.classObject, "put", "(ILjava/lang/Object;)V");
- LOG_FATAL_IF(gSparseArrayOffsets.put == NULL, "Unable to find SparseArray.put(int, V)");
+ jclass sparseArrayClass = FindClassOrDie(env, "android/util/SparseArray");
+ gSparseArrayOffsets.classObject = MakeGlobalRefOrDie(env, sparseArrayClass);
+ gSparseArrayOffsets.constructor = GetMethodIDOrDie(env, gSparseArrayOffsets.classObject,
+ "<init>", "()V");
+ gSparseArrayOffsets.put = GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "put",
+ "(ILjava/lang/Object;)V");
- return AndroidRuntime::registerNativeMethods(env,
- "android/content/res/AssetManager", gAssetManagerMethods, NELEM(gAssetManagerMethods));
+ return RegisterMethodsOrDie(env, "android/content/res/AssetManager", gAssetManagerMethods,
+ NELEM(gAssetManagerMethods));
}
}; // namespace android
diff --git a/core/jni/android_util_Binder.cpp b/core/jni/android_util_Binder.cpp
index 1dd6378..10a5b5a 100644
--- a/core/jni/android_util_Binder.cpp
+++ b/core/jni/android_util_Binder.cpp
@@ -48,6 +48,8 @@
#include <android_runtime/AndroidRuntime.h>
+#include "core_jni_helpers.h"
+
//#undef ALOGV
//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
@@ -827,19 +829,11 @@
static int int_register_android_os_Binder(JNIEnv* env)
{
- jclass clazz;
+ jclass clazz = FindClassOrDie(env, kBinderPathName);
- clazz = env->FindClass(kBinderPathName);
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.Binder");
-
- gBinderOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gBinderOffsets.mExecTransact
- = env->GetMethodID(clazz, "execTransact", "(IJJI)Z");
- assert(gBinderOffsets.mExecTransact);
-
- gBinderOffsets.mObject
- = env->GetFieldID(clazz, "mObject", "J");
- assert(gBinderOffsets.mObject);
+ gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z");
+ gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
return AndroidRuntime::registerNativeMethods(
env, kBinderPathName,
@@ -911,15 +905,10 @@
static int int_register_android_os_BinderInternal(JNIEnv* env)
{
- jclass clazz;
+ jclass clazz = FindClassOrDie(env, kBinderInternalPathName);
- clazz = env->FindClass(kBinderInternalPathName);
- LOG_FATAL_IF(clazz == NULL, "Unable to find class com.android.internal.os.BinderInternal");
-
- gBinderInternalOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gBinderInternalOffsets.mForceGc
- = env->GetStaticMethodID(clazz, "forceBinderGc", "()V");
- assert(gBinderInternalOffsets.mForceGc);
+ gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V");
return AndroidRuntime::registerNativeMethods(
env, kBinderInternalPathName,
@@ -1234,37 +1223,22 @@
static int int_register_android_os_BinderProxy(JNIEnv* env)
{
- jclass clazz;
+ jclass clazz = FindClassOrDie(env, "java/lang/Error");
+ gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
- clazz = env->FindClass("java/lang/Error");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class java.lang.Error");
- gErrorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
+ clazz = FindClassOrDie(env, kBinderProxyPathName);
+ gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gBinderProxyOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
+ gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice",
+ "(Landroid/os/IBinder$DeathRecipient;)V");
- clazz = env->FindClass(kBinderProxyPathName);
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.BinderProxy");
+ gBinderProxyOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
+ gBinderProxyOffsets.mSelf = GetFieldIDOrDie(env, clazz, "mSelf",
+ "Ljava/lang/ref/WeakReference;");
+ gBinderProxyOffsets.mOrgue = GetFieldIDOrDie(env, clazz, "mOrgue", "J");
- gBinderProxyOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gBinderProxyOffsets.mConstructor
- = env->GetMethodID(clazz, "<init>", "()V");
- assert(gBinderProxyOffsets.mConstructor);
- gBinderProxyOffsets.mSendDeathNotice
- = env->GetStaticMethodID(clazz, "sendDeathNotice", "(Landroid/os/IBinder$DeathRecipient;)V");
- assert(gBinderProxyOffsets.mSendDeathNotice);
-
- gBinderProxyOffsets.mObject
- = env->GetFieldID(clazz, "mObject", "J");
- assert(gBinderProxyOffsets.mObject);
- gBinderProxyOffsets.mSelf
- = env->GetFieldID(clazz, "mSelf", "Ljava/lang/ref/WeakReference;");
- assert(gBinderProxyOffsets.mSelf);
- gBinderProxyOffsets.mOrgue
- = env->GetFieldID(clazz, "mOrgue", "J");
- assert(gBinderProxyOffsets.mOrgue);
-
- clazz = env->FindClass("java/lang/Class");
- LOG_FATAL_IF(clazz == NULL, "Unable to find java.lang.Class");
- gClassOffsets.mGetName = env->GetMethodID(clazz, "getName", "()Ljava/lang/String;");
- assert(gClassOffsets.mGetName);
+ clazz = FindClassOrDie(env, "java/lang/Class");
+ gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;");
return AndroidRuntime::registerNativeMethods(
env, kBinderProxyPathName,
@@ -1284,28 +1258,20 @@
if (int_register_android_os_BinderProxy(env) < 0)
return -1;
- jclass clazz;
+ jclass clazz = FindClassOrDie(env, "android/util/Log");
+ gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e",
+ "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I");
- clazz = env->FindClass("android/util/Log");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.util.Log");
- gLogOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gLogOffsets.mLogE = env->GetStaticMethodID(
- clazz, "e", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I");
- assert(gLogOffsets.mLogE);
+ clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor");
+ gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>",
+ "(Ljava/io/FileDescriptor;)V");
- clazz = env->FindClass("android/os/ParcelFileDescriptor");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
- gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gParcelFileDescriptorOffsets.mConstructor
- = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
-
- clazz = env->FindClass("android/os/StrictMode");
- LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.StrictMode");
- gStrictModeCallbackOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
- gStrictModeCallbackOffsets.mCallback = env->GetStaticMethodID(
- clazz, "onBinderStrictModePolicyChange", "(I)V");
- LOG_FATAL_IF(gStrictModeCallbackOffsets.mCallback == NULL,
- "Unable to find strict mode callback.");
+ clazz = FindClassOrDie(env, "android/os/StrictMode");
+ gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
+ gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz,
+ "onBinderStrictModePolicyChange", "(I)V");
return 0;
}
diff --git a/core/jni/android_view_DisplayEventReceiver.cpp b/core/jni/android_view_DisplayEventReceiver.cpp
index 1ccf419..b9fbccb 100644
--- a/core/jni/android_view_DisplayEventReceiver.cpp
+++ b/core/jni/android_view_DisplayEventReceiver.cpp
@@ -28,6 +28,8 @@
#include <gui/DisplayEventReceiver.h>
#include "android_os_MessageQueue.h"
+#include "core_jni_helpers.h"
+
namespace android {
// Number of events to read at a time from the DisplayEventReceiver pipe.
@@ -260,30 +262,19 @@
(void*)nativeScheduleVsync }
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_view_DisplayEventReceiver(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/DisplayEventReceiver",
- gMethods, NELEM(gMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/view/DisplayEventReceiver", gMethods,
+ NELEM(gMethods));
- FIND_CLASS(gDisplayEventReceiverClassInfo.clazz, "android/view/DisplayEventReceiver");
+ jclass clazz = FindClassOrDie(env, "android/view/DisplayEventReceiver");
+ gDisplayEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_METHOD_ID(gDisplayEventReceiverClassInfo.dispatchVsync,
- gDisplayEventReceiverClassInfo.clazz,
- "dispatchVsync", "(JII)V");
- GET_METHOD_ID(gDisplayEventReceiverClassInfo.dispatchHotplug,
- gDisplayEventReceiverClassInfo.clazz,
- "dispatchHotplug", "(JIZ)V");
- return 0;
+ gDisplayEventReceiverClassInfo.dispatchVsync = GetMethodIDOrDie(env,
+ gDisplayEventReceiverClassInfo.clazz, "dispatchVsync", "(JII)V");
+ gDisplayEventReceiverClassInfo.dispatchHotplug = GetMethodIDOrDie(env,
+ gDisplayEventReceiverClassInfo.clazz, "dispatchHotplug", "(JIZ)V");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index 2e2b23f..759fceb 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -46,6 +46,8 @@
#include "MinikinUtils.h"
+#include "core_jni_helpers.h"
+
namespace android {
using namespace uirenderer;
@@ -971,32 +973,19 @@
(void*) android_app_ActivityThread_dumpGraphics }
};
-
+int register_android_view_GLES20Canvas(JNIEnv* env) {
#ifdef USE_OPENGL_RENDERER
- #define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
- #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-#else
- #define FIND_CLASS(var, className)
- #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor)
+ jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
+ gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
#endif
-int register_android_view_GLES20Canvas(JNIEnv* env) {
- jclass clazz;
- FIND_CLASS(clazz, "android/graphics/Rect");
- GET_METHOD_ID(gRectClassInfo.set, clazz, "set", "(IIII)V");
-
- return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+ return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
const char* const kActivityThreadPathName = "android/app/ActivityThread";
int register_android_app_ActivityThread(JNIEnv* env) {
- return AndroidRuntime::registerNativeMethods(env, kActivityThreadPathName,
+ return RegisterMethodsOrDie(env, kActivityThreadPathName,
gActivityThreadMethods, NELEM(gActivityThreadMethods));
}
diff --git a/core/jni/android_view_GraphicBuffer.cpp b/core/jni/android_view_GraphicBuffer.cpp
index 5ebed9c..7f1af49 100644
--- a/core/jni/android_view_GraphicBuffer.cpp
+++ b/core/jni/android_view_GraphicBuffer.cpp
@@ -38,6 +38,8 @@
#include <private/gui/ComposerService.h>
+#include "core_jni_helpers.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -277,18 +279,6 @@
// JNI Glue
// ----------------------------------------------------------------------------
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(!var, "Unable to find method " methodName);
-
const char* const kClassPathName = "android/view/GraphicBuffer";
static JNINativeMethod gMethods[] = {
@@ -307,22 +297,21 @@
};
int register_android_view_GraphicBuffer(JNIEnv* env) {
- jclass clazz;
- FIND_CLASS(clazz, "android/view/GraphicBuffer");
- GET_FIELD_ID(gGraphicBufferClassInfo.mNativeObject, clazz, "mNativeObject", "J");
+ jclass clazz = FindClassOrDie(env, "android/view/GraphicBuffer");
+ gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, clazz, "mNativeObject", "J");
- FIND_CLASS(clazz, "android/graphics/Rect");
- GET_METHOD_ID(gRectClassInfo.set, clazz, "set", "(IIII)V");
- GET_FIELD_ID(gRectClassInfo.left, clazz, "left", "I");
- GET_FIELD_ID(gRectClassInfo.top, clazz, "top", "I");
- GET_FIELD_ID(gRectClassInfo.right, clazz, "right", "I");
- GET_FIELD_ID(gRectClassInfo.bottom, clazz, "bottom", "I");
+ clazz = FindClassOrDie(env, "android/graphics/Rect");
+ gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
+ gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
+ gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
+ gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
+ gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
- FIND_CLASS(clazz, "android/graphics/Canvas");
- GET_FIELD_ID(gCanvasClassInfo.mSurfaceFormat, clazz, "mSurfaceFormat", "I");
- GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V");
+ clazz = FindClassOrDie(env, "android/graphics/Canvas");
+ gCanvasClassInfo.mSurfaceFormat = GetFieldIDOrDie(env, clazz, "mSurfaceFormat", "I");
+ gCanvasClassInfo.setNativeBitmap = GetMethodIDOrDie(env, clazz, "setNativeBitmap", "(J)V");
- return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+ return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
};
diff --git a/core/jni/android_view_InputChannel.cpp b/core/jni/android_view_InputChannel.cpp
index b64b7c60..4b42ab5 100644
--- a/core/jni/android_view_InputChannel.cpp
+++ b/core/jni/android_view_InputChannel.cpp
@@ -26,6 +26,8 @@
#include "android_os_Parcel.h"
#include "android_util_Binder.h"
+#include "core_jni_helpers.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -275,34 +277,19 @@
(void*)android_view_InputChannel_nativeDup },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_InputChannel(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/InputChannel",
- gInputChannelMethods, NELEM(gInputChannelMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/view/InputChannel", gInputChannelMethods,
+ NELEM(gInputChannelMethods));
- FIND_CLASS(gInputChannelClassInfo.clazz, "android/view/InputChannel");
+ jclass clazz = FindClassOrDie(env, "android/view/InputChannel");
+ gInputChannelClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_FIELD_ID(gInputChannelClassInfo.mPtr, gInputChannelClassInfo.clazz,
- "mPtr", "J");
-
- GET_METHOD_ID(gInputChannelClassInfo.ctor, gInputChannelClassInfo.clazz,
- "<init>", "()V");
+ gInputChannelClassInfo.mPtr = GetFieldIDOrDie(env, gInputChannelClassInfo.clazz, "mPtr", "J");
- return 0;
+ gInputChannelClassInfo.ctor = GetMethodIDOrDie(env, gInputChannelClassInfo.clazz, "<init>",
+ "()V");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/android_view_InputDevice.cpp b/core/jni/android_view_InputDevice.cpp
index bef0f84..2323f43 100644
--- a/core/jni/android_view_InputDevice.cpp
+++ b/core/jni/android_view_InputDevice.cpp
@@ -25,6 +25,8 @@
#include "android_view_InputDevice.h"
#include "android_view_KeyCharacterMap.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -77,24 +79,15 @@
}
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_view_InputDevice(JNIEnv* env)
{
- FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
- gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
+ gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
+ gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
- GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
- "<init>",
+ gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
"(IIILjava/lang/String;IILjava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZ)V");
- GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
+ gInputDeviceClassInfo.addMotionRange = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz,
"addMotionRange", "(IIFFFFF)V");
return 0;
diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp
index 093dde8..76d626c 100644
--- a/core/jni/android_view_InputEventReceiver.cpp
+++ b/core/jni/android_view_InputEventReceiver.cpp
@@ -37,6 +37,8 @@
#include <ScopedLocalRef.h>
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -408,30 +410,20 @@
(void*)nativeConsumeBatchedInputEvents },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_view_InputEventReceiver(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/InputEventReceiver",
+ int res = RegisterMethodsOrDie(env, "android/view/InputEventReceiver",
gMethods, NELEM(gMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
- FIND_CLASS(gInputEventReceiverClassInfo.clazz, "android/view/InputEventReceiver");
+ jclass clazz = FindClassOrDie(env, "android/view/InputEventReceiver");
+ gInputEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_METHOD_ID(gInputEventReceiverClassInfo.dispatchInputEvent,
+ gInputEventReceiverClassInfo.dispatchInputEvent = GetMethodIDOrDie(env,
gInputEventReceiverClassInfo.clazz,
"dispatchInputEvent", "(ILandroid/view/InputEvent;)V");
- GET_METHOD_ID(gInputEventReceiverClassInfo.dispatchBatchedInputEventPending,
- gInputEventReceiverClassInfo.clazz,
- "dispatchBatchedInputEventPending", "()V");
- return 0;
+ gInputEventReceiverClassInfo.dispatchBatchedInputEventPending = GetMethodIDOrDie(env,
+ gInputEventReceiverClassInfo.clazz, "dispatchBatchedInputEventPending", "()V");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp
index e697531..de65d0d 100644
--- a/core/jni/android_view_InputEventSender.cpp
+++ b/core/jni/android_view_InputEventSender.cpp
@@ -37,6 +37,8 @@
#include <ScopedLocalRef.h>
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -299,27 +301,16 @@
(void*)nativeSendMotionEvent },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_view_InputEventSender(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/InputEventSender",
- gMethods, NELEM(gMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/view/InputEventSender", gMethods, NELEM(gMethods));
- FIND_CLASS(gInputEventSenderClassInfo.clazz, "android/view/InputEventSender");
+ jclass clazz = FindClassOrDie(env, "android/view/InputEventSender");
+ gInputEventSenderClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_METHOD_ID(gInputEventSenderClassInfo.dispatchInputEventFinished,
- gInputEventSenderClassInfo.clazz,
- "dispatchInputEventFinished", "(IZ)V");
- return 0;
+ gInputEventSenderClassInfo.dispatchInputEventFinished = GetMethodIDOrDie(
+ env, gInputEventSenderClassInfo.clazz, "dispatchInputEventFinished", "(IZ)V");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/android_view_InputQueue.cpp b/core/jni/android_view_InputQueue.cpp
index 21b73b1..96ccdee 100644
--- a/core/jni/android_view_InputQueue.cpp
+++ b/core/jni/android_view_InputQueue.cpp
@@ -33,6 +33,8 @@
#include "android_view_KeyEvent.h"
#include "android_view_MotionEvent.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -256,27 +258,13 @@
static const char* const kInputQueuePathName = "android/view/InputQueue";
-#define FIND_CLASS(var, className) \
- do { \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class %s", className); \
- } while(0)
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- do { \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName); \
- } while(0)
-
int register_android_view_InputQueue(JNIEnv* env)
{
- jclass clazz;
- FIND_CLASS(clazz, kInputQueuePathName);
- GET_METHOD_ID(gInputQueueClassInfo.finishInputEvent, clazz, "finishInputEvent", "(JZ)V");
+ jclass clazz = FindClassOrDie(env, kInputQueuePathName);
+ gInputQueueClassInfo.finishInputEvent = GetMethodIDOrDie(env, clazz, "finishInputEvent",
+ "(JZ)V");
- return AndroidRuntime::registerNativeMethods(
- env, kInputQueuePathName,
- g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, kInputQueuePathName, g_methods, NELEM(g_methods));
}
} // namespace android
diff --git a/core/jni/android_view_KeyCharacterMap.cpp b/core/jni/android_view_KeyCharacterMap.cpp
index 62d5129..4eed5a7 100644
--- a/core/jni/android_view_KeyCharacterMap.cpp
+++ b/core/jni/android_view_KeyCharacterMap.cpp
@@ -26,6 +26,8 @@
#include "android_os_Parcel.h"
#include "android_view_KeyEvent.h"
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -221,40 +223,23 @@
(void*)nativeGetEvents },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_KeyCharacterMap(JNIEnv* env)
{
- FIND_CLASS(gKeyCharacterMapClassInfo.clazz, "android/view/KeyCharacterMap");
- gKeyCharacterMapClassInfo.clazz = jclass(env->NewGlobalRef(gKeyCharacterMapClassInfo.clazz));
+ gKeyCharacterMapClassInfo.clazz = FindClassOrDie(env, "android/view/KeyCharacterMap");
+ gKeyCharacterMapClassInfo.clazz = MakeGlobalRefOrDie(env, gKeyCharacterMapClassInfo.clazz);
- GET_METHOD_ID(gKeyCharacterMapClassInfo.ctor, gKeyCharacterMapClassInfo.clazz,
+ gKeyCharacterMapClassInfo.ctor = GetMethodIDOrDie(env, gKeyCharacterMapClassInfo.clazz,
"<init>", "(J)V");
- FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
- gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
+ gKeyEventClassInfo.clazz = FindClassOrDie(env, "android/view/KeyEvent");
+ gKeyEventClassInfo.clazz = MakeGlobalRefOrDie(env, gKeyEventClassInfo.clazz);
- jclass clazz;
- FIND_CLASS(clazz, "android/view/KeyCharacterMap$FallbackAction");
+ jclass clazz = FindClassOrDie(env, "android/view/KeyCharacterMap$FallbackAction");
- GET_FIELD_ID(gFallbackActionClassInfo.keyCode, clazz,
- "keyCode", "I");
+ gFallbackActionClassInfo.keyCode = GetFieldIDOrDie(env, clazz, "keyCode", "I");
+ gFallbackActionClassInfo.metaState = GetFieldIDOrDie(env, clazz, "metaState", "I");
- GET_FIELD_ID(gFallbackActionClassInfo.metaState, clazz,
- "metaState", "I");
-
- return AndroidRuntime::registerNativeMethods(env,
- "android/view/KeyCharacterMap", g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, "android/view/KeyCharacterMap", g_methods, NELEM(g_methods));
}
}; // namespace android
diff --git a/core/jni/android_view_KeyEvent.cpp b/core/jni/android_view_KeyEvent.cpp
index 7ae21a7..216e6f6 100644
--- a/core/jni/android_view_KeyEvent.cpp
+++ b/core/jni/android_view_KeyEvent.cpp
@@ -25,6 +25,8 @@
#include <ScopedUtfChars.h>
#include "android_view_KeyEvent.h"
+#include "core_jni_helpers.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -124,56 +126,32 @@
(void*)android_view_KeyEvent_nativeKeyCodeFromString},
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_STATIC_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetStaticMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find static method" methodName);
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_KeyEvent(JNIEnv* env) {
- FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
+ jclass clazz = FindClassOrDie(env, "android/view/KeyEvent");
+ gKeyEventClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_STATIC_METHOD_ID(gKeyEventClassInfo.obtain, gKeyEventClassInfo.clazz,
+ gKeyEventClassInfo.obtain = GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz,
"obtain", "(JJIIIIIIIILjava/lang/String;)Landroid/view/KeyEvent;");
- GET_METHOD_ID(gKeyEventClassInfo.recycle, gKeyEventClassInfo.clazz,
+ gKeyEventClassInfo.recycle = GetMethodIDOrDie(env, gKeyEventClassInfo.clazz,
"recycle", "()V");
- GET_FIELD_ID(gKeyEventClassInfo.mDeviceId, gKeyEventClassInfo.clazz,
- "mDeviceId", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mSource, gKeyEventClassInfo.clazz,
- "mSource", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mMetaState, gKeyEventClassInfo.clazz,
- "mMetaState", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mAction, gKeyEventClassInfo.clazz,
- "mAction", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mKeyCode, gKeyEventClassInfo.clazz,
- "mKeyCode", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mScanCode, gKeyEventClassInfo.clazz,
- "mScanCode", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mRepeatCount, gKeyEventClassInfo.clazz,
- "mRepeatCount", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mFlags, gKeyEventClassInfo.clazz,
- "mFlags", "I");
- GET_FIELD_ID(gKeyEventClassInfo.mDownTime, gKeyEventClassInfo.clazz,
- "mDownTime", "J");
- GET_FIELD_ID(gKeyEventClassInfo.mEventTime, gKeyEventClassInfo.clazz,
- "mEventTime", "J");
- GET_FIELD_ID(gKeyEventClassInfo.mCharacters, gKeyEventClassInfo.clazz,
- "mCharacters", "Ljava/lang/String;");
+ gKeyEventClassInfo.mDeviceId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDeviceId", "I");
+ gKeyEventClassInfo.mSource = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mSource", "I");
+ gKeyEventClassInfo.mMetaState = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mMetaState",
+ "I");
+ gKeyEventClassInfo.mAction = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mAction", "I");
+ gKeyEventClassInfo.mKeyCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mKeyCode", "I");
+ gKeyEventClassInfo.mScanCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mScanCode", "I");
+ gKeyEventClassInfo.mRepeatCount = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mRepeatCount",
+ "I");
+ gKeyEventClassInfo.mFlags = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mFlags", "I");
+ gKeyEventClassInfo.mDownTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDownTime", "J");
+ gKeyEventClassInfo.mEventTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mEventTime",
+ "J");
+ gKeyEventClassInfo.mCharacters = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mCharacters",
+ "Ljava/lang/String;");
- return AndroidRuntime::registerNativeMethods(
- env, "android/view/KeyEvent", g_methods, NELEM(g_methods));
+ return RegisterMethodsOrDie(env, "android/view/KeyEvent", g_methods, NELEM(g_methods));
}
} // namespace android
diff --git a/core/jni/android_view_MotionEvent.cpp b/core/jni/android_view_MotionEvent.cpp
index 318acbb..e622768 100644
--- a/core/jni/android_view_MotionEvent.cpp
+++ b/core/jni/android_view_MotionEvent.cpp
@@ -29,6 +29,8 @@
#include "android_util_Binder.h"
#include "android/graphics/Matrix.h"
+#include "core_jni_helpers.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -852,72 +854,41 @@
(void*)android_view_MotionEvent_nativeAxisFromString },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_STATIC_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetStaticMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find static method" methodName);
-
-#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
- var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method" methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_MotionEvent(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/MotionEvent",
- gMotionEventMethods, NELEM(gMotionEventMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/view/MotionEvent", gMotionEventMethods,
+ NELEM(gMotionEventMethods));
- FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
- gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
+ gMotionEventClassInfo.clazz = FindClassOrDie(env, "android/view/MotionEvent");
+ gMotionEventClassInfo.clazz = MakeGlobalRefOrDie(env, gMotionEventClassInfo.clazz);
- GET_STATIC_METHOD_ID(gMotionEventClassInfo.obtain, gMotionEventClassInfo.clazz,
+ gMotionEventClassInfo.obtain = GetStaticMethodIDOrDie(env, gMotionEventClassInfo.clazz,
"obtain", "()Landroid/view/MotionEvent;");
- GET_METHOD_ID(gMotionEventClassInfo.recycle, gMotionEventClassInfo.clazz,
+ gMotionEventClassInfo.recycle = GetMethodIDOrDie(env, gMotionEventClassInfo.clazz,
"recycle", "()V");
- GET_FIELD_ID(gMotionEventClassInfo.mNativePtr, gMotionEventClassInfo.clazz,
+ gMotionEventClassInfo.mNativePtr = GetFieldIDOrDie(env, gMotionEventClassInfo.clazz,
"mNativePtr", "J");
- jclass clazz;
- FIND_CLASS(clazz, "android/view/MotionEvent$PointerCoords");
+ jclass clazz = FindClassOrDie(env, "android/view/MotionEvent$PointerCoords");
- GET_FIELD_ID(gPointerCoordsClassInfo.mPackedAxisBits, clazz,
- "mPackedAxisBits", "J");
- GET_FIELD_ID(gPointerCoordsClassInfo.mPackedAxisValues, clazz,
- "mPackedAxisValues", "[F");
- GET_FIELD_ID(gPointerCoordsClassInfo.x, clazz,
- "x", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.y, clazz,
- "y", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.pressure, clazz,
- "pressure", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.size, clazz,
- "size", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.touchMajor, clazz,
- "touchMajor", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.touchMinor, clazz,
- "touchMinor", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.toolMajor, clazz,
- "toolMajor", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.toolMinor, clazz,
- "toolMinor", "F");
- GET_FIELD_ID(gPointerCoordsClassInfo.orientation, clazz,
- "orientation", "F");
+ gPointerCoordsClassInfo.mPackedAxisBits = GetFieldIDOrDie(env, clazz, "mPackedAxisBits", "J");
+ gPointerCoordsClassInfo.mPackedAxisValues = GetFieldIDOrDie(env, clazz, "mPackedAxisValues",
+ "[F");
+ gPointerCoordsClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "F");
+ gPointerCoordsClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "F");
+ gPointerCoordsClassInfo.pressure = GetFieldIDOrDie(env, clazz, "pressure", "F");
+ gPointerCoordsClassInfo.size = GetFieldIDOrDie(env, clazz, "size", "F");
+ gPointerCoordsClassInfo.touchMajor = GetFieldIDOrDie(env, clazz, "touchMajor", "F");
+ gPointerCoordsClassInfo.touchMinor = GetFieldIDOrDie(env, clazz, "touchMinor", "F");
+ gPointerCoordsClassInfo.toolMajor = GetFieldIDOrDie(env, clazz, "toolMajor", "F");
+ gPointerCoordsClassInfo.toolMinor = GetFieldIDOrDie(env, clazz, "toolMinor", "F");
+ gPointerCoordsClassInfo.orientation = GetFieldIDOrDie(env, clazz, "orientation", "F");
- FIND_CLASS(clazz, "android/view/MotionEvent$PointerProperties");
+ clazz = FindClassOrDie(env, "android/view/MotionEvent$PointerProperties");
- GET_FIELD_ID(gPointerPropertiesClassInfo.id, clazz,
- "id", "I");
- GET_FIELD_ID(gPointerPropertiesClassInfo.toolType, clazz,
- "toolType", "I");
+ gPointerPropertiesClassInfo.id = GetFieldIDOrDie(env, clazz, "id", "I");
+ gPointerPropertiesClassInfo.toolType = GetFieldIDOrDie(env, clazz, "toolType", "I");
- return 0;
+ return res;
}
} // namespace android
diff --git a/core/jni/android_view_PointerIcon.cpp b/core/jni/android_view_PointerIcon.cpp
index 5e29213..bbd031e 100644
--- a/core/jni/android_view_PointerIcon.cpp
+++ b/core/jni/android_view_PointerIcon.cpp
@@ -25,6 +25,8 @@
#include <utils/Log.h>
#include <android/graphics/GraphicsJNI.h>
+#include "core_jni_helpers.h"
+
namespace android {
static struct {
@@ -106,42 +108,26 @@
// --- JNI Registration ---
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className); \
- var = jclass(env->NewGlobalRef(var));
-
-#define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_PointerIcon(JNIEnv* env) {
- FIND_CLASS(gPointerIconClassInfo.clazz, "android/view/PointerIcon");
+ jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
+ gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
- GET_FIELD_ID(gPointerIconClassInfo.mBitmap, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
"mBitmap", "Landroid/graphics/Bitmap;");
- GET_FIELD_ID(gPointerIconClassInfo.mStyle, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.mStyle = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
"mStyle", "I");
- GET_FIELD_ID(gPointerIconClassInfo.mHotSpotX, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
"mHotSpotX", "F");
- GET_FIELD_ID(gPointerIconClassInfo.mHotSpotY, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
"mHotSpotY", "F");
- GET_STATIC_METHOD_ID(gPointerIconClassInfo.getSystemIcon, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
"getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
- GET_METHOD_ID(gPointerIconClassInfo.load, gPointerIconClassInfo.clazz,
+ gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
"load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
return 0;
diff --git a/core/jni/android_view_RenderNode.cpp b/core/jni/android_view_RenderNode.cpp
index 050037e..d1e693b 100644
--- a/core/jni/android_view_RenderNode.cpp
+++ b/core/jni/android_view_RenderNode.cpp
@@ -28,6 +28,8 @@
#include <RenderNode.h>
#include <Paint.h>
+#include "core_jni_helpers.h"
+
namespace android {
using namespace uirenderer;
@@ -551,21 +553,8 @@
#endif
};
-#ifdef USE_OPENGL_RENDERER
- #define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
- #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-#else
- #define FIND_CLASS(var, className)
- #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor)
-#endif
-
int register_android_view_RenderNode(JNIEnv* env) {
- return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+ return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
};
diff --git a/core/jni/android_view_RenderNodeAnimator.cpp b/core/jni/android_view_RenderNodeAnimator.cpp
index ae42219..90b311a 100644
--- a/core/jni/android_view_RenderNodeAnimator.cpp
+++ b/core/jni/android_view_RenderNodeAnimator.cpp
@@ -25,6 +25,8 @@
#include <Interpolator.h>
#include <RenderProperties.h>
+#include "core_jni_helpers.h"
+
namespace android {
using namespace uirenderer;
@@ -197,22 +199,16 @@
#endif
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find method " methodName);
-
int register_android_view_RenderNodeAnimator(JNIEnv* env) {
- FIND_CLASS(gRenderNodeAnimatorClassInfo.clazz, kClassPathName);
- gRenderNodeAnimatorClassInfo.clazz = jclass(env->NewGlobalRef(gRenderNodeAnimatorClassInfo.clazz));
+ gRenderNodeAnimatorClassInfo.clazz = FindClassOrDie(env, kClassPathName);
+ gRenderNodeAnimatorClassInfo.clazz = MakeGlobalRefOrDie(env,
+ gRenderNodeAnimatorClassInfo.clazz);
- GET_STATIC_METHOD_ID(gRenderNodeAnimatorClassInfo.callOnFinished, gRenderNodeAnimatorClassInfo.clazz,
- "callOnFinished", "(Landroid/view/RenderNodeAnimator;)V");
+ gRenderNodeAnimatorClassInfo.callOnFinished = GetStaticMethodIDOrDie(
+ env, gRenderNodeAnimatorClassInfo.clazz, "callOnFinished",
+ "(Landroid/view/RenderNodeAnimator;)V");
- return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+ return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
diff --git a/core/jni/android_view_TextureView.cpp b/core/jni/android_view_TextureView.cpp
index 5c04a78..011c326 100644
--- a/core/jni/android_view_TextureView.cpp
+++ b/core/jni/android_view_TextureView.cpp
@@ -31,6 +31,8 @@
#include "android/graphics/GraphicsJNI.h"
+#include "core_jni_helpers.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -212,35 +214,22 @@
(void*) android_view_TextureView_unlockCanvasAndPost },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(!var, "Unable to find class " className);
-
-#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
- var = env->GetMethodID(clazz, methodName, methodDescriptor); \
- LOG_FATAL_IF(!var, "Unable to find method " methodName);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(!var, "Unable to find field" fieldName);
-
int register_android_view_TextureView(JNIEnv* env) {
- jclass clazz;
- FIND_CLASS(clazz, "android/graphics/Rect");
- GET_METHOD_ID(gRectClassInfo.set, clazz, "set", "(IIII)V");
- GET_FIELD_ID(gRectClassInfo.left, clazz, "left", "I");
- GET_FIELD_ID(gRectClassInfo.top, clazz, "top", "I");
- GET_FIELD_ID(gRectClassInfo.right, clazz, "right", "I");
- GET_FIELD_ID(gRectClassInfo.bottom, clazz, "bottom", "I");
+ jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
+ gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
+ gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
+ gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
+ gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
+ gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
- FIND_CLASS(clazz, "android/graphics/Canvas");
- GET_FIELD_ID(gCanvasClassInfo.mSurfaceFormat, clazz, "mSurfaceFormat", "I");
- GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V");
+ clazz = FindClassOrDie(env, "android/graphics/Canvas");
+ gCanvasClassInfo.mSurfaceFormat = GetFieldIDOrDie(env, clazz, "mSurfaceFormat", "I");
+ gCanvasClassInfo.setNativeBitmap = GetMethodIDOrDie(env, clazz, "setNativeBitmap", "(J)V");
- FIND_CLASS(clazz, "android/view/TextureView");
- GET_FIELD_ID(gTextureViewClassInfo.nativeWindow, clazz, "mNativeWindow", "J");
+ clazz = FindClassOrDie(env, "android/view/TextureView");
+ gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
- return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+ return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
};
diff --git a/core/jni/android_view_VelocityTracker.cpp b/core/jni/android_view_VelocityTracker.cpp
index 49d4da0..ddd5fc8 100644
--- a/core/jni/android_view_VelocityTracker.cpp
+++ b/core/jni/android_view_VelocityTracker.cpp
@@ -26,6 +26,7 @@
#include <ScopedUtfChars.h>
+#include "core_jni_helpers.h"
namespace android {
@@ -242,32 +243,18 @@
(void*)android_view_VelocityTracker_nativeGetEstimator },
};
-#define FIND_CLASS(var, className) \
- var = env->FindClass(className); \
- LOG_FATAL_IF(! var, "Unable to find class " className);
-
-#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
- var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
- LOG_FATAL_IF(! var, "Unable to find field " fieldName);
-
int register_android_view_VelocityTracker(JNIEnv* env) {
- int res = jniRegisterNativeMethods(env, "android/view/VelocityTracker",
- gVelocityTrackerMethods, NELEM(gVelocityTrackerMethods));
- LOG_FATAL_IF(res < 0, "Unable to register native methods.");
- (void)res;
+ int res = RegisterMethodsOrDie(env, "android/view/VelocityTracker", gVelocityTrackerMethods,
+ NELEM(gVelocityTrackerMethods));
- jclass clazz;
- FIND_CLASS(clazz, "android/view/VelocityTracker$Estimator");
+ jclass clazz = FindClassOrDie(env, "android/view/VelocityTracker$Estimator");
- GET_FIELD_ID(gEstimatorClassInfo.xCoeff, clazz,
- "xCoeff", "[F");
- GET_FIELD_ID(gEstimatorClassInfo.yCoeff, clazz,
- "yCoeff", "[F");
- GET_FIELD_ID(gEstimatorClassInfo.degree, clazz,
- "degree", "I");
- GET_FIELD_ID(gEstimatorClassInfo.confidence, clazz,
- "confidence", "F");
- return 0;
+ gEstimatorClassInfo.xCoeff = GetFieldIDOrDie(env, clazz, "xCoeff", "[F");
+ gEstimatorClassInfo.yCoeff = GetFieldIDOrDie(env, clazz, "yCoeff", "[F");
+ gEstimatorClassInfo.degree = GetFieldIDOrDie(env, clazz, "degree", "I");
+ gEstimatorClassInfo.confidence = GetFieldIDOrDie(env, clazz, "confidence", "F");
+
+ return res;
}
} // namespace android
diff --git a/core/jni/core_jni_helpers.h b/core/jni/core_jni_helpers.h
new file mode 100644
index 0000000..3f169c3
--- /dev/null
+++ b/core/jni/core_jni_helpers.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef CORE_JNI_HELPERS
+#define CORE_JNI_HELPERS
+
+#include "JNIHelp.h"
+#include <android_runtime/AndroidRuntime.h>
+
+namespace android {
+
+// Defines some helpful functions.
+
+static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
+ jclass clazz = env->FindClass(class_name);
+ LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
+ return clazz;
+}
+
+static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
+ const char* field_signature) {
+ jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
+ return res;
+}
+
+static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
+ const char* method_signature) {
+ jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
+ return res;
+}
+
+static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
+ const char* field_signature) {
+ jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
+ return res;
+}
+
+static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
+ const char* method_signature) {
+ jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
+ return res;
+}
+
+template <typename T>
+static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
+ jobject res = env->NewGlobalRef(in);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
+ return static_cast<T>(res);
+}
+
+static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
+ const JNINativeMethod* gMethods, int numMethods) {
+ int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods);
+ LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
+ return res;
+}
+
+} // namespace android
+
+#endif // CORE_JNI_HELPERS