JNI各種タイプ変換まとめ


JNI自体は複雑ではなく,難点はタイプ変換にあり,以下ではタイプ変換についてまとめる.
1、基本タイプ、直接変換
static jbyte byte_test_jni(JNIEnv *env, jclass clazz, jbyte bt)
{
	LOGD("%s:%u, static_use_byte!------bt :%d", __func__, __LINE__,(int)bt);
	jbyte ret = 't';
	return ret;
}

2、Stringに戻る
static jstring static_hello_jni(JNIEnv *env, jclass clazz)
{
	LOGD("%s:%u, static_hello", __func__, __LINE__);
	return (env)->NewStringUTF("static hello jni.");
}

3、クラスをパラメータとする
(1)インスタンスオブジェクトによるクラスの取得
(2)クラス取得方法とフィールドID
(3)処理を行う
static void class_arg_test_jni(JNIEnv *env, jclass clazz, jobject obj)
{
	LOGD("%s:%u, class_arg_test_jni", __func__, __LINE__);
	//         
	jclass objClass = env->GetObjectClass(obj);
	if(objClass)
	{
	    /***       ID**********int*****/
	    jfieldID intID = env->GetFieldID(objClass,"nValue","I");
	    /***    ID          *********/
	    jint nValue = (int)env->GetIntField(obj,intID);
	    LOGD("%s:%u, class_arg_test_jni, nValue :%d", __func__, __LINE__, nValue );
	    /*****           ********/
	    env->SetIntField(obj,intID,222);

	    //str
	    jfieldID strID = env->GetFieldID(objClass,"strValue","Ljava/lang/String;");
	    jstring str = env->NewStringUTF("XXXXXXXXXXXXOOOOOOOOOOOOOOOOO");
	    env->SetObjectField(obj,strID,str);

	    //array-----------    nArr 
	    jfieldID intArrID = env->GetFieldID(objClass,"nArr","[I");
	    jint nArr[8] = {8,7,6,5,4,3,2,1};
	    jintArray jnArray = env->NewIntArray(8);
	    env->SetIntArrayRegion(jnArray,0,8,nArr);
	    env->SetObjectField(obj,intArrID,jnArray);

	    //  enum  
	    /*******1、       ID***************/
	    jfieldID enumID = env->GetFieldID(objClass,"mdate","Lcom/example/data/TestClass$DateIn;");
	    /*******2、  ID            (    )************/
	    jobject jenum = (jclass)env->GetObjectField(obj,enumID);
	    if(jenum == NULL) {
	    	LOGD("%s:%u, get enum failed", __func__, __LINE__);
	    	return;
	    }
	    LOGD("%s:%u, ok", __func__, __LINE__ );
	    /********3、       ID**************/
	    jclass jenumClass = (env)->FindClass("com/example/data/TestClass$DateIn");
	    jmethodID getVal = env->GetMethodID(jenumClass, "name", "()Ljava/lang/String;");

	    jstring value = (jstring)env->CallObjectMethod(jenum, getVal);
	    const char * valueNative = env->GetStringUTFChars(value, 0);

	    if (strcmp(valueNative, "MONDAY") == 0)
	    	LOGI("%s:%u, TODAY IS MONDAY!", __func__, __LINE__);
	    else if(strcmp(valueNative, "TUESDAY") == 0)
	    	LOGI("%s:%u, TODAY IS NOT TUESDAY!", __func__, __LINE__);
	   	else
	    	LOGI("%s:%u, TODAY", __func__, __LINE__);
	}
}

4、クラスに戻る
static jobject class_return_test_jni(JNIEnv *env, jclass clazz)
{
	LOGD("%s:%u, class_return_test_jni", __func__, __LINE__);
	//         
	jclass objectClass = (env)->FindClass("com/example/data/TestClass");
	//         
	jmethodID mid = (env)->GetMethodID(objectClass,"<init>","()V");

	/*********  java   TestClass********/
	jobject  m_obj   = env->NewObject(objectClass,mid);
	LOGI("%s:%u, static_deal----GetMethodID", __func__, __LINE__ );

	/*******************       ID******************/
	jfieldID ival = (env)->GetFieldID(objectClass,"nValue","I");
	//jfieldID strID = env->GetFieldID(objectClass,"strValue","Ljava/lang/String;");

	/***************** jni   java     *****************/
	(env)->SetIntField(m_obj,ival,100);
	return m_obj;
}

5、列挙を返す
void enum_arg_test_jni(JNIEnv *env, jclass clazz, jobject obj2)
{
	LOGD("%s:%u, enum_arg_test_jni", __func__, __LINE__);
	//          
	jclass enumclass= env->GetObjectClass(obj2);
	if(enumclass == NULL) {
		LOGD("%s:%u, get enum failed", __func__, __LINE__);
	       return;
	}
	 //name()   enum       ,()Ljava/lang/String;      
	//1、       ID
	jmethodID getVal = env->GetMethodID(enumclass, "name", "()Ljava/lang/String;");
	//2、    ID  ,     name()  
	jstring value = (jstring)env->CallObjectMethod(obj2, getVal);
	const char * valueNative = env->GetStringUTFChars(value, 0);

	if (strcmp(valueNative, "MONDAY") == 0)
		LOGI("%s:%u, TODAY IS MONDAY!", __func__, __LINE__);
	else if(strcmp(valueNative, "TUESDAY") == 0)
		LOGI("%s:%u, TODAY IS NOT TUESDAY!", __func__, __LINE__);
	else
		LOGI("%s:%u, TODAY", __func__, __LINE__);
}

demoダウンロード