Saturday, April 20, 2013

jni jnienv NewObjectV example c c++ java



NewObjectV


jobject NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);

Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void (V) as the return type.

The clazz argument must not refer to an array class.

NewObjectV

Programmers place all arguments that are to be passed to the constructor in an args argument of type va_list that immediately follows the methodID argument. NewObjectV() accepts these arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.
LINKAGE:
Index 29 in the JNIEnv interface function table.
PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: the method ID of the constructor.

Additional Parameter for NewObjectV:

args: a va_list of arguments to the constructor.

RETURNS:

Returns a Java object, or NULL if the object cannot be constructed.

THROWS:

InstantiationException: if the class is an interface or an abstract class.

OutOfMemoryError: if the system runs out of memory.

Any exceptions thrown by the constructor.

Example - NewObjectV


jobject cbgp_jni_new(JNIEnv * jEnv,
       const char * pcClass,
       const char * pcConstr,
       ...)
{
  va_list ap;
  jclass jcObject;
  jmethodID jmObject;
  jobject joNew;

  va_start(ap, pcConstr);

  /* Get the object's class */
  if ((jcObject= (*jEnv)->FindClass(jEnv, pcClass)) == NULL)
    jni_abort(jEnv, "Could not find class \"%s\"", pcClass);

  /* Get the constructor's method ID */
  if ((jmObject= (*jEnv)->GetMethodID(jEnv, jcObject,
          "<init>", pcConstr)) == NULL)
    jni_abort(jEnv, "Could not get method ID \"%s\" in class \"%s\"",
       pcConstr, pcClass);

  /* Build new object... */
  joNew= (*jEnv)->NewObjectV(jEnv, jcObject, jmObject, ap);
  if (joNew == NULL)
    jni_abort(jEnv, "Could not instantiate \"%s\"", pcClass);

  return joNew;
}