fusesource / hawtjni

A JNI code generator based on the JNI generator used by the eclipse SWT project
http://fusesource.github.io/hawtjni/
Apache License 2.0
165 stars 61 forks source link

How to get 'long' point of object o call c++ class/object binding #74

Closed chaorunrun closed 3 years ago

chaorunrun commented 3 years ago

This may be stupid but I am so confused how to call a native method with 'long' pointer to the object under neath.

For example, the c++ binding code could be

ABC.java
public class ABC {
    @JniMethod(flags={MethodFlag.CPP_METHOD)
    public static final native void Read(long self, @JniArg(cast="uint8_t*") byte[] buffer, int length);
...
}

in another caller .java file the c++ binding is called by

ABC abc = new ABC();
abc.Read(buffer, length);

And the compile will report error on missing argument 'long' self. Is there a common way to get the 'long' pointer to a specific object(make a method return this)?

chaorunrun commented 3 years ago

hmmmmm I was really stupid. The native object address was actually returned by MethodFlag.CPP_NEW marked method. It means that

public class ABCManager {
    public static class ABC {
        @JniMethod(flags={MethodFlag.CPP_NEW}
        public static final native long ABC();
    }
}

long pointer_to_obj = ABC.ABC();

will do things right.