Source of UseObjImpl.cpp


  1: //: appendixb:UseObjImpl.cpp
  2: //# Tested with VC++ & BC++. Include path must 
  3: //# be adjusted to find the JNI headers. See 
  4: //# the makefile for this chapter (in the 
  5: //# downloadable source code) for an example.
  6: #include <jni.h>
  7: extern "C" JNIEXPORT void JNICALL
  8: Java_UseObjects_changeObject(
  9: JNIEnv* env, jobject, jobject obj) {
 10:   jclass cls = env->GetObjectClass(obj);
 11:   jfieldID fid = env->GetFieldID(
 12:     cls, "aValue", "I");
 13:   jmethodID mid = env->GetMethodID(
 14:     cls, "divByTwo", "()V");
 15:   int value = env->GetIntField(obj, fid);
 16:   printf("Native: %d\n", value);
 17:   env->SetIntField(obj, fid, 6);
 18:   env->CallVoidMethod(obj, mid);
 19:   value = env->GetIntField(obj, fid);
 20:   printf("Native: %d\n", value);
 21: } ///:~