Access a JNI Object as a reference

I am writing an application where I have a lot in common with a JNI call, and each time I have to make a getter() call to access the values โ€‹โ€‹of the variables. Instead, you can access the JNI object reference to the Java Layer to get the updated value of the variable only by the variable name (for example, obj.name instead of obj.getName() ).

I checked with this and this , but did not get a way, like a hidden object address in a java layer.

EDIT I wanted to access Obj in this way at the Java level from JNI.

 private native CustomObj getCPPCustomObjectPointer(); 

Any suggestion here.

+4
java c ++ android pointers jni
Sep 29 '15 at 9:26
source share
2 answers

Is it possible to access the object link of a JNI object in Java Layer?

Yes, you can. However, you cannot use it to access its properties. You can save your address only as long .

If you want to do this, you must create your C ++ objects in heap memory and return them as long numbers.

 MyClass *obj = new MyClass(); return (long) obj; 

On the Java side, you can save this address as long wherever you want. Because objects were created in heap memory, they will remain valid between JNI calls.

In addition, you must pass them to later JNI calls as a long number, and then you must forward them to MyClass * on the C ++ side.

 MyClass *obj = (MyClass *)thatLongNumber; obj->someProperty; // Access its properties and methods via -> operator 
+1
Oct 06 '15 at 10:40
source share

Do you want to keep a reference to a C ++ object from the Java side ? you cannot .

These implementations (C / Java) for representing and accessing objects / primitives are completely different. This is why there are so many mambo jambo functions when you drop from one data type to another.

0
01 Oct '15 at 22:03
source share



All Articles