Merge "Simplify toStringArray implementation"
diff --git a/Android.bp b/Android.bp
index a9c478b..2d091f4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -56,7 +56,6 @@
"JNIHelp.cpp",
"JniConstants.cpp",
"JniInvocation.cpp",
- "toStringArray.cpp",
],
shared_libs: ["liblog"],
cflags: [
@@ -109,7 +108,6 @@
srcs: [
"JNIHelp.cpp",
"JniConstants.cpp",
- "toStringArray.cpp",
],
shared_libs: [
"liblog",
diff --git a/JNIHelp.cpp b/JNIHelp.cpp
index 0dce458..b91e789 100644
--- a/JNIHelp.cpp
+++ b/JNIHelp.cpp
@@ -408,6 +408,10 @@
return e->NewString(unicodeChars, len);
}
+jobjectArray jniCreateStringArray(JNIEnv* env, size_t count) {
+ return env->NewObjectArray(count, JniConstants::GetStringClass(env), nullptr);
+}
+
void jniUninitializeConstants() {
JniConstants::Uninitialize();
}
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
diff --git a/libnativehelper.map.txt b/libnativehelper.map.txt
index 171533b..c092b61 100644
--- a/libnativehelper.map.txt
+++ b/libnativehelper.map.txt
@@ -23,6 +23,7 @@
jniGetNioBufferFields;
jniGetReferent;
jniCreateString;
+ jniCreateStringArray;
jniLogException;
jniUninitializeConstants;
@@ -31,9 +32,6 @@
JniInvocationInit;
JniInvocationGetLibrary;
- newStringArray;
- toStringArray;
-
local:
*;
};
diff --git a/toStringArray.cpp b/toStringArray.cpp
deleted file mode 100644
index 0c6a1a9..0000000
--- a/toStringArray.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2011 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.
- */
-
-#include <nativehelper/toStringArray.h>
-
-#include "JniConstants.h"
-
-namespace {
-
-struct ArrayCounter {
- const char* const* strings;
- explicit ArrayCounter(const char* const* strings) : strings(strings) {}
- size_t operator()() {
- size_t count = 0;
- while (strings[count] != nullptr) {
- ++count;
- }
- return count;
- }
-};
-
-struct ArrayGetter {
- const char* const* strings;
- explicit ArrayGetter(const char* const* strings) : strings(strings) {}
- const char* operator()(size_t i) {
- return strings[i];
- }
-};
-
-} // namespace
-
-jobjectArray newStringArray(JNIEnv* env, size_t count) {
- return env->NewObjectArray(count, JniConstants::GetStringClass(env), nullptr);
-}
-
-jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
- ArrayCounter counter(strings);
- ArrayGetter getter(strings);
- return toStringArray(env, &counter, &getter);
-}