Question

So I have a c++ class which has a lot of functions and they are nested calls within each function(Look at the sample below).

 void function1(){

      function2();
      function3();

 }

 void function2(){

      //Some implementation of function 2 here

 }


 void function2(){

      //Some implementation of function 3 here

 }

I now want to write a JNI wrapper to call function1, my question is whether I will have to write JNI wrappers for all the 3 functions or would it be enough if I write JNI for function1?

Also, if I do have to write JNI wrappers for all the functions, is there anyway the function1 can call function2 & function3 ? Because after modifying the name of the functions according to JNI, how do I call them?

Example of my JNI :

    JNIEXPORT void JNICALL Java_com_example_MainActivity_function1(){

              //How do I call other functions here?
    }

Any pointers would be appreciated.

Edit :

C++ class as requested :

 void Controller::initialize(InitData Data)
 {
// Create all managers

Log.d("Log","came in logd");
readConfig(Data);
createManagers();

// Create UserInterface

createUserInterface();

// Initialize all managers

initializeManagers(); 

// Initialize all modules

initializeModules(Data);

// Initialize loggers



  m_sessionModule->waitForSessionResponse();

 }

The rest of the functions being called are defined in the same class

Was it helpful?

Solution

  1. For calling the method of the C++ class in the JNI, the object of this class must be created already(if it's not a static method).
  2. You need to create the JNI wrappers only for the methods that can be called from the Java code.

Example:

C++ class:

class Controller
{
public:
  void function1()
  {
    function2();
    function3();
  }

  void function2(){}
  void function3(){}
}

JNI methods:

JNIEXPORT void JNICALL Java_com_example_MainActivity_function1()
{
  //getControllerInstance() must to return the already created 
  //instance of the class.
  Controller* controller = getControllerInstance();

  if(controller != nullptr)
    controller->function1();
}

//If you want to call function2() method from Java.
JNIEXPORT void JNICALL Java_com_example_MainActivity_function2()
{
  //getControllerInstance() must to return the already created 
  //instance of the class.
  Controller* controller = getControllerInstance();

  if(controller != nullptr)
    controller->function2();
}

P.S. The above code is an example, it is not working code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top