Search code examples
javac++java-native-interfacecocos2d-x

How to convert Java Map to cocos2d ValueMap?


Short Question:

How Can I convert jobject to cocos2d::ValueMap?

Detailed Question:

I have Java part:

public class MyCallback implements MyListener {

    public native void callback(Object data);

    public MyCallback(){}

    @Override
    public void onResponse(Map<String, String> data) {
        callback(data);
    }
}

I want to return Map data to Cocos2d Class. So I wrote:

JNIEXPORT void JNICALL Java_com_comp_ MyCallback_ callback
    (JNIEnv *env, jobject obj, jobject data) {

and I get jobject data

So now how to get ValueMap from jobject?

cocos2d::ValueMap myData = ... ???

Solution

  • HashMaps are object that have to be accessed to get the content. So, lots of code ahead of you ;)

    Take a look below:

    #include <stdio.h>
    #include "jni.h"
    #include "recipeNo037_PassHashMap.h"
    
    JNIEXPORT int JNICALL Java_recipeNo037_PassHashMap_displayHashMap
      (JNIEnv *env, jclass obj, jobject objarg) {
    
      /* Get objarg's class - objarg is the one we pass from
         Java */
      jclass clsHashMap = (*env)->GetObjectClass(env, objarg);
    
      /* Remember that you can alway get method signature using javap tool
         > javap -s -p java.util.HashMap | grep -A 1 key
             public java.util.Set<K> keySet();
               descriptor: ()Ljava/util/Set;
      */
    
      jmethodID midKeySet =
        (*env)->GetMethodID(env, clsHashMap, "keySet", "()Ljava/util/Set;");
    
      /* We have to make sure that method exists */
      if (midKeySet == NULL) {
        return -1; /* method not found */
      }
    
      /* Now, it's time for getting Set of keys */
      jobject objKeySet = (*env)->CallObjectMethod(env, objarg, midKeySet);
    
      /* Then, we can proceed to accessing keys */
      jclass clsSet = (*env)->GetObjectClass(env, objKeySet);
    
      /* The same story goes here - use javap to get propper descriptor
         > javap -s -p java.util.Set | grep -A 1 toArray
             public abstract java.lang.Object[] toArray();
               descriptor: ()[Ljava/lang/Object;
      */
    
      jmethodID midToArray =
        (*env)->GetMethodID(env, clsSet, "toArray", "()[Ljava/lang/Object;");
    
      if (midKeySet == NULL) {
        return -2; /* method not found */
      }
    
      jobjectArray arrayOfKeys = (*env)->CallObjectMethod(env, objKeySet, midToArray);
    
      int arraySize = (*env)->GetArrayLength(env, arrayOfKeys);
    
      for (int i=0; i < arraySize; i++)
      {
        jstring objKey = (*env)->GetObjectArrayElement(env, arrayOfKeys, i);
        const char* c_string_key = (*env)->GetStringUTFChars(env, objKey, 0);
    
        /* Once we have key, we can retrieve value for that key */
        jmethodID midGet =
          (*env)->GetMethodID(env, clsHashMap, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
    
        /* It's time to get Value for Key */
        jobject objValue = (*env)->CallObjectMethod(env, objarg, midGet, objKey);
        const char* c_string_value = (*env)->GetStringUTFChars(env, objValue, 0);
    
        printf("[key, value] = [%s, %s]\n", c_string_key, c_string_value);
    
        (*env)->ReleaseStringUTFChars(env, objKey, c_string_key);
        (*env)->DeleteLocalRef(env, objKey);
    
        (*env)->ReleaseStringUTFChars(env, objValue, c_string_value);
        (*env)->DeleteLocalRef(env, objValue);
      }
    
      return 0;
    
    }
    

    For a full sample code, take a look here: recipeNo037

    I, personally, would have HashMap replaced by two Array objects. This way, lots of code can be removed.

    Take a look here (recipeNo038) for alternative approach:

    This time, we are passing two Arrays of Strings. They are aligned such way, that corresponding indexes contain key/value from HashMap. This way, we can heavily reduce the code in C while overhead in Java is not that big.

    JNIEXPORT int JNICALL Java_recipeNo038_PassHashMap_displayHashMap
      (JNIEnv *env, jclass obj, jobjectArray keys, jobjectArray values) {
    
      /* We need to get array size. There is strong assumption that
         keys and values have the same length
      */
      int arraySize = (*env)->GetArrayLength(env, keys);
    
      /* For all elements in array, we will convert them to C based strings
      */
      for (int i=0; i < arraySize; i++)
      {
        /* First, we take key */
        jstring objKey = (*env)->GetObjectArrayElement(env, keys, i);
        const char* c_string_key = (*env)->GetStringUTFChars(env, objKey, 0);
    
        /* Then, we take the value value  */
        jobject objValue = (*env)->GetObjectArrayElement(env, values, i);
        const char* c_string_value = (*env)->GetStringUTFChars(env, objValue, 0);
    
        /* And we print some info for user */
        printf("[key, value] = [%s, %s]\n", c_string_key, c_string_value);
    
        /* Make sure to release stuff */
        (*env)->ReleaseStringUTFChars(env, objKey, c_string_key);
        (*env)->DeleteLocalRef(env, objKey);
    
        (*env)->ReleaseStringUTFChars(env, objValue, c_string_value);
        (*env)->DeleteLocalRef(env, objValue);
      }
    
      return 0;
    
    }