sample.cc:
std::list<std::string> lst;
lst.push_back("this");
lst.push_back("is");
lst.push_back("test");
lst.push_back("message");
java_c_reference.cc:
CALLBACK_START("handleSampleData",**XXXX**, objectReference);
if(mid != NULL){
env->CallVoidMethod(reference_->handler_object, mid, **YYYY**);
}
DETACH_FROM_VM(reference_);
此处XXXX为签名,YYYY为 JNI数据类型
如何使用std::list<std::string> lst
的签名和JNI类型来获取我的Java ArryList<String>
Test.java:
public void handleSampleData(ArrayList<String> arrSample){
}
答案 0 :(得分:1)
简单的答案,迭代地将它复制到您在JNI中构造的ArrayList,然后将该新对象传递回JVM。
jclass ArrayList_class = (*env)->FindClass(env, "java/util/ArrayList");
ArrayList_init_id = (*env)->GetMethodID(env, ArrayList_class, "<init>", "()V");
ArrayList_add_id = (*env)->GetMethodID(env, ArrayList_class, "add", "(Ljava/lang/Object;)Z");
jobject List_obj = (*env)->NewObject(env, ArrayList_class, ArrayList_init_id);
// Begin psudo-code
foreach (item in lst) {
(*env)->CallVoidMethod(env, List_obj, ArrayList_add_id, (*env)->NewStringUTF(env, item));
}
// Now pass List_obj to where ever it needs to go.
// I usually call DeleteLocalRef to make sure the objects get garbage collected.
(*env)->DeleteLocalRef(env, List_obj);