How the JVM knows the class of an object at runtime

How jvm knows which class is an instance of an object at runtime. I know that we can use the getClass method to get the class name, but how does the getClass method work? Thank you, Praveen.

+4
source share
4 answers

Answer? Magic!

No, seriously, this implementation is defined, and to implement the JVM you do not need to use one simple method, for example, store the class as a reference in a field with a constant offset in the instance data. All you need to do is make sure that the getClass method works as documented. For example, in an escape analysis, the JVM can allocate an instance statically on the heap because it knows that the instance is not surviving the stack frame. In this case, he can choose to call getClass() to directly load the Class instance, and not to call the method at all! Similarly, since getClass() logically a virtual method (I know it's final, but its return value is constant in all instances of the class from any given class loader, but different for each individual class, as if it were virtual, which returned a constant value ), it may undergo a similar optimization of the built-in caching .

+5
source

I don’t know the specifics of the JVM, but in most object-oriented languages ​​+ runtime systems (Delphi, C ++ ,. NET), a hidden field is added to the data of the object instance, which indicates information like the instance of the object,

You should be careful with this when comparing an instance of an object directly with an external data structure, because a hidden type pointer or a table pointer of virtual methods in the object instance data throws out alignment with the fields in the external data structure.

You can usually see this artifact by looking at the size of the object instance. Sizeof () or equivalent. An object without declared fields will still have a memory size greater than zero. Where the magic of type information comes from.

+2
source

As in previous answers, implementation is not specified.

To get an idea of ​​what the implementation might look like, I looked at the runtime portion of the newly created JVM Hotspot. In Hotspot, each object begins with a tag word (for GC and other purposes) and a klass pointer. If you call getClass, then the native implementation in Object.c is called:

 JNIEXPORT jclass JNICALL Java_java_lang_Object_getClass(JNIEnv *env, jobject this) { if (this == NULL) { JNU_ThrowNullPointerException(env, NULL); return 0; } else { return (*env)->GetObjectClass(env, this); } } 

GetObjectClass is part of the JNI API. ( http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html ) JNI's internal implementation of GetObjectClass really just solves the object pointer, reads the class from the class pointer, and returns a Java view of this class:

 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj)) JNIWrapper("GetObjectClass"); HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj); Klass* k = JNIHandles::resolve_non_null(obj)->klass(); jclass ret = (jclass) JNIHandles::make_local(env, k->java_mirror()); HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret); return ret; JNI_END 
0
source

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


All Articles