4

I'm writing an application where I have lot to do with JNI call and every-time I have to perform getter() call to access variable values. Instead is it possible to access Object reference of JNI object on Java Layer so can get updated variable value just by variable name (like obj.name instead obj.getName()).

I have check with this and this, but not getting way how to covert address to Object at java layer.

EDIT I wanted to access Obj this way at Java layer from JNI.

private native CustomObj getCPPCustomObjectPointer();

Any suggestion here.

Community
  • 1
  • 1
CoDe
  • 11,056
  • 14
  • 90
  • 197
  • 1
    You should be able to make a Java class with `native` methods for JNI access. Why don't you post the code you have right now so the community can take a look at it and figure out what the best way forward is? – kris larson Sep 29 '15 at 09:33
  • [This](http://elliotth.blogspot.in/2007/03/optimizing-jni-array-access.html) I followed to set object from Java-JNI but how to access from JNI-Java as an pointer Object so can access class-object variable by name. This is where I stuck. – CoDe Sep 29 '15 at 09:40
  • Please post your code. – kris larson Sep 29 '15 at 09:53
  • Kris, I don't know how to get object from JNI to Java, this is where I stuck. I have update what Im expecting from JNI layer. Please follow. – CoDe Sep 29 '15 at 10:02
  • This might be useful: http://stackoverflow.com/questions/10327773/android-jni-get-two-fields-from-java-class The Oracle Java documentation for accessing object fields is here: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp16536 – Andrew Henle Sep 29 '15 at 10:47
  • @AndrewHenle Thanks, but in my case I wanted to access object like class/struct access from JNI to Java layer. Any suggestion here! – CoDe Sep 29 '15 at 10:58
  • @Shubh Like this? http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/field.html – Andrew Henle Sep 29 '15 at 12:50
  • @AndrewHenle This is again to access Java layer variable to JNI but what about JNI-variable to Java layer. Am I missing anything here u wanted to explain! Also I wanted to access Class-structure object instead individual variable. – CoDe Sep 29 '15 at 13:08
  • If you want to map native objects to java objects, you need to use jna: https://stackoverflow.com/questions/30179833/how-do-i-call-c-c-code-from-android-using-jna – Denis Tulskiy Sep 29 '15 at 15:48
  • @DenisTulskiy That's one alternative. It isn't the only one. – user207421 Oct 01 '15 at 09:58
  • @EJP what are other option and fast as JNI at least as I have lot to do with JNI layer per second basis. – CoDe Oct 01 '15 at 10:05
  • @AndrewHenle Thanks, now I got your point. I am getting **JNI DETECTED ERROR IN APPLICATION: field operation on invalid global reference: 0xa** ..any time you faced similar problem? – CoDe Oct 01 '15 at 13:52
  • May be this video holds the answer https://www.youtube.com/watch?v=7MIUVo-sHcM&index=2&list=PL25m8eyiFUJjNGoObid-BG-bPh66DOEXH – Misgevolution Oct 05 '15 at 08:48

2 Answers2

1

Is it possible to access Object reference of JNI object on Java Layer?

Yes, you can. However you cannot use it for accessing its properties. You are only able to hold its address as a long value.

If you would like to do so, you should create your C++ objects in heap memory and return their addresses as long numbers.

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

In Java side you can save that address as a long number wherever you want. Since objects have been created in heap memory, they will remain valid between JNI calls.

Also, you have to pass them to later JNI calls as a long number and then you should cast them to MyClass * in your C++ side.

MyClass *obj = (MyClass *)thatLongNumber;
obj->someProperty; // Access its properties and methods via -> operator
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Thanks..about to hold address as long, I'm aware but as no use if can not access property. Can we have some pseudo code to access property via JNI-> Java -> JNI(access property). – CoDe Oct 06 '15 at 10:55
  • I couldn't understand what you mean by _"JNI-> Java -> JNI(access property)"_. If you would want to access properties/fields of a C++ object (which its address as a `long` number is already known at Java side), AFAIK, you cannot do this at _Java_ side but you can do at _C++_ side. – frogatto Oct 06 '15 at 12:01
  • I mean ..get address from JNI to JAVA and Now take same address from Java to JNI to read object property as pointer reference. I'm stuck how to create object via this 'long' address at JNI layer. – CoDe Oct 06 '15 at 13:15
  • @Shubh Simply by casting it. `MyClass *obj = (MyClass *)thatLongNumber;` – frogatto Oct 06 '15 at 13:24
0

You want to retain a reference to a C++ object from your Java side? you can't.

Those implementations (C/Java) for representing and accessing objects/primitives are completely different. That's why there's so much mambo jambo functions when you you cast from one data type to another.

Sean
  • 5,176
  • 2
  • 34
  • 50