Saturday, April 20, 2013

jni jnienv GetCharField example c c++ java


Get<type>Field Routines - GetCharField


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(). (GetCharField)

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 - GetCharField


  1. static
    jboolean
  2. getKeyData(JNIEnv *env, jobject clazz, jint ptr, jint keycode, jobject keydata)
  3. {
  4.     jboolean rv;

  5.     unsigned short displayLabel = env->GetCharField(keydata, gKeyDataDisplayLabelField);
  6.     unsigned short number = env->GetCharField(keydata, gKeyDataNumberField);

  7.     jcharArray chars = (jcharArray) env->GetObjectField(keydata, gKeyDataMetaField);
  8.     jchar* ch = env->GetCharArrayElements(chars, NULL);

  9.     KeyCharacterMap* kmap = reinterpret_cast<KeyCharacterMap*>(ptr);
  10.     rv = kmap->getKeyData(keycode, &displayLabel, &number, ch);

  11.     env->SetCharField(keydata, gKeyDataDisplayLabelField, displayLabel);
  12.     env->SetCharField(keydata, gKeyDataNumberField, number);

  13.     env->ReleaseCharArrayElements(chars, ch, 0);
  14.     return rv;
  15. }