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

  • Home
  • SEARCH
  • 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 6679377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:21:08+00:00 2026-05-26T04:21:08+00:00

I am currently working with JNI (Java Native Interface) to send data between Java

  • 0

I am currently working with JNI (Java Native Interface) to send data between Java and C++. After implementing a little of code I realized the code for each method was always similar. An example could be:

JNIEXPORT void JNICALL Java_com_trial_jni_Receiver_setData__II(JNIEnv * env, jobject thiz, jint nativeObject, jint value)  
{  
    reinterpret_cast<Receiver *>(nativeObject)->setData(value);  
}  

JNIEXPORT void JNICALL Java_com_trial_jni_Receiver_setData__ILjava_lang_String_2(JNIEnv *env, jobject thiz, jint nativeObject, jstring value)  
{  
    reinterpret_cast<Receiver *>(nativeObject)->setData(value);  
}

Due to all the code has a similar structure, I decided generate a set of macros to generate automatically all this code. Thanks to Gregory Pakosz’ answer in this link Is it possible to iterate over arguments in variadic macros? I am now able to verify using the preprocessor how many parameters I introduced in a macro and process every single param.

But from the previous example there is a thing I am unable to achive that I would like to. Suppose I have this method inside a macro called JNI_METHOD. I would like to do something like this:

#define JNI_METHOD(package,clazz,method,...) \  
    JNIEXPORT void JNICALL Java_ ##package## _ ##clazz## _ ##method##__II(JNIEnv * env, jobject thiz, jint nativeObject, SET_DECLARATION_PARAMS(__VA_ARGS__ )) \  
    { \  
        reinterpret_cast<clazz *>(nativeObject)->method(SET_DECLARED_PARAMS(__VA_ARGS__)); \  
    }  

JNI_METHOD(com_trial_jni,Receiver,setData,jint);  
JNI_METHOD(com_trial_jni,Receiver,setData,jstring);  

In order to avoid having this question too long I didn’t paste the declaration of SET_DECLARATION_PARAMS and SET_DECLARED_PARAMS but the first one will result in something like ‘jint arg1’ and the second in ‘arg1’ without the type.

The question is: is there any way to generate a macro returning ‘I’ for ‘jint’ or ‘Ljava_lang_String_2’ for ‘jstring’. Note that the stringification cannot be used and this is needed to have ‘ILjava_lang_String_2’ instead of ‘II’ in the second generated method name.

Thanks!

  • 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-26T04:21:09+00:00Added an answer on May 26, 2026 at 4:21 am

    Well, the link you provided pretty much gives you the solution you need. Consider this:

    #define CONCATENATE(arg1, arg2)   CONCATENATE1(arg1, arg2)
    #define CONCATENATE1(arg1, arg2)  CONCATENATE2(arg1, arg2)
    #define CONCATENATE2(arg1, arg2)  arg1##arg2
    
    #define JNI_TRANSLATE_TYPE_jint I
    #define JNI_TRANSLATE_TYPE_jstring Ljava_lang_String_2
    
    #define JNI_TRANSLATE_TYPE(T) CONCATENATE(JNI_TRANSLATE_TYPE_, T)
    

    Test, on VS2010:

    #define STRINGIZE(arg)  STRINGIZE1(arg)
    #define STRINGIZE1(arg) STRINGIZE2(arg)
    #define STRINGIZE2(arg) #arg
    
    #pragma message("jint: " STRINGIZE(JNI_TRANSLATE_TYPE(jint)))
    #pragma message("jstring: " STRINGIZE(JNI_TRANSLATE_TYPE(jstring)))
    

    Output:

    1>  jint: I
    1>  jstring: Ljava_lang_String_2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently working with an API which requires we send our collection details in
Currently working in Java, i'd like to be able to select part of an
I am currently working with PHP code that random selects colors: <div onclick=location.href='<?php the_permalink()
I am currently working on my own little project, but I have a little
I'm working on a cross platform application in Java which currently works nicely on
Currently working on a project in MVC-3. Trying to put the following code in
Where currently working on an C# application that requires data to be stored in
Currently I am working with the Android NDK and JNI. I am trying to
I am currently working on a Java EJB project being deployed to Weblogic 10.3.3.
Currently working in the deployment of an OFBiz based ERP, we've come to the

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.