jni 返回java对象有什么需要注意的地方
1个回答
展开全部
JNIEXPORT jobject JNICALL Java_com_example_filltriangle_GL2JNILib_test(JNIEnv* env, jclass tis)
{
jclass m_login_msg = env->FindClass("hzcw/msgdata/LoginMsg");
jmethodID m_method_login_msg_init = env->GetMethodID(m_login_msg,"<init>","()V");
jobject m_login_msg_obj = env->NewObject(m_login_msg, m_method_login_msg_init);
jfieldID m_fid_controlDictionary = env->GetFieldID(m_login_msg,"controlDictionary","Ljava/util/HashMap;");
// new一个hashmap对象
jclass class_hashmap=env->FindClass("java/util/HashMap");
jmethodID hashmap_construct_method=env->GetMethodID(class_hashmap, "<init>","()V");
jobject obj_hashmap =env->NewObject(class_hashmap, hashmap_construct_method, "");
jmethodID hashmap_put_method= env->GetMethodID(class_hashmap,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
// new一个ArrayList对象
jclass class_arraylist=env->FindClass("java/util/ArrayList");
jmethodID arraylist_construct_method=env->GetMethodID(class_arraylist, "<init>","()V");
jobject obj_arraylist =env->NewObject(class_arraylist, arraylist_construct_method, "");
jmethodID arraylist_add_method= env->GetMethodID(class_arraylist,"add","(Ljava/lang/Object;)Z");
// new一个ControlDictionaryValue对象
jclass m_cls_ControlDictionaryValue = env->FindClass("hzcw/datastruct/ControlDictionaryValue");
jmethodID m_mid_ControlDictionaryValue = env->GetMethodID(m_cls_ControlDictionaryValue,"<init>","()V");
jobject m_obj_ControlDictionaryValue = env->NewObject(m_cls_ControlDictionaryValue, m_mid_ControlDictionaryValue, "");
// 获取ControlDictionaryValue对象的value变量
jfieldID m_fid_ControlDictionaryValue_value = env->GetFieldID(m_cls_ControlDictionaryValue,"value","Ljava/lang/String;");
env->SetObjectField(m_obj_ControlDictionaryValue,m_fid_ControlDictionaryValue_value,env->NewStringUTF("牙科"));
env->CallBooleanMethod(obj_arraylist, arraylist_add_method, m_obj_ControlDictionaryValue);
env->CallObjectMethod(obj_hashmap,hashmap_put_method,env->NewStringUTF((char*)"科室"), obj_arraylist);
env->SetObjectField(m_login_msg_obj,m_fid_controlDictionary,obj_hashmap);
env->DeleteLocalRef(m_obj_ControlDictionaryValue); //删除局部引用
env->DeleteLocalRef(obj_arraylist); //删除局部引用
env->DeleteLocalRef(obj_hashmap); //删除局部引用
return m_login_msg_obj;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询