#include "javatech_jni22_JNIDemo.h" JNIEXPORT void JNICALL Java_javatech_jni22_JNIDemo_doStatic (JNIEnv *jenv, jclass cls) {} // doStatic JNIEXPORT void JNICALL Java_javatech_jni22_JNIDemo_doSomething (JNIEnv *jenv, jobject jo) { // Find the jclass corresponding to the jobject jclass cls = jenv->GetObjectClass (jo); // Get the static a_static_float field. jfieldID fid = jenv->GetStaticFieldID (cls, "a_static_float", "F"); jfloat a_static_float = jenv->GetStaticFloatField (cls, fid); printf ("got a_static_float = %f\n", a_static_float); // Find the field ID of the 'my_custom' field jfieldID my_custom_fid = jenv->GetFieldID ( cls, "my_custom", "Ljavatech/jni22/MyCustomObject;" ); if (my_custom_fid == NULL) return; printf ("got my_custom_fid\n"); // Get the jobject reference to 'my_custom' jobject my_custom_jo = jenv->GetObjectField (jo, my_custom_fid); printf ("got my_custom_jo\n"); // Find the jclass of the my_custom_jo jobject. jclass my_custom_cls = jenv->GetObjectClass (my_custom_jo); printf ("got my_custom_cls\n"); // Find the field ID of the 'val' field of MyCustomObject. jfieldID val_fid = jenv->GetFieldID (my_custom_cls, "val", "I"); if (val_fid == NULL) { jthrowable jth = jenv->ExceptionOccurred (); if (jth) { printf (" there was an exception\n"); jenv->ExceptionDescribe (); jenv->ExceptionClear (); printf (" described and cleared it\n"); } return; } printf ("got val_fid\n"); // Get the 'val' field jint val = jenv->GetIntField (my_custom_jo, val_fid); printf ("got val = %d\n", val); // Manipulate it somehow jint multiplier = (jint) a_static_float; val *= multiplier; printf ("multiplied it by %f\n", a_static_float); // Set it back into my_custom_jo jenv->SetIntField (my_custom_jo, val_fid, val); printf ("put it back into my_custom_jo\n"); // Get the method ID to call a Java method jmethodID mid = jenv->GetMethodID (cls, "callback", "(I)I"); if (mid == NULL) return; printf ("got mid\n"); jint param = 8; // Call the Java method printf ("calling Java 'callback' method\n"); jint ret = jenv->CallIntMethod (jo, mid, param); // Check for any exception that may have occurred during the Java method jthrowable jth = jenv->ExceptionOccurred (); if (jth) { printf (" there was an exception\n"); jenv->ExceptionDescribe (); jenv->ExceptionClear (); printf (" did not return from Java 'callback'\n"); } else printf (" return from 'callback' = %d\n", ret); } // doSomething