Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7402421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:44:14+00:00 2026-05-29T04:44:14+00:00

I used SWIG for generating some native JNI function interface for Irrlicht C/C++ 3D

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:44:16+00:00Added an answer on May 29, 2026 at 4:44 am

    Here is some gross solution for this problem. It just worked.

    #!/usr/bin/env python
    
    #import the necessary modules.
    import re #for regular expressions.
    import sys #for parsing command line opts.
    
    JNI_CPP_FUNC_PREFIX_INDEX = 1
    JNI_METHOD_SIGNATURE_MAP = {
    #basic type
    'void':'V',
    'boolean':'Z',
    'int':'I',
    'long':'J',
    'double':'D',
    'float':'F',
    'byte':'B',
    'char':'C',
    'short':'S',
    #array type
    'int[]':'[I',
    'float[]':'[F',
    'byte[]':'[B',
    'char[]':'[C',
    'short[]':'[S',
    'double[]':'[D',
    'long[]':'[J',
    'boolean[]':'[Z',
    }
    
    #param type = True; return type = False
    def generateJniMethodTypeSignature(type, paramORreturnType = True):
        retParamSignature = None
        if type and type > "":
        try:
            retParamSignature = JNI_METHOD_SIGNATURE_MAP[type]
        except:
            if type == "String" :
                retParamSignature = 'LJava/lang/String'
            else :
                retParamSignature = "Lnet/sf/jirr/" + type
            #if paramORreturnType :
            retParamSignature += ";"
    
        #print "retParamSignature: "
        #print retParamSignature
        return retParamSignature
    
    #if sys.argv[1]:
    #   print "File: %s" % (sys.argv[1])
    #   logfile = raw_input("Please input a log file to parse, e.g /var/file: ");
    try:
        jniMethodsFilePath = sys.argv[1]
        jniMethodsFile = open(jniMethodsFilePath, "r")
    
        outputFilePath = sys.argv[2]
        outputFile = open(outputFilePath, "w")
    
        for eachLine in jniMethodsFile.readlines() :
            eachLine = eachLine.strip()
            #print(eachLine)
        #                                               retType  name space (   param space  )
        regex = re.match('^public\sfinal\sstatic\snative\s(\w+)\s(\w+)(\s)*(\()([^)]*)(\s)*(\))', eachLine)
        if regex:
            #'''
            print "whole match: " + regex.group(0)  #whole match.
            '''
            print "retType: " + regex.group(1)      #retType
            print "name: " + regex.group(2)         #name
            print "left space: "
            print regex.group(3)                    #left space
            print "(: " + regex.group(4)            #(
            print "param: " + regex.group(5)        #param
            print "right space: "
            print regex.group(6)                    #right space
            print "): " + regex.group(7)            #)
            #print eachLine
            '''
            retType = regex.group(1).strip() 
            funcName = regex.group(2).strip()
            param = regex.group(5).strip()
            #java native function name
            command = "SET_A_JNI_METHOD(" + funcName + ", \"("
    
            #print "param: " + regex.group(5) 
            paramRegex = re.split('\s*,\s*', param)
            if paramRegex:
                for eachParam in paramRegex:
                    eachParam = eachParam.strip()
                    #print "eachParam: " + eachParam
                    eachParamRegex = re.split('\s*', eachParam)
                    if eachParamRegex:
                        eachParamType = eachParamRegex[0].strip()
                        #print "eachParamType: " + eachParamType
                        paramTypeSignature = generateJniMethodTypeSignature(eachParamType)
                        if paramTypeSignature:
                            #print "paramTypeSignature: " + paramTypeSignature
                            command = command + paramTypeSignature
                command = command + ")"
                retTypeSignature = generateJniMethodTypeSignature(retType, False)
                if retTypeSignature:
                    #parameter type signature.
                    command = command + retTypeSignature + "\", "
                    #print "command: " + command
                #c/c++ jni function name
                funcNameRegex = re.split('_', funcName)
                if funcNameRegex:
                    #print cppFuncNameRegex
                    i = 0
                    cppFuncName = "Java_net_sf_jirr_JirrJNI_"
    
                    for eachFuncNamePart in funcNameRegex:
                        #print eachFuncNamePart
                        i = i + 1
                        if i == 1:
                            cppFuncName = cppFuncName + eachFuncNamePart
                            if i != len(funcNameRegex):
                                cppFuncName = cppFuncName + '_'
                            continue
                        cppFuncName = cppFuncName + str(JNI_CPP_FUNC_PREFIX_INDEX) + eachFuncNamePart
                        if i != len(funcNameRegex) :
                            cppFuncName = cppFuncName + '_'
    
                    command = command + cppFuncName + "), "
                    print "output: " + command + "\n"
                    outputFile.write(command + '\n')
        outputFile.close()
        jniMethodsFile.close()          
    except IOError, (errno, strerror):
        print "I/O Error(%s) : %s" % (errno, strerror)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used SWIG to bind a set of classes to lua. I know
The software tool SWIG can be used to create a programming interface (bindings) to
I need to parse function headers from a .i file used by SWIG which
I used SWIG to wrap my c++ class. Some methods have a const std::string&
I used SWIG to generate a Perl module for a C++ program. I have
First, I have never used SWIG, I dont know what it does... We have
I just on internet the Google is using swig. Does any one have used
We're using some C library in our Java project. Several years ago some other
The USPS provides a C library and a Java wrapper for generating their new
Has anybody out there used the SWIG library with C#? If you have, what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.