通过前面的分析,创建的insttanceKlass 都没放入了java_lang_Class::fixup_mirror_list()这里类的数组里面了,所有的instance列举如下
---------------------------------------------list->at(8) $3 = 0x7f6d6800f2b8 "java/lang/Object" (gdb) p _klass $4 = (InstanceKlass *) 0x100000f30 ---------------------------------------------list->at(9) $7 = 0x7f6d68014f58 "java/io/Serializable" (gdb) p _klass $8 = (InstanceKlass *) 0x100001120 ---------------------------------------------list->at(10) $9 = 0x7f6d68014fa8 "java/lang/Comparable" (gdb) p _klass $10 = (InstanceKlass *) 0x100001308 -----------------------------------------------list->at(11) (gdb) p name->as_C_string() $12 = 0x7f6d680156b8 "java/lang/CharSequence" (gdb) p _klass $13 = (InstanceKlass *) 0x1000014f0 -----------------------------------------------list->at(12) (gdb) p name->as_C_string() $15 = 0x7f6d68014d38 "java/lang/String" (gdb) p _klass $16 = (InstanceKlass *) 0x1000016d8 ----------------------------------------------- (gdb) p name->as_C_string() $17 = 0x7f6d6800ea68 "java/lang/Class" 加载一半就不执行了,不知道为什么 -----------------------------------------------list->at(13) $8 = 0x7ff06c0100a8 "java/lang/reflect/AnnotatedElement" (gdb) p _klass $9 = (InstanceKlass *) 0x100001948 -----------------------------------------------list->at(13) (gdb) p name->as_C_string() $11 = 0x7ff06c00ef28 "java/lang/reflect/GenericDeclaration" (gdb) p _klass $16 = (InstanceKlass *) 0x100001b30 -------------------------------------------预加载中创建的对象 (gdb) p $26.as_C_string() $27 = 0x7ff06c009c88 "java/lang/Class" (gdb) p k $21 = (InstanceMirrorKlass *) 0x100001f00 --------------------------------------------
最后的java/lang/Class是,不再list中
进入主流程
GrowableArray <Klass*>* list = java_lang_Class::fixup_mirror_list(); int list_length = list->length(); for (int i = 0; i < list_length; i++) { Klass* k = list->at(i); assert(k->is_klass(), "List should only hold classes"); EXCEPTION_MARK; KlassHandle kh(THREAD, k); java_lang_Class::fixup_mirror(kh, CATCH); }
说明 上述的list_length为17 , 前面从list->at(8)就是读取的Object,之前的是ArrayKlass类型的对象,现在读取的是k 为 $32 = (TypeArrayKlass *) 0x100000030
-->
java_lang_Class::fixup_mirror(KlassHandle k, TRAPS) {create_mirror(k, Handle(NULL), CHECK);}
进入
oop java_lang_Class::create_mirror(KlassHandle k, Handle protection_domain, TRAPS) { assert(k->java_mirror() == NULL, "should only assign mirror once"); int computed_modifiers = k->compute_modifier_flags(CHECK_0); k->set_modifier_flags(computed_modifiers); // Class_klass has to be loaded because it is used to allocate // the mirror. if (SystemDictionary::Class_klass_loaded()) { // Allocate mirror (java.lang.Class instance) Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK_0); InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass()); java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror())); // It might also have a component mirror. This mirror must already exist. if (k->oop_is_array()) { Handle comp_mirror; if (k->oop_is_typeArray()) { BasicType type = TypeArrayKlass::cast(k())->element_type(); comp_mirror = Universe::java_mirror(type); } else { assert(k->oop_is_objArray(), "Must be"); Klass* element_klass = ObjArrayKlass::cast(k())->element_klass(); assert(element_klass != NULL, "Must have an element klass"); comp_mirror = element_klass->java_mirror(); } assert(comp_mirror.not_null(), "must have a mirror"); // Two-way link between the array klass and its component mirror: ArrayKlass::cast(k())->set_component_mirror(comp_mirror()); set_array_klass(comp_mirror(), k()); } else { assert(k->oop_is_instance(), "Must be"); // Allocate a simple java object for a lock. // This needs to be a java object because during class initialization // it can be held across a java call. typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_NULL); set_init_lock(mirror(), r); // Set protection domain also set_protection_domain(mirror(), protection_domain()); // Initialize static fields InstanceKlass::cast(k())->do_local_static_fields(&initialize_static_field, CHECK_NULL); } return mirror(); } else { if (fixup_mirror_list() == NULL) { GrowableArray<Klass*>* list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true); set_fixup_mirror_list(list); } fixup_mirror_list()->push(k()); return NULL; } }
解析橘色标记的橘色就是创建mirror的内存空间,
InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK_0);
这个句子的主干是 Klass->allocat_instance(k,xxx),这个Klass就是名字为"java/lang/Class"的instanceKlass对象,这个对象的地址为0x100001f00,这个对象非常重要
那么解析过程如下
//解析宏如下 获取 SystemDictionary::Class_klass() static Klass* name() { return check_klass_##option(_well_known_klasses[Class_klass_knum]); } ---> static Klass* check_klass_Pre( Klass* k) { return check_klass(k); } -----> // Fast access to commonly used classes (preloaded) static Klass* check_klass(Klass* k) { assert(k != NULL, "preloaded klass not initialized"); return k; } (gdb) p k $21 = (InstanceMirrorKlass *) 0x100001f00
打印对象
(gdb) p *k $23 = (InstanceMirrorKlass) { <InstanceKlass> = { <Klass> = { <Metadata> = { <MetaspaceObj> = {<No data fields>}, members of Metadata: _vptr.Metadata = 0x7ff071f18a50 <vtable for InstanceMirrorKlass+16>, _valid = 0 }, members of Klass: _layout_helper = 97, _super_check_offset = 56, _name = 0x7ff070064108, _secondary_super_cache = 0x0, _secondary_supers = 0x7ff05ada9560, _primary_supers = {0x100000f30, 0x100001f00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, _java_mirror = 0x0, _super = 0x100000f30, _subklass = 0x0, _next_sibling = 0x100001d18, _next_link = 0x100001d18, _class_loader_data = 0x7ff06c02e868, _modifier_flags = 17, _access_flags = { _flags = 538968113 }, _last_biased_lock_bulk_revocation_time = 0, _prototype_header = 0x1, _biased_lock_revocation_count = 0, _modified_oops = 0 '