Simplify toStringArray implementation
Moves all code out of toStringArray.cpp and removes toStringArray()
from the exported API.
toStringArray() is now implemented inline in toStringArray.h.
newStringArray() is renamed to jniCreateStringArray() and moved
to JNIHelp.cpp.
Bug: 151443957
Test: m
Change-Id: I1ecc9ea9d55615eec7f3b86deedd1456f3be7f09
diff --git a/include/nativehelper/libnativehelper_api.h b/include/nativehelper/libnativehelper_api.h
index a5368b2..a38d1a9 100644
--- a/include/nativehelper/libnativehelper_api.h
+++ b/include/nativehelper/libnativehelper_api.h
@@ -160,6 +160,15 @@
jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
/*
+ * Allocates a new array for java/lang/String instances with space for |count| elements. Elements
+ * are initially null.
+ *
+ * Returns a new array on success or nullptr in case of failure. This method raises an
+ * OutOfMemoryError exception if allocation fails.
+ */
+jobjectArray jniCreateStringArray(JNIEnv* env, size_t count);
+
+/*
* Log a message and an exception.
* If exception is NULL, logs the current exception in the JNI environment.
*/
@@ -231,15 +240,6 @@
/* ---------------------------------- C API for toStringArray.h --------------------------------- */
/*
- * Allocates a new array for java/lang/String instances with space for |count| elements. Elements
- * are initially null.
- *
- * Returns a new array on success or nullptr in case of failure. This method raises an
- * OutOfMemoryError exception if allocation fails.
- */
-jobjectArray newStringArray(JNIEnv* env, size_t count);
-
-/*
* Converts an array of C strings into a managed array of Java strings. The size of the C array is
* determined by the presence of a final element containing a nullptr.
*
diff --git a/include/nativehelper/toStringArray.h b/include/nativehelper/toStringArray.h
index 02072b0..80ea797 100644
--- a/include/nativehelper/toStringArray.h
+++ b/include/nativehelper/toStringArray.h
@@ -25,45 +25,38 @@
#include <vector>
#include "ScopedLocalRef.h"
-template <typename Counter, typename Getter>
-jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
- size_t count = (*counter)();
- jobjectArray result = newStringArray(env, count);
- if (result == NULL) {
- return NULL;
+template <typename StringVisitor>
+jobjectArray toStringArray(JNIEnv* env, size_t count, StringVisitor&& visitor) {
+ jobjectArray result = jniCreateStringArray(env, count);
+ if (result == nullptr) {
+ return nullptr;
}
for (size_t i = 0; i < count; ++i) {
- ScopedLocalRef<jstring> s(env, env->NewStringUTF((*getter)(i)));
+ ScopedLocalRef<jstring> s(env, env->NewStringUTF(visitor(i)));
if (env->ExceptionCheck()) {
- return NULL;
+ return nullptr;
}
env->SetObjectArrayElement(result, i, s.get());
if (env->ExceptionCheck()) {
- return NULL;
+ return nullptr;
}
}
return result;
}
-struct VectorCounter {
- const std::vector<std::string>& strings;
- explicit VectorCounter(const std::vector<std::string>& strings) : strings(strings) {}
- size_t operator()() {
- return strings.size();
- }
-};
-struct VectorGetter {
- const std::vector<std::string>& strings;
- explicit VectorGetter(const std::vector<std::string>& strings) : strings(strings) {}
- const char* operator()(size_t i) {
- return strings[i].c_str();
- }
-};
-
inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
- VectorCounter counter(strings);
- VectorGetter getter(strings);
- return toStringArray<VectorCounter, VectorGetter>(env, &counter, &getter);
+ return toStringArray(env, strings.size(), [&strings](size_t i) { return strings[i].c_str(); });
+}
+
+inline jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
+ size_t count = 0;
+ for (; strings[count] != nullptr; ++count) {}
+ return toStringArray(env, count, [&strings](size_t i) { return strings[i]; });
+}
+
+template <typename Counter, typename Getter>
+jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
+ return toStringArray(env, counter(), [getter](size_t i) { return getter(i); });
}
#endif // __cplusplus