Sunday, April 21, 2013

jni jnienv SetStaticObjectField example c c++ java


SetStatic<type>Field Routines - SetStaticObjectField

void SetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID, NativeType value);

This family of accessor routines sets the value of a static field of an object. The field to access is specified by a field ID, which is obtained by callingGetStaticFieldID(). (SetStaticObjectField)

The following table describes the set routine name and value types. You should replace type in SetStatic<type>Field with the Java type of the field, or one of the actual set static field routine names from the table, and replace NativeType with the corresponding native type for that routine.
Table 4-6a SetStatic<type>Field Family of Accessor Routines

SetStatic<type>Field Routine Name

NativeType

SetStaticObjectField()

jobject

SetStaticBooleanField()

jboolean

SetStaticByteField()

jbyte

SetStaticCharField()

jchar

SetStaticShortField()

jshort

SetStaticIntField()

jint

SetStaticLongField()

jlong

SetStaticFloatField()

jfloat

SetStaticDoubleField()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-6b SetStatic<type>Field Family of Accessor Routines
SetStatic<type>Field Routine Name
Index
SetStaticObjectField()
154
SetStaticBooleanField()
155
SetStaticByteField()
156
SetStaticCharField()
157
SetStaticShortField()
158
SetStaticIntField()
159
SetStaticLongField()
160
SetStaticFloatField()
161
SetStaticDoubleField()
162
PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

fieldID: a static field ID.

value: the new value of the field.

Example of SetStaticObjectField






JNIEXPORT void JNICALL Java_JniExample_org_1native_1call (JNIEnv *env, jclass clazz, jobject obj) {
// Lookup the integer field ('intField') in 'obj' and get its value
 jfieldID intFieldId = env->GetFieldID(clazz, "intField", "I");
 if (id == 0) throw JNIException("Field not found");
 int intFieldVal = env->GetIntField(obj, intFieldId);
 // Lookup the static String field ('stringField') in 'obj', then convert the Java string representation
 // to C++ type 'const char *'.
 jfieldID strFieldId = env->GetStaticFieldID(clazz, "stringField", "Ljava/lang/String;");
 if (id == 0) throw JNIException("Field not found");
 jstring jstr = (jstring) env->GetStaticObjectField(clazz, strFieldId);
 const char *str = (jstr == 0) ? 0 : env->GetStringUTFChars(jstr, 0);
 // Lookup the integer array field ('intArray') in 'obj', then convert it to C++ type 'jint *'.
 jfieldID arrFieldId = env->GetFieldID(clazz, "intArray", "[I");
 if (id == 0) throw JNIException("Field not found");
 jintArray jarr = (jintArray) env->GetObjectField(obj, arrFieldId);
 jint *arr = env->GetIntArrayElements(jarr, 0);
 // Set new values
 env->SetIntField(obj, intFieldId, 0);
 arr[0] = 0; arr[1] = 0;
 env->SetStaticObjectField(clazz, strFieldId, env->NewStringUTF("Good-bye, world!"));
 // Explicitly release resources
 env->ReleaseIntArrayElements(jarr, arr, 0);
 env->ReleaseStringUTFChars(jstr, str);
}