How to clearly distinguish an instance of a Java object

I am currently creating my own JVMTI agent for Java 1.7. The problem is that I need to index some data about specific instances of Java objects. So my question is: can I use a value of type jobject as an identifier for an instance of an object to get my indexed data?

I am looking for any information about job type semantics. Is this a pointer to the memory location of the object? Is this the address of the stack pointer? Is this the address of the internal structure of the JVM? Thus, I cannot understand if the value of jobject is unique and unchanged throughout the life of a Java object.

Thanks for your help.

change

According to the JNI specifications found here , a jobject is represented as a pointer to an instance of an object.

+6
source share
2 answers

When you say that you are “job type value”, I assume that you mean the value returned by toString . If you look at the java document, it reports that:

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character, and the hexadecimal representation of the object’s hash code. In other words, this method returns a string equal to the value:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

And if you look at the Java document for the hashCode method, it states:

Whenever it is called by the same object more than once during the execution of a Java application, the hashCode method must consistently return the same integer

and

As reasonably practical, the hashCode method defined by the Object class returns different integers for different objects. (This is usually done by converting the internal address of the object to an integer, but this implementation method is not required by the JavaTM programming language.)

Update: Response to Ryan's comment: System.identityHashCode will provide you with the source hash code, even if the hashcode method is overridden. However, like notes, a note is not unique.

So, I think the answer to your questions is yes, its immutable and very likely to be unique, but you must read the documents or source code for your JVM.

+2
source

At least in HotSpot, a jobject really is a pointer to the location of the object, that is, dereferencing it, it will give you a unique address for each object, which is half the "unique and unchanging personality" that you are asking for. The problem is that the address may change during garbage collection, as HotSpot can move objects around.

The JVMTI GetTag and SetTag functions internally use a hash table from the location of the object in the tag. HotSpot updates this hash table whenever objects are moved, something that you cannot easily replicate from the perspective of the JVMTI agent. Assigning your own unique identification values ​​using tags, as you say, you are already doing, perhaps the only way to go here.

+1
source

Source: https://habr.com/ru/post/910363/


All Articles