如何通过JNI将HashMap从Java发送到C [英] How to send a HashMap from Java to C via JNI
问题描述
我有一个 Object
,它有一个 HashMap
字段。当 Object
传递给C时,我如何访问该字段?
对象
的 Class
包含以下字段:
private String hello;
私人地图< String,String> params = new HashMap< String,String>();
您的问题的答案可以归结为为什么你想要将 Map
传递给C,而不是迭代Java中的 Map
并且将内容传递给C.但是,我是谁来质疑为什么?
你问如何访问 HashMap
(在你提供的代码中, Map
)字段?在Java中为它写一个访问器方法,并在你传递容器
Object
时调用C的访问器方法。下面是一些简单的示例代码,展示了如何将Java中的 Map
传递给C,以及如何访问 size()
Map
的方法。从它,你应该能够推断如何调用其他方法。
容器对象:
public class Container {
private String hello;
私人地图< String,String> parameterMap = new HashMap< String,String>();
public Map< String,String> getParameterMap(){
return parameterMap;
$ b 将Container传递给JNI的主类: p>
public class MyClazz {
public doProcess(){
Container container = new Container();
container.getParameterMap()。put(foo,bar);
manipulateMap(container);
}
public native void manipulateMap(Container container);
$ / code>
相关C函数:
JNIEXPORT jint JNICALL Java_MyClazz_manipulateMap(JNIEnv * env,jobject selfReference,jobject jContainer){
//初始化Container类
jclass c_Container =(* env) - > GetObjectClass(env,jContainer);
//初始化Container类的Get Parameter Map方法
jmethodID m_GetParameterMap =(* env) - > GetMethodID(env,c_Container,getParameterMap,()Ljava / util /地图;);
//调用所述方法将参数映射存储在jParameterMap中$ b $ jobject jParameterMap =(* env) - > CallObjectMethod(env,jContainer,m_GetParameterMap);
//初始化Map接口
jclass c_Map = env-> FindClass(java / util / Map);
//初始化Map
的Get Size方法jmethodID m_GetSize =(* env) - > GetMethodID(env,c_Map,size,()I);
//获取大小并将其存储在jSize中; jSize的值应该是1
int jSize =(* env) - > CallIntMethod(env,jParameterMap,m_GetSize);
//定义您需要的其他方法。
}
值得注意的是,我并没有在方法中初始化methodID和类本身; 这个答案显示了如何将它们缓存以供重用。
I have an Object
that has a HashMap
field. When the Object
is passed to C, how can I access the field?
The Object
's Class
has the following fields:
private String hello;
private Map<String, String> params = new HashMap<String, String>();
The answer to your question really boils down to why you'd want to pass a Map
to C rather than iterate your Map
in Java and pass the contents to C. But, who am I to question why?
You ask how to access the HashMap
(in your provided code, Map
) field? Write an accessor method for it in Java and call that accessor method from C when you pass the container Object
. Below is some bare-bones sample code showing how to pass a Map
from Java to C, and how to access the size()
method of the Map
. From it, you should be able to extrapolate how to call other methods.
Container Object:
public class Container {
private String hello;
private Map<String, String> parameterMap = new HashMap<String, String>();
public Map<String, String> getParameterMap() {
return parameterMap;
}
}
Master Class which passes a Container to JNI:
public class MyClazz {
public doProcess() {
Container container = new Container();
container.getParameterMap().put("foo","bar");
manipulateMap(container);
}
public native void manipulateMap(Container container);
}
Relevant C function:
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.
}
Of note, I'm not crazy about initializing methodIDs and classes in the method itself; this SO Answer shows you how to cache them for re-use.
这篇关于如何通过JNI将HashMap从Java发送到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!