// Native C++ code that implements manipulateFields(...)
//
// Matthew Mead
// Monday, February 01, 1999 1:04 PM

#include <stdio.h>
#include "JavaFields.h"    

JNIEXPORT void JNICALL Java_JavaFields_manipulateFields(JNIEnv *env, jobject obj) 
{
  jclass cls;
  jfieldID fid_i, fid_pi, fid_si, fid_f;
  jint i, si, pi;
  jfloat f;

    // Get the class associated with this object (class JavaFields)
  cls = env->GetObjectClass(obj);

    // The the ID for variable 'i'
  fid_i = env->GetFieldID(cls, "i", "I");

    // Get the value of 'i'
  i = env->GetIntField(obj, fid_i);

    // The the ID for variable 'f'
  fid_f = env->GetFieldID(cls, "f", "F");

    // Get the value of 'f'
  f = env->GetFloatField(obj, fid_f);

    // The the ID for variable 'si'
  fid_si = env->GetStaticFieldID(cls, "si", "I");

    // Get the value of 'si'
  si = env->GetStaticIntField(cls, fid_si);
  
    // The the ID for variable 'pi'
  fid_pi = env->GetFieldID(cls, "pi", "I");

    // Get the value of 'pi' (No protection from native code!)
  pi = env->GetIntField(obj, fid_pi);

  printf("From native code:\n");
  printf("public int i = %i\n", i);
  printf("public float f = %f\n", f);
  printf("static public int si = %i\n", si);
  printf("private int pi = %i\n", pi);

    // Set the value of 'i'
  env->SetIntField(obj, fid_i, 100);

    // Set the value of 'f'
  env->SetFloatField(obj, fid_f, 123.45);

    // Set the value of 'si'
  env->SetStaticIntField(cls, fid_si, 200);

    // Set the value of 'pi' (No protection from native code!)
  env->SetIntField(obj, fid_pi, 300);
}
