I used SWIG for generating some native JNI function interface for Irrlicht C/C++ 3D engine, and I got a bunch of java proxy classes and an intermediate c/c++ files which implements the java native functions to glue the java proxy classes and Irrlicht C/C++ 3d engine.
All java proxy classes are generated within the package net.sf.jirr. And the generated java native methods are defined in the net.sf.jirr.JirrJNI class.
Since the name of the SWIG generated c/c++ jni functions doesn't comply with the default android jni function call convention, I need to register these java native method with the corresponding C/C++ jni function manually.
i.e. Take the generated java native method net.sf.jirr.JirrJNI.SColor_setRed for example: The generated java native method is defined as:
public final static native void SColor_setRed(long jarg1, SColor jarg1_, long jarg2);
The generated c/c++ jni function is defined as:
SWIGEXPORT void JNICALL Java_net_sf_jirr_JirrJNI_SColor_1setRed(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jlong jarg2)
As the c/c++ jni function doesn't comply with android c/c++ jni function call convention(in this case the generated c/c++ jni function name should be Java_net_sf_jirr_JirrJNI_SColor_setRed if it needs to be called without registering the native function within the JNI_OnLoad function.)
So, I'm trying to register the native methods manually which is like:
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
......
if (!registerNativeMethods(env, JNI_REG_CLASS, (JNINativeMethod*)JNI_METHODS, JNI_METHODS_COUNT)) {
return -1;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
return result;
}
where the JNI_REG_CLASS, JNI_METHODS, JNI_METHODS_COUNT are defined as:
#define SET_A_JNI_METHOD(name, signature, func_ptr) \
{ (char*)#name, (char*)signature, (void*)func_ptr }
const char* JNI_REG_CLASS = "net/sf/jirr/JirrJNI";
JNINativeMethod JNI_METHODS[] = { SET_A_JNI_METHOD(SColor_setRed, "(JLnet/sf/jirr/SColor;J)V", Java_net_sf_jirr_JirrJNI_SColor_1setRed), ......};
int JNI_METHODS_COUNT = sizeof(JNI_METHODS) / sizeof(JNI_METHODS[0]);
So, I need to parse the net.sf.jirr.JirrJNI class and generate a bunch of SET_A_JNI_METHOD(......) macroes for all of the java native methods.
In this SET_A_JNI_METHOD macro, the name part is just the java native function name; the signature part complies with the java jni method signature standards(which is explained in the official java jni JNINativeMethod reference); the func_ptr part is to add Java_$(PackageName)_ as the prefix add some index number to each part(excluding the class name part) of the java native method name(separated by "_" here). i.e:
Java native method: IFileSystem_getFileDir
name part: IFileSystem_getFileDir;
func_ptr part: Java_net_sf_jirr_JirrJNI_IFileSystem_1getFileDir;
Java native method: IFileSystem_addFolderFileArchive__SWIG_0
name part: IFileSystem_addFolderFileArchive__SWIG_0;
func_ptr part: Java_net_sf_jirr_JirrJNI_IFileSystem_1addFolderFileArchive_1_1SWIG_10
Any hint on how to get this done with Python regex?
Here is some gross solution for this problem. It just worked.