SWIG Import a generated class from another module and package into the current class

I find it hard to get a SWIG typemap (javapackage) to work properly. I tried to make a simple version of the problem, and even this seems to fail.

foo.h:

#ifndef FOO_H #define FOO_H class Foo { public: Foo() {}; int doSomething() { return 1 }; }; #endif 

bar.h:

 #ifndef BAR_H #define BAR_H #include "foo.h" class Bar { public: Bar() {}; int doSomething(Foo foo) { return foo.doSomething(); }; }; #endif 

Foo.i

 %module FooMod %include "typemaps.i" %include "stdint.i" %{ #include "../header/foo.h" %} %include "../header/foo.h" 

Bar.i

 %module BarMod %import "Foo.i" %typemap("javapackage") Foo, Foo *, Foo & "com.me.t.foo"; %include "typemaps.i" %include "stdint.i" %{ #include "../header/bar.h" %} %include "../header/bar.h" 

By running them using the following commands:

 swig -c++ -java -package com.me.t.foo -outdir ../../src/com/me/t/foo -o ../src/Foo.cpp Foo.i swig -c++ -java -package com.me.t.bar -outdir ../../src/com/me/t/bar -o ../src/Bar.cpp Bar.i 

And I get this output:

 package com.me.t.bar; public class Bar { private long swigCPtr; protected boolean swigCMemOwn; protected Bar(long cPtr, boolean cMemoryOwn) { swigCMemOwn = cMemoryOwn; swigCPtr = cPtr; } protected static long getCPtr(Bar obj) { return (obj == null) ? 0 : obj.swigCPtr; } protected void finalize() { delete(); } public synchronized void delete() { if (swigCPtr != 0) { if (swigCMemOwn) { swigCMemOwn = false; BarModJNI.delete_Bar(swigCPtr); } swigCPtr = 0; } } public Bar() { this(BarModJNI.new_Bar(), true); } public int doSomething(Foo foo) { return BarModJNI.Bar_doSomething(swigCPtr, this, Foo.getCPtr(foo), foo); } } 

BarModJNI.java:

 package com.me.t.bar; public class BarModJNI { public final static native long new_Bar(); public final static native int Bar_doSomething(long jarg1, Bar jarg1_, long jarg2, Foo jarg2_); public final static native long Bar_getFoo(long jarg1, Bar jarg1_); public final static native void delete_Bar(long jarg1); } 

Files are created correctly, but note that there is no import statement , so Foo cannot be found from any of the Bar Java classes. This is a simple example, but just hardcoding the import statement is not an option for me, because the generated source files containing C JNI code may have the wrong location of the Foo class files.

This seems like a very simple and common problem, so I wonder if I missed something or if something is wrong.

Thanks for the help!

+4
source share
2 answers

Got the same problem and found the answer, so posted it to the community.

You need to make 3 changes.

  • Add import operations to the generated proxy class (Bar.java):

     // Bar.i %pragma(java) jniclassimports=%{ import com.me.t.foo.Foo; %} 
  • Add import operations to the generated JNI shell class (BarModJNI.java):

     // Bar.i %typemap(javaimports) Bar %{ import com.me.t.foo.Foo; %} 
  • Tell SWIG to make Foo.getCPtr public member variable, because the Bar class wants to access it:

     // Foo.i SWIG_JAVABODY_PROXY(public, public, SWIGTYPE) SWIG_JAVABODY_TYPEWRAPPER(public, public, public, SWIGTYPE) 

Link:

+4
source

Just a couple of comments on the answer provided by Zbigniew. I ran into the same problem described in this article. I would like to clarify two points.

Firstly, it seems to me that step 1 is to add imports to the JNI shell class (anyJNI.java), and step 2 is to add imports to a specific class.

Secondly, step 2 does not work for me, instead of %typemap(javaimports) <class> I had to use %typemap(javaimports) SWIGTYPE . Too bad that it adds import to all generated Java classes, and not just what you want.

Finally, I still have the problem that SWIG does not recognize the imported class when wrapping a certain type, and it still uses SWIGTYPE_<type> instead of the direct <type>

0
source

Source: https://habr.com/ru/post/1480260/


All Articles