如何通过 JNI 将 HashMap 从 Java 发送到 C

How to send a HashMap from Java to C via JNI(如何通过 JNI 将 HashMap 从 Java 发送到 C)
本文介绍了如何通过 JNI 将 HashMap 从 Java 发送到 C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 Object 有一个 HashMap 字段.当 Object 传递给 C 时,如何访问该字段?

I have an Object that has a HashMap field. When the Object is passed to C, how can I access the field?

ObjectClass有以下字段:

private String hello;
private Map<String, String> params = new HashMap<String, String>();

推荐答案

你的问题的答案真的归结为为什么你想将 Map 传递给C 而不是在 Java 中迭代您的 Map 并将内容传递给 C.但是,我该质疑为什么?

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?

您问如何访问 HashMap(在您提供的代码中,Map)字段?在 Java 中为其编写访问器方法,并在传递容器 Object 时从 C 中调用该访问器方法.下面是一些简单的示例代码,展示了如何将 Map 从 Java 传递到 C,以及如何访问 Mapsize() 方法代码>.从中,您应该能够推断出如何调用其他方法.

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.

容器对象:

public class Container {

    private String hello;
    private Map<String, String> parameterMap = new HashMap<String, String>();

    public Map<String, String> getParameterMap() {
        return parameterMap;
    }
}

将容器传递给 JNI 的主类:

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);
}

相关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.
}

值得注意的是,我并不热衷于在方法本身中初始化 methodID 和类;this SO Answer 向您展示了如何缓存它们以供重复使用.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)