2023-01-20 276
我有一个Object具有HashMap字段.当将Object传递给C时,如何访问该字段?
Object’s Class具有以下字段:
private String hello;
private Map<String, String> params = new HashMap<String, String>();
您问题的答案确实归结为为什么要将Map传递给C,而不是在Java中迭代您的Map并将内容传递给C但是,我要问谁?为什么?
您询问如何访问HashMap(在您提供的代码中,Map)字段?在Java中编写一个登录方法,并在通过容器Object时从C调用该访问者方法.以下是一些裸露的示例代码,显示了如何将Map从java传递到c,以及如何访问Map的size()方法.从中,您应该能够推断如何调用其他方法.
容器对象:
public class Container {
private String hello;
private Map<String, String> parameterMap = new HashMap<String, String>();
public Map<String, String> getParameterMap() {
return parameterMap;
}
}
大师课程将容器传递到JNI:
public class MyClazz {
public doProcess() {
Container container = new Container();
container.getParameterMap().put("foo","bar");
manipulateMap(container);
}
public native void manipulateMap(Container container);
}
相关C函数:
JNIEXPORT jint JNICALL Java_MyClazz_manipulateMap(JNIEnv *env, jobject selfReference, jobject jContainer) {
// initialize the Container class
jclass c_Container = (*env)->GetObjectClass(env, jContainer);
// initialize the Get Parameter Map method of the Container class
jmethodID m_GetParameterMap = (*env)->GetMethodID(env, c_Container, "getParameterMap", "()Ljava/util/Map;");
// call said method to store the parameter map in jParameterMap
jobject jParameterMap = (*env)->CallObjectMethod(env, jContainer, m_GetParameterMap);
// initialize the Map interface
jclass c_Map = env->FindClass("java/util/Map");
// initialize the Get Size method of Map
jmethodID m_GetSize = (*env)->GetMethodID(env, c_Map, "size", "()I");
// Get the Size and store it in jSize; the value of jSize should be 1
int jSize = (*env)->CallIntMethod(env, jParameterMap, m_GetSize);
// define other methods you need here.
}
注意,我对方法本身的初始化方法和类并不疯狂. 这个答案向您展示了如何缓存它们以重复使用.
以上所述是小编给大家介绍的如何通过JNI向java发送java到c的hashmap,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/26079.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态