Saturday, April 20, 2013

jni jnienv GetObjectField example c c++ java


Get<type>Field Routines - GetObjectField


NativeType Get<type>Field(JNIEnv *env, jobject obj,
jfieldID fieldID);

This family of accessor routines returns the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID(). (GetObjectField)

The following table describes the Get<type>Field routine name and result type. You should replace type in Get<type>Field with the Java type of the field, or use one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-1a Get<type>Field Family of Accessor Routines

Get<type>Field Routine Name

Native Type

GetObjectField()

jobject

GetBooleanField()

jboolean

GetByteField()

jbyte

GetCharField()

jchar

GetShortField()

jshort

GetIntField()

jint

GetLongField()

jlong

GetFloatField()

jfloat

GetDoubleField()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-1b Get<type>Field Family of Accessor Routines

Get<type>Field Routine Name

Index

GetObjectField()
95

GetBooleanField()
96

GetByteField()
97

GetCharField()
98

GetShortField()
99

GetIntField()
100

GetLongField()
101

GetFloatField()
102

GetDoubleField()
103
PARAMETERS:

env: the JNI interface pointer.

obj: a Java object (must not be NULL).

fieldID: a valid field ID.

RETURNS:

Returns the content of the field.
Example - GetObjectField

JNIEXPORT jint JNICALL Java_org_mangler_VentriloInterface_getevent(JNIEnv* env, jobject obj, jobject eventdata) {
 v3_event *ev = v3_get_event(V3_BLOCK);
 if(ev != NULL) {
  jclass event_class = (*env)->GetObjectClass(env, eventdata);

  // Event type.
  jfieldID type_field = (*env)->GetFieldID(env, event_class, "type", "S");
  (*env)->SetShortField(
   env, 
   eventdata, 
   type_field, 
   1234
  );

  // Get PCM class.
  jfieldID pcm_field = (*env)->GetFieldID(env, event_class, "pcm", "Lorg/mangler/VentriloEventData$_pcm;");
  jobject pcm = 
  (*env)->GetObjectField(
   env, 
   eventdata, 
   pcm_field
  );
  jclass pcm_class = (*env)->GetObjectClass(env, pcm);

  // Set PCM fields.
  jfieldID pcm_length_field = (*env)->GetFieldID(env, pcm_class, "length", "I");
  (*env)->SetIntField(
   env,
   pcm,
   pcm_length_field,
   1337
  );

  free(ev);
 }
 return 0;
}