This is less a problem with Java and JNI and more a problem of how to call a var args function in C with a dynamic list of arguments.
See Calling a C function with a varargs argument dynamically which suggests having two versions of the var args function (although I think the convention is more to allow passthrough of an existing va_list
, rather than for constructing one (which seems to be quite involved)).
The JNI bit should be just to define a Java native method with object array arguments which will have a C equivalent receiving the array. Use the JNI API to convert the values into the C equivalents (ints and ANSI strings) then load them into the var args structure and call your vadd_row()
function.
Java:
package mypackage;
public class MyClass {
...
public native void addRow(Object[] args);
...
}
C:
void vadd_row(int arg1, int arg2, va_list argp) {
... your function ...
}
void add_row(int arg1, int arg2, ...) {
va_list argp;
va_start(argp, arg2);
vadd_row(int arg1, int arg2, argp);
va_end(argp);
}
JNIEXPORT void JNICALL mypackage_MyClass_addRow(JNIEnv *env, jobject this, jint arg1, jint arg2, jobjectArray jarg_array) {
va_list argp;
/* need to construct argp, see link below for hints[1]; go through each element
of the java array, get the object; convert to primitive value or ANSI string,
then encode it into the va_list */
vadd_row((int)arg1, (int)arg2, argp);
}
[1] https://bbs.archlinux.org/viewtopic.php?pid=238721
Is it worth the hassle?
Consider just writing a simpler C function that receives the arguments in an array, then create a wrapper that uses var args if needed.