Saturday, April 20, 2013

jni jnienv GetMethodID example c c++ java


GetMethodID


jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);

Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the clazz’s superclasses and inherited by clazz. The method is determined by its name and signature.

GetMethodID() causes an uninitialized class to be initialized.

To obtain the method ID of a constructor, supply <init> as the method name and void (V) as the return type.
LINKAGE:
Index 33 in the JNIEnv interface function table.
PARAMETERS of GetMethodID

env: the JNI interface pointer.

clazz: a Java class object.

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

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

RETURNS of GetMethodID

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

THROWS:

NoSuchMethodError: if the specified method cannot be found.

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

OutOfMemoryError: if the system runs out of memory.

Example - GetMethodID

JNIEXPORT jint JNICALL 
Java_COM_novell_nsi_libsWrapper_BinderyJNI_NWScanObject  /* modified for example */
(
   JNIEnv   *env,
   jclass   obj, 
   jobject  objName,           // out, optional, StringBuffer
)
{
   nstr8 cObjectName [MAX_BINDERY_OBJECT_NAME_LEN];

   jstring javaString;   
   jclass cls;
   jmethodID mid;   
         
   .
   .
   .
   Some code to get cObjectName
         
   javaString = (jstring)(*env)->NewStringUTF(env, (const char *)cObjectName);

   cls = (*env)->GetObjectClass(env, objName);
   mid = (*env)->GetMethodID(env, cls, "append","(Ljava/lang/String;)Ljava/lang/StringBuffer;");
   if (0 == mid)
   {
      printf("GetMethodID returned 0\n");
      return(-1);
   }
   (*env)->CallObjectMethod(env, objName, mid, javaString);