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 8424675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:03:54+00:00 2026-06-10T04:03:54+00:00

I found this question, but the one answer was basically, you wouldn’t want to

  • 0

I found this question, but the one answer was basically, you wouldn’t want to do that: Is it possible to add code to an existing method when using Swig to build a C# wrapper for C++ code?

I actually agree with it in the case as described, where the OP was trying to insert code in a way that could be fragile. In my case I am doing exactly what the answer suggested: renaming the method and using %typemap(javacode) to implement the wrapping method.

But I’ve written this as a macro, and I want to override several methods, so I end up calling %typecode(javacode) multiple times, and only the last wrapping method javacode typemap is active.

The details of the macro are complicated, since it uses variable args to express the signature.

But demonstrating the issue:

%define WRAP(CLASS,METHOD)
%rename(method ## _internal,fullname=1) CLASS::METHOD;
%typemap(javamethodmodifiers) CLASS::METHOD "private";
%typemap(javacode) {
    public void METHOD() {
       METHOD ## _internal();  // delegate to original
       // extra code here
    }
}   // (note: dont use %{ %} -- need macro evaluation)
%enddef

WRAP(Foo,bar)
WRAP(Foo,baz)

class Foo {
   void bar();
   void baz();
}

Only the public void baz() { baz_internal(); … } gets generated.
The problem here is that the %rename and %typemap(javamethodmodifiers) are unique since the scope to CLASS::METHOD, but the %typemap(javacode) applies to the whole class. If syntax like

 %typemap(javacode,append=true) {   // code   }

were supported, then this would solve the problem. Is there some technique that could accomplish this? This could make sense for typemaps like javacode or javaimports, whose defaults are empty.

  • 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-06-10T04:03:56+00:00Added an answer on June 10, 2026 at 4:03 am

    I can make a SWIG macro that generates the code you’re looking for. It’s a bit of a bodge, but it works. The trick was abusing the javaout (with noblock=1) typemap instead of javacode so that it gets applied once per function rather than once per class:

    %module test
    
    %define WRAP(CLASS,METHOD)
    %rename(METHOD ## _internal,fullname=1) CLASS::METHOD;
    %javamethodmodifiers CLASS::METHOD "private";
    %typemap(javaout,noblock=1) void CLASS::METHOD {
      {
        $jnicall;
      }
    
      public void METHOD() {
        METHOD ## _internal();
        // some other bits
      }
    }
    %enddef
    
    WRAP(Foo,bar)
    WRAP(Foo,baz)
    
    class Foo {
    public:
       void bar();
       void baz();
    };
    

    This generates exactly the code you’re looking for, but I suspect you could skip the %rename entirely and do it all from the javaout typemap. This approach works with SWIG 1.3 and upwards.

    I did try another approach with $typemap and copying typemaps around, but this didn’t work out in the end.

    If you want to support methods which return things too you’ll want to add a third macro argument:

    %define WRAP(CLASS,METHOD,RETURN)
    %rename(METHOD ## _internal,fullname=1) CLASS::METHOD;
    %javamethodmodifiers CLASS::METHOD "private";
    %typemap(javaout,noblock=1) RETURN CLASS::METHOD {
        $typemap(javaout,RETURN)
    
      public $typemap(jstype,RETURN) METHOD() {
        RETURN result = METHOD ## _internal();
        // some other bits
        return result;
      }
    }
    %enddef
    

    $typemap there is used to refer to the default, less specialised typemaps to avoid having to duplicate a lot of code for non-primitive/special case returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking for an existing answer to this question, but haven't found one.
I know that this question is repeated one. But no one could answer briefly
I found this question that is discussing what I would like to do, but
I found this answer to this question https://stackoverflow.com/a/7244888/1473523 , but in my situation am
Searching here I found that this question was already asked , but I think
I haven't found a clear answer to this question (but have determined the HTML
There's probably a Google search that'll answer this question but for the life of
Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
While searching for an answer to this question I found this sample code which
i've read several thread for this question but unfortunatly not found the answer to

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.