diff options
author | Sven Gothel <[email protected]> | 2022-06-03 06:16:09 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-06-03 06:16:09 +0200 |
commit | 44c438e78f2c560890cab65800d482ec4f0e578e (patch) | |
tree | 36e19a0e0ef3d6c038c41933d70e9814cbe09610 | |
parent | 7fe60db0c28dfe9bfc269fba73d5846a0c518844 (diff) |
JNI: Add convert_jlist_string_to_vector(), the reverse function of convert_vector_string_to_jarraylist()
-rw-r--r-- | include/jau/jni/helper_jni.hpp | 2 | ||||
-rw-r--r-- | java_jni/jni/helper_jni.cxx | 21 |
2 files changed, 23 insertions, 0 deletions
diff --git a/include/jau/jni/helper_jni.hpp b/include/jau/jni/helper_jni.hpp index beff04c..c37adf2 100644 --- a/include/jau/jni/helper_jni.hpp +++ b/include/jau/jni/helper_jni.hpp @@ -133,6 +133,8 @@ namespace jau { jobject get_new_arraylist(JNIEnv *env, jsize size, jmethodID *add); jobject convert_vector_string_to_jarraylist(JNIEnv *env, const std::vector<std::string>& array); + std::vector<std::string> convert_jlist_string_to_vector(JNIEnv *env, jobject jlist); + // // C++ JavaAnon implementation // diff --git a/java_jni/jni/helper_jni.cxx b/java_jni/jni/helper_jni.cxx index 8a72d00..064f85f 100644 --- a/java_jni/jni/helper_jni.cxx +++ b/java_jni/jni/helper_jni.cxx @@ -384,6 +384,27 @@ jobject jau::convert_vector_string_to_jarraylist(JNIEnv *env, const std::vector< return result; } +std::vector<std::string> jau::convert_jlist_string_to_vector(JNIEnv *env, jobject jlist) +{ + std::vector<std::string> result; + + jclass list_class = search_class(env, "java/util/List"); + jmethodID list_size = search_method(env, list_class, "size", "()I", false); + nsize_t array_size = env->CallIntMethod(jlist, list_size); + + if (0 == array_size) { + return result; + } + jmethodID list_get = search_method(env, list_class, "get", "(I)Ljava/lang/Object;", false); + + for(nsize_t i=0; i<array_size; ++i) { + jobject jstr = env->CallObjectMethod(jlist, list_get, i); + result.push_back( jau::from_jstring_to_string(env, (jstring)jstr) ); + env->DeleteLocalRef(jstr); + } + return result; +} + // // C++ java_anon implementation // |