Sunday, April 21, 2013

jni jnienv GetStaticFieldID example c c++ java


GetStaticFieldID


jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);

Returns the field ID for a static field of a class. The field is specified by its name and signature. The GetStatic<type>Field and SetStatic<type>Fieldfamilies of accessor functions use field IDs to retrieve static fields.

GetStaticFieldID() causes an uninitialized class to be initialized.
LINKAGE:
Index 144 in the JNIEnv interface function table.
PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

name: the static field name in a 0-terminated modified UTF-8 string.

sig: the field signature in a 0-terminated modified UTF-8 string.

RETURNS of GetStaticFieldID

Returns a field ID, or NULL if the specified static field cannot be found.

THROWS:

NoSuchFieldError: if the specified static field cannot be found.

ExceptionInInitializerError: if the class initializer fails due to an exception.

OutOfMemoryError: if the system runs out of memory.

Example of GetStaticFieldID
JNIEXPORT void JNICALL
Java_Employee_raiseSalary(JNIEnv* env, jobject obj_this, jdouble
byPercent) {

jclass class_Employee;
jfieldID id_salary;
jdouble salary;
jstring field_name;
jstring 
field_type;
jint rc;

fprintf(stdout,"inside Employee.c .....\n");

fflush(stdout);

class_Employee = (*env)->GetObjectClass(env, obj_this);
if (class_Employee == 0) {
fprintf(stdout,"GetObjectClass failed .....\n");
fflush(stdout);
return;
}
fprintf(stdout,"got class_Employee .....\n");

fflush(stdout);

id_salary = (*env)->GetStaticFieldID(env, class_Employee,
"salary", "D");
if (id_salary == 0) {
fprintf(stdout,"GetStaticFieldID failed .....\n");
fflush(stdout);
return;
}