Add jniThrowExceptionFmt.

I still think you should be doing checks and throwing exceptions in Java,
and your native code should just crash if you lie to it, but this will
ease the translation of frameworks/base code over to using JNI more correctly.

Change-Id: I9f2512e2349452b82360b375911c814ab00db23b
diff --git a/libnativehelper/JNIHelp.c b/libnativehelper/JNIHelp.c
index e5abc0f..fb23a9e 100644
--- a/libnativehelper/JNIHelp.c
+++ b/libnativehelper/JNIHelp.c
@@ -211,6 +211,14 @@
     return result;
 }
 
+int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt,
+        va_list args)
+{
+    char msgBuf[512];
+    vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
+    return jniThrowException(env, className, msgBuf);
+}
+
 /*
  * Throw a java.lang.NullPointerException, with an optional message.
  */
diff --git a/libnativehelper/include/nativehelper/JNIHelp.h b/libnativehelper/include/nativehelper/JNIHelp.h
index 71ecf38..1b5ff0c 100644
--- a/libnativehelper/include/nativehelper/JNIHelp.h
+++ b/libnativehelper/include/nativehelper/JNIHelp.h
@@ -118,6 +118,23 @@
 {
     return jniThrowException(&env->functions, className, msg);
 }
+
+extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className,
+        const char* fmt, va_list args);
+
+/*
+ * Equivalent to jniThrowException but with a printf-like format string and
+ * variable-length argument list. This is only available in C++.
+ */
+inline int jniThrowExceptionFmt(JNIEnv* env, const char* className,
+        const char* fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+    return jniThrowExceptionFmt(&env->functions, className, fmt, args);
+    va_end(args);
+}
+
 inline int jniThrowNullPointerException(JNIEnv* env, const char* msg)
 {
     return jniThrowNullPointerException(&env->functions, msg);