I have an existing library (JPhysX) that is a Java wrapper for a native C++ library (PhysX). The Java library makes use of types generated by SWIG, for example, , which represents a pointer to an com.jphysx.SWIGTYPE_p_NxStreamNxStream object in the C++ code. Now I want to create my own C++ class that inherits from the C++ type NxStream, and have the Java wrapper for my class also inherit from the wrapper .com.jphysx.SWIGTYPE_p_NxStream
The problem is that when I call SWIG to generate the wrapper for my class, it also creates a new wrapper called , which is functionally identical to the one in SWIGTYPE_p_NxStream, but still a different type as far as Java is concerned.com.jphysx
How can I convince SWIG to reuse this existing wrapper from com.jphysx and make the wrapper of my class inherit from instead?com.jphysx.SWIGTYPE_p_NxStream
Making the wrapper class explicitly inherit from the desired type did the trick in this case:
There were some methods in the wrapper class with which I had similar problems, but I simply removed them from the SWIG interface file because they aren’t going to be called from the Java code anyway.
Edit: this does not work. Since the wrapper type inherits from another wrapper type, it suddenly has two
swigCPtrfields. The one in the subtype is initialized, the one in the supertype remains0… but this is the one that gets used when you use the supertype somewhere.Edit 2: I finally solved the problem, by adding a method to the Java wrapper class to convert the
UserStreamobject to aSWIGTYPE_p_NxStreamobject:This JNI method was hand-written outside SWIG’s stuff: