Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer to your question really boils down to <strong>why</strong> you'd want to pass a <code>Map</code> to C rather than iterate your <code>Map</code> in Java and pass the contents to C. But, who am I to question why? </p> <p>You ask how to access the <code>HashMap</code> (in your provided code, <code>Map</code>) field? Write an accessor method for it in Java and call that accessor method from C when you pass the container <code>Object</code>. Below is some bare-bones sample code showing how to pass a <code>Map</code> from Java to C, and how to access the <code>size()</code> method of the <code>Map</code>. From it, you should be able to extrapolate how to call other methods.</p> <p>Container Object:</p> <pre class="lang-java prettyprint-override"><code>public class Container { private String hello; private Map&lt;String, String&gt; parameterMap = new HashMap&lt;String, String&gt;(); public Map&lt;String, String&gt; getParameterMap() { return parameterMap; } } </code></pre> <p>Master Class which passes a Container to JNI:</p> <pre class="lang-java prettyprint-override"><code>public class MyClazz { public doProcess() { Container container = new Container(); container.getParameterMap().put("foo","bar"); manipulateMap(container); } public native void manipulateMap(Container container); } </code></pre> <p>Relevant C function:</p> <pre class="lang-c prettyprint-override"><code>JNIEXPORT jint JNICALL Java_MyClazz_manipulateMap(JNIEnv *env, jobject selfReference, jobject jContainer) { // initialize the Container class jclass c_Container = (*env)-&gt;GetObjectClass(env, jContainer); // initialize the Get Parameter Map method of the Container class jmethodID m_GetParameterMap = (*env)-&gt;GetMethodID(env, c_Container, "getParameterMap", "()Ljava/util/Map;"); // call said method to store the parameter map in jParameterMap jobject jParameterMap = (*env)-&gt;CallObjectMethod(env, jContainer, m_GetParameterMap); // initialize the Map interface jclass c_Map = env-&gt;FindClass("java/util/Map"); // initialize the Get Size method of Map jmethodID m_GetSize = (*env)-&gt;GetMethodID(env, c_Map, "size", "()I"); // Get the Size and store it in jSize; the value of jSize should be 1 int jSize = (*env)-&gt;CallIntMethod(env, jParameterMap, m_GetSize); // define other methods you need here. } </code></pre> <p>Of note, I'm not crazy about initializing methodIDs and classes in the method itself; <a href="https://stackoverflow.com/questions/10617735/in-jni-how-do-i-cache-the-class-methodid-and-fieldids-per-ibms-performance-r/13940735#13940735">this SO Answer</a> shows you how to cache them for re-use.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload