Given a java object A a = new A(), and a bunch of native instance methods, if I were to take the address of the jobject representing a passed into those methods, would the address always be the same?
I have multiple final fields in these classes that I want to store in a hashmap in my C code (so I don't have to continue fetching them with Get___Field), with the hash of a jobject being the address. If I can guarantee that the address of a passed in jobject representing a will always be the same, then the hash is deterministic, which means my program's behavior will not be inconsistent.
As comments have suggested, you cannot keep the passed-in
jobjectaround beyond the current JNI call context because it is a local reference. Thejobjectvalues may be reused (leading to duplicate table entries) or become invalid. Furthermore, each call toCreateGlobalRef()will create a new, distinctjobject. The short answer is "you cannot do it this way". Such has been already addressed in this post. As an alternative approach, note that every Java Object has ahashCode()andequals()method. You can call those methods from your C/C++ code using JNI, and use them in your hash table. However, without knowing the details of when and how you are fetching the field values, calling JNI methods may not be any better than fetching the field values again.Finally, as answered in this question, you can test
jobjectequality directly usingenv->IsSameObject(jobject1, jobject2). Presumably,jobject1is the global reference that you have created and stored, whereasjobject2is the passed-in local reference that you want to test against. Unfortunately this still doesn't help you probe into your hash table.