Take the 2-minute tour ×

I have a c++ class and I use swig to wrap that to java. I want to extend or add a new member function to this class, but I also want to use my own jni function as this new member function or let my own jni function be called in this new member function. How can I do that in swig? I know %native, but it seems to only generate static method but I want it to generate object method.

share|improve this question
Do you really need to use a JNI function? %extend would normally be the way to do this. You can always write extra javacode that calls the static JNI function though. –  Flexo May 25 '12 at 17:22
The point is that I want my client to use my class. And call its member function which has to be using my own JNI function. %extend is not enough. How can I do that in swig? –  xichblueagle May 25 '12 at 17:25
Let me ask you a question: Do you need SWIG? With JavaCPP, we can easily pass Java references around to C++ member functions with the @Raw annotation. –  Samuel Audet Jun 2 '12 at 14:47

1 Answer 1

If you just want to stick an extra native method in one of the proxy classes you can do that with something like:

%module test;

%typemap(javacode) foo %{
  public void native HandRolled();

struct foo {
  void bar();

void test(foo& f);

Personally though if you've got lots of native code that you don't want to use with %native directly I'd be inclined to make them into separate classes, i.e. you have the SWIG generated parts and the manual, JNI parts as distinct items. You can then use typemaps to "massage" the pure JNI parts into the SWIG generated part as appropriate.

share|improve this answer

Your Answer


