java - Handling association with JNI -


i have question regarding how correctly handle association (or depedency) in jni.

lets assume in shared library have 2 classes, nativeclass1 , nativeclass2. nativeclass1 has method void foonative(nativeclass2* nativeobj) allows perform operation object of type nativeclass2.

for each of these classes, java class defined wrap corresponding native object (javaclass1 , javaclass2, each 1 having long private member pointing dynamically allocated native object of type nativeclass1and nativeclass2, respectively).

i javaclass1 have method public void foojava(javaclass2 obj) (and corresponding native method private native void call_foonative(long nativeobject1ptr, long nativeobject2ptr) call nativeclass1::void foonative(nativeclass2* nativeobj) after casting pointers).

how underlying long pointer (to nativeclass2) member javaclass2 in order call void javaclass1::call_foonative(long nativeobject1ptr, long nativeobject2ptr) ( assuming pass pointer of nativeclass1 first parameter)?

i thought of 2 methods:

  1. creating public getter method long pointer javaclass2.

but have access actual native object, create shared library, perform delete ptr on void pointer of nativeclass2 or damage native ojbect in other way.

  1. instead of passing pointer nativeclass2 object (as second parameter call_foonative(...), pass actual java object of type javaclass2 , determine pointer getfieldid , getlongfield (which permited on private member, stated in java native interface: programmer's guide , specification sheng liang, "10.9 violating access control rules".

which 1 correct way in terms of design , security?

how underlying long pointer (to nativeclass2) member javaclass2 in order call void javaclass1::call_foonative(long nativeobject1ptr, long nativeobject2ptr) (assuming pass pointer of nativeclass1 first parameter)?

your first method can reasonable. it's method used swig.

swig open-source tool generates java wrappers c++ code. if you're wrapping lot of classes or methods, may want consider using it.

for example, here's code generated swig (with class name changed):

public class foo {   private long swigcptr;   protected boolean swigcmemown;    public foo(long cptr, boolean cmemoryown) {     swigcmemown = cmemoryown;     swigcptr = cptr;   }    public static long getcptr(foo obj) {     return (obj == null) ? 0 : obj.swigcptr;   }    ... 

but have access actual native object, create shared library, perform delete ptr on void pointer of nativeclass2 or damage native object in other way.

not -- java , native code has access reference particular instance of nativeclass2.

  1. instead of passing pointer nativeclass2 object (as second parameter call_foonative(...), pass actual java object of type javaclass2 , determine pointer getfieldid , getlongfield ...

this won't prevent access pointer java. java code can use reflection access private fields.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -