How to convert HashMap from Java to JNI(c++)
We are currently using JNI for transferring simpele/primitive types from Java to C++ successfully.
1) How JNI is enables to convert complex objects such as hashMap, Vectores from Java to C++ and vice versa.
2) We want to send a Hashmap .We try to send the Hashmap as a "jobject". The question is how can we convert this object to a known MAP in JNI(C++).
3) is there a better way to convert the HashMap ?
# 3
Hi -
Thanks for you help
If I understand correctly you mean I have to use the "GetObjectClass / GetMethodID / CallObjectMethod / FindClass" in order to get the next HashMap methodes:
hasNext() / hasNextMethod() / next() /getKey() / getValue() & iterator ..
Please see the next code.
I think it's a lot of actions. Or I miss something !!!
Do you have another way to convert the list of <String,String> from Java to JNI with less actions ?
Thanks again
JNIEXPORT jint JNICALL Java_ReadFile_SendNewParams
(JNIEnv * env, jobject jobj, jobject hashMap)
{
// Get the HashMap Class
jclass jclass_of_hashmap = (env)->GetObjectClass(hashMap);
// Get link to Method "entrySet"
jmethodID entrySetMethod = (env)->GetMethodID(jclass_of_hashmap, "entrySet", "()Ljava/util/Set;");
// Invoke the "entrySet" method on the HashMap object
jobject jobject_of_entryset = env->CallObjectMethod(hashMap, entrySetMethod);
// Get the Set Class
jclass jclass_of_set = (env)->FindClass("java/util/Set"); // Problem during compilation !!!!!
if (jclass_of_set == 0) {
printf("java/util/Set lookup failed\n");
return;
}
// Get link to Method "iterator"
jmethodID iteratorMethod = env->GetMethodID(jclass_of_set, "iterator", "()Ljava/util/Iterator;");
// Invoke the "iterator" method on the jobject_of_entryset variable of type Set
jobject jobject_of_iterator = env->CallObjectMethod(jobject_of_entryset, iteratorMethod);
// Get the "Iterator" class
jclass jclass_of_iterator = (env)->FindClass("java/util/Iterator");
// Get link to Method "hasNext"
jmethodID hasNextMethod = env->GetMethodID(jclass_of_iterator, "hasNext", "()Z");
// Invoke - Get the value hasNextMethod
jboolean bHasNext = env->CallBooleanMethod(jobject_of_iterator, hasNextMethod);
// Get link to Method "hasNext"
jmethodID nextMethod = env->GetMethodID(jclass_of_iterator, "next", "()Ljava/util/Map/Entry;");
jclass jclass_of_mapentry = (env)->FindClass("java/util/Map/Entry");
jmethodID getKeyMethod = env->GetMethodID(jclass_of_mapentry, "getKey", "()Ljava/lang/Object");
jmethodID getValueMethod = env->GetMethodID(jclass_of_mapentry, "getValue", "()Ljava/lang/Object");
.
.
.
}