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 ?

[430 byte] By [Ramin.Massachia] at [2007-11-27 7:07:50]
# 1

Clarification:

My HashMap contains <String,String>

I know I can use GetObjectClass & GetFieldID and HetMethodID and ...

But how can i draw the <String,String>.

1) I should use the HashMap APIin JNI code.!!!!?

2) How to convert the HashMap to known MAP in C++ without using the HashMap and it's API ?

Ramin.Massachia at 2007-7-12 18:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
1. Yes - use the Hashmap methods - via JNI - to retrieve your data.2. Use whatever C or C++ classes/methods you need to create a native mapping.
bschauwejavaa at 2007-7-12 18:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 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");

.

.

.

}

Ramin.Massachia at 2007-7-12 18:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Weell, you could create your own java class, and pass and instance of theat class to your JNI method.Your own class can be much more precise - maybe having three methods:int getSize();String getLeft(int index);String getRight(Index);
bschauwejavaa at 2007-7-12 18:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...