Referencing existing SWIG wrappers when creating new ones
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_NxStream
NxStream
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
Asked by: Brad919 | Posted: 23-01-2022
Answer 1
Making the wrapper class explicitly inherit from the desired type did the trick in this case:
%typemap(javabase) UserStream "com.jphysx.SWIGTYPE_p_NxStream";
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 swigCPtr
fields. The one in the subtype is initialized, the one in the supertype remains 0
... 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 UserStream
object to a SWIGTYPE_p_NxStream
object:
%typemap(javacode) UserStream %{
public native com.JPhysX.SWIGTYPE_p_NxStream toNxStreamPtr();
%}
This JNI method was hand-written outside SWIG's stuff:
JNIEXPORT jobject JNICALL Java_physics_UserStream_toNxStreamPtr(JNIEnv *env, jobject userStreamObject) {
jclass userStreamClass = env->GetObjectClass(userStreamObject);
jmethodID getCPtrMethodID = env->GetStaticMethodID(userStreamClass, "getCPtr", "(Lphysics/UserStream;)J");
jlong cPtr = env->CallStaticLongMethod(userStreamClass, getCPtrMethodID, userStreamObject);
jboolean futureUse = false;
jclass nxStreamPtrClass = env->FindClass("com/JPhysX/SWIGTYPE_p_NxStream");
jmethodID nxStreamPtrConstructor = env->GetMethodID(nxStreamPtrClass, "<init>", "(JZ)V");
jobject nxStreamPtrObject = env->NewObject(nxStreamPtrClass, nxStreamPtrConstructor, cPtr, futureUse);
return nxStreamPtrObject;
}
Answered by: Adelaide935 | Posted: 24-02-2022
Similar questions
java - Referencing an external library in an Ant build
We have an ant build script for a project we're developing for a PDA. In eclipse we have a load of referenced libraries and I know how to get them to work when we run the jar on the PDA because we have a .lnk file where you can add the external libraries simply by adding the following:
512#"\J9\PPRO11\bin\j9.exe" -jcl:ppro11 -cp "\dist\WiFiTest.jar;\placelab\lib\placelab.jar" j2medemo.wifi.WiFiTest
...
xml - Problem referencing nodes directly when using XPath in Java
I've recently hit a wall with an issue with Java's native XML API (the W3C one) which at its core is that if I try to use a direct XPath expression in my document like, say, //body the XPath is evaluated as false (incorrect behaviour), however if I replace that with //*[1] it evaluates as true (desired behaviour).
I have checked numerous times and with different document...
In Java is there a performance difference between referencing a field through getter versus through a variable?
Is there any differences between doing
Field field = something.getSomethingElse().getField();
if (field == 0) {
//do something
}
somelist.add(field);
versus
if (something.getSomethingElse().getField() == 0) {
//do something
}
somelist.add(something.getSomethingElse().getField());
Do references to the field through getters incur a performanc...
java - Referencing Environment Variables in web.xml
I'm pre-packaging a JSP web-app that relies on some file path settings found within web.xml. These settings are unknown at packaging time, because they reference a path the customer will set when deploying the entire application (of which the web-app is a management interface).
It seems that the easiest way to avoid tokens and file modifications in my installer script, is to ask the user for an install location, s...
java - Referencing a cloned widgets in gwt
Right now i have my program to clone widgets. problem i am facing is i have to get the data from these widgets(particularly from textbox) on my DB end. But once i clone them i am not sure how do i keep trace on them and know which data goes where as these widgets don't have names(i mean the cloned ones)? Any help is appreciated.
Thank you.
java - referencing ant script location from within ant file
I have a utility build script that gets called from a variety of project-specific build scripts on a build server. Everything works fine, until the relative directory structure changes. That is:
trunk/
utilities/
imported.xml
some_resource_file
projectName/
importing.xml
works just fine, but sometimes we need:
trunk/
importing.xml
u...
sorting - referencing java objects on a sorted map by index?
I have a sorted map and want to reference its ordered objects by their index position. Is this possible? How would I convert a sorted map to an array list while maintaining the ordering, so I can retrieve an object by its index (order). Is this the only way to do it?
Ideally I could have this structure, and would know the index of an object within that strucre and could rertieve it by saying:
Ob...
Best practice for referencing Java inside my JSPs
I'm asking for some opinions on referencing Java code (in my case an enum value) in a JSP.
I currently have conditional logic in my JSP like:
<c:if test="${actionBean.order.status eq 'complete'}">
show some stuff
</c:if>
Now, 'complete' is a value associated with an enum in my codebase. Would it be better to reference the enum inside my JSP? If so, how?
...
java - referencing a directory from a different project in the properties file of another project
I am a Java novice
I am using Eclipse and I have a project (say Main Project) which has in its build path another project (let's call it Second Project).
I need to write in the directories section of the properties file of the Main Project the address of certain files which are part of the Second Project (and as result stored in the Second directory).
Considering the string properties.dir = ...
java - Self referencing symmetrical Hibernate Map Table using @ManyToMany
I have the following class
public class ElementBean {
private String link;
private Set<ElementBean> connections;
}
I need to create a map table where elements are mapped to each other in a many-to-many symmetrical relationship.
@ManyToMany(targetEntity=ElementBean.class)
@JoinTable(
name="element_elements",
joinColumns=@JoinColumn(name="FROM_ELEMENT_ID", nu...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)