Hotspot源码解析-第二十章-字典表创建和基础类预加载(四)

本文主要是介绍Hotspot源码解析-第二十章-字典表创建和基础类预加载(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

20.5 字典表创建和基础类预加载

20.5.1 systemDictionary.cpp/hpp

20.5.1.1 SystemDictionary::initialize
void SystemDictionary::initialize(TRAPS) {// Allocate arraysassert(dictionary() == NULL,"SystemDictionary should only be initialized once");_sdgeneration        = 0;// 创建字典(就是Hash Table)_dictionary          = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount));// 创建占位符表(就是Hash Table)_placeholders        = new PlaceholderTable(_nof_buckets);_number_of_modifications = 0;// 创建约束表(就是Hash Table)_loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);// 创建解析错误表(就是Hash Table)_resolution_errors   = new ResolutionErrorTable(_resolution_error_size);// 创建调用方法符号表(就是Hash Table)_invoke_method_table = new SymbolPropertyTable(_invoke_method_size);// 分配一个数组来存放系统类加载锁_system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);// 初始化基础类,细节继续看`章节20.5.1.2`initialize_preloaded_classes(CHECK);
#if INCLUDE_JFRjfr_event_handler_proxy = SymbolTable::new_permanent_symbol("jdk/jfr/proxy/internal/EventHandlerProxy", CHECK);
#endif // INCLUDE_JFR
}
20.5.1.2 initialize_preloaded_classes

这一部分也用到了很多宏定义,咱们先把宏展开后再来讲解

class SystemDictionary : AllStatic {friend class VMStructs;friend class SystemDictionaryHandles;public:enum WKID {NO_WKID = 0,#define WK_KLASS_ENUM(name, symbol, ignore_o) WK_KLASS_ENUM_NAME(name), WK_KLASS_ENUM_NAME(symbol) = WK_KLASS_ENUM_NAME(name),WK_KLASSES_DO(WK_KLASS_ENUM)#undef WK_KLASS_ENUMWKID_LIMIT,FIRST_WKID = NO_WKID + 1};
}
// ==============第1块宏:枚举=================
// 宏展开前
#define WK_KLASS_ENUM(name, symbol, ignore_o) WK_KLASS_ENUM_NAME(name), WK_KLASS_ENUM_NAME(symbol) = WK_KLASS_ENUM_NAME(name),WK_KLASSES_DO(WK_KLASS_ENUM)
// 宏展开后
// 第1步WK_KLASS_ENUM(String_klass,java_lang_String,Pre)WK_KLASS_ENUM_NAME(String_klass), WK_KLASS_ENUM_NAME(java_lang_String) = WK_KLASS_ENUM_NAME(String_klass),
// 第2步// 前置宏定义#define WK_KLASS_ENUM_NAME(kname)    kname##_knumString_klass_knum,java_lang_String_knum=String_klass_knum
// 最终 enum WKID 宏展开后enum WKID {NO_WKID = 0,Object_klass_knum,        java_lang_Object_knum = Object_klass_knum,           \String_klass_knum,        java_lang_String_knum = String_klass_knum,           \Class_klass_knum,         java_lang_Class_knum = Class_klass_knum,             \Cloneable_klass_knum,     java_lang_Cloneable_knum = Cloneable_klass_knum,      \ClassLoader_klass_knum,   java_lang_ClassLoader_knum = ClassLoader_klass_knum,  \Serializable_klass_knum,  java_io_Serializable_knum = Serializable_klass_knum,  \System_klass_knum,        java_lang_System_knum = System_klass_knum,            \...WKID_LIMIT,FIRST_WKID = NO_WKID + 1}; // ==============第2块宏:函数=================
// 方法的宏展开前
#define WK_KLASS_DECLARE(name, symbol, option) \static Klass* name() { return check_klass_##option(_well_known_klasses[WK_KLASS_ENUM_NAME(name)]); } \static Klass** name##_addr() {                                                                       \return &SystemDictionary::_well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)];           \}WK_KLASSES_DO(WK_KLASS_DECLARE);#undef WK_KLASS_DECLARE
// 方法的宏展开后,这里以 Object_klass为例,其他的类推static Klass* Object_klass() { return check_klass_pre(_well_known_klasses[Object_klass_knum]); } static Klass** Object_klass_addr() {                                                             return &SystemDictionary::_well_known_klasses[SystemDictionary::Object_klass_knum];           }// ==============第3块宏:wk_init_info数组=================
// 宏展开前
static const short wk_init_info[] = {#define WK_KLASS_INIT_INFO(name, symbol, option) \( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \<< SystemDictionary::CEIL_LG_OPTION_LIMIT) \| (int)SystemDictionary::option ),WK_KLASSES_DO(WK_KLASS_INIT_INFO)#undef WK_KLASS_INIT_INFO0
};
// 宏展开后,这里还是以 Object_klass 为例,其他的类推
static const short wk_init_info[] = {( ((int)vmSymbols::java_lang_Object_enum << SystemDictionary::CEIL_LG_OPTION_LIMIT) | (int)SystemDictionary::Pre ),0
};

要使用的宏已经展开了,下面接着讲正题

void SystemDictionary::initialize_preloaded_classes(TRAPS) {assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");// Preload commonly used klassesWKID scan = FIRST_WKID;// first do Object, then String, Classif (UseSharedSpaces) {initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK);// Initialize the constant pool for the Object_classInstanceKlass* ik = InstanceKlass::cast(Object_klass());ik->constants()->restore_unshareable_info(CHECK);initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);} else {// 直接看这一步,这个的实现往后看,完成后,那些基础的类就被加载进来了,现在知道为啥我们在Java代码时,基础类(Object/Class/String等)都是不需要额外加载和import进来的,原因就在这initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);}// 计算String 和 Class 类的字段偏移java_lang_String::compute_offsets();java_lang_Class::compute_offsets();// 给8个基础类型创建它们的Class类型对象(在虚拟机中就是InstanceMirrorKlass)Universe::initialize_basic_type_mirrors(CHECK);// 修正mirrorUniverse::fixup_mirrors(CHECK);// 接下来都是继续加载基础类initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK);// Preload ref klasses and set reference typesInstanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER);InstanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass));initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Cleaner_klass), scan, CHECK);InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT);InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK);InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL);InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM);InstanceKlass::cast(WK_KLASS(Cleaner_klass))->set_reference_type(REF_CLEANER);initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(ReferenceQueue_klass), scan, CHECK);// JSR 292 classesWKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);WKID jsr292_group_end   = WK_KLASS_ENUM_NAME(VolatileCallSite_klass);initialize_wk_klasses_until(jsr292_group_start, scan, CHECK);if (EnableInvokeDynamic) {initialize_wk_klasses_through(jsr292_group_end, scan, CHECK);} else {// Skip the JSR 292 classes, if not enabled.scan = WKID(jsr292_group_end + 1);}initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK);// 基础类型的包装类的Klass_box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass);_box_klasses[T_CHAR]    = WK_KLASS(Character_klass);_box_klasses[T_FLOAT]   = WK_KLASS(Float_klass);_box_klasses[T_DOUBLE]  = WK_KLASS(Double_klass);_box_klasses[T_BYTE]    = WK_KLASS(Byte_klass);_box_klasses[T_SHORT]   = WK_KLASS(Short_klass);_box_klasses[T_INT]     = WK_KLASS(Integer_klass);_box_klasses[T_LONG]    = WK_KLASS(Long_klass);//_box_klasses[T_OBJECT]  = WK_KLASS(object_klass);//_box_klasses[T_ARRAY]   = WK_KLASS(object_klass);{ // 计算加载类时应该使用 loadClass 还是 loadClassInternalMethod* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());_has_loadClassInternal = (method != NULL);}{// 计算要不要用 checkPackageAccess 方法Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());_has_checkPackageAccess = (method != NULL);}
}

SystemDictionary::initialize_wk_klasses_until

void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {assert((int)start_id <= (int)limit_id, "IDs are out of order!");// 这里就是遍历 WKID 枚举,把基础类都加载一遍,看本章节前面对宏展开的那块for (int id = (int)start_id; id < (int)limit_id; id++) {assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");int info = wk_init_info[id - FIRST_WKID];int sid  = (info >> CEIL_LG_OPTION_LIMIT);int opt  = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));// 直接看这,这一步完成后,就对基础加载完成了,继续往后看initialize_wk_klass((WKID)id, opt, CHECK);}// 下一次开始的位置换到本次limit的位置start_id = limit_id;
}

SystemDictionary::initialize_wk_klass

bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");int  info = wk_init_info[id - FIRST_WKID];int  sid  = (info >> CEIL_LG_OPTION_LIMIT);// 拿到对应的符号Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);// 从数组 _well_known_klasses 中拿到已加载的类Klass**    klassp = &_well_known_klasses[id];bool must_load = (init_opt < SystemDictionary::Opt);if ((*klassp) == NULL) { // 初始klassp肯定是null//  下面就是真正类加载的环节了,这个先不讲,留待后续类加载一章来讲if (must_load) {(*klassp) = resolve_or_fail(symbol, true, CHECK_0); // 加载必须的类} else {(*klassp) = resolve_or_null(symbol,       CHECK_0); // 加载可选的类}}return ((*klassp) != NULL);
}

这篇关于Hotspot源码解析-第二十章-字典表创建和基础类预加载(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/620325

相关文章

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

在C#中合并和解析相对路径方式

《在C#中合并和解析相对路径方式》Path类提供了几个用于操作文件路径的静态方法,其中包括Combine方法和GetFullPath方法,Combine方法将两个路径合并在一起,但不会解析包含相对元素... 目录C#合并和解析相对路径System.IO.Path类幸运的是总结C#合并和解析相对路径对于 C

手把手教你idea中创建一个javaweb(webapp)项目详细图文教程

《手把手教你idea中创建一个javaweb(webapp)项目详细图文教程》:本文主要介绍如何使用IntelliJIDEA创建一个Maven项目,并配置Tomcat服务器进行运行,过程包括创建... 1.启动idea2.创建项目模板点击项目-新建项目-选择maven,显示如下页面输入项目名称,选择

Java解析JSON的六种方案

《Java解析JSON的六种方案》这篇文章介绍了6种JSON解析方案,包括Jackson、Gson、FastJSON、JsonPath、、手动解析,分别阐述了它们的功能特点、代码示例、高级功能、优缺点... 目录前言1. 使用 Jackson:业界标配功能特点代码示例高级功能优缺点2. 使用 Gson:轻量

Java如何接收并解析HL7协议数据

《Java如何接收并解析HL7协议数据》文章主要介绍了HL7协议及其在医疗行业中的应用,详细描述了如何配置环境、接收和解析数据,以及与前端进行交互的实现方法,文章还分享了使用7Edit工具进行调试的经... 目录一、前言二、正文1、环境配置2、数据接收:HL7Monitor3、数据解析:HL7Busines

MyBatis延迟加载的处理方案

《MyBatis延迟加载的处理方案》MyBatis支持延迟加载(LazyLoading),允许在需要数据时才从数据库加载,而不是在查询结果第一次返回时就立即加载所有数据,延迟加载的核心思想是,将关联对... 目录MyBATis如何处理延迟加载?延迟加载的原理1. 开启延迟加载2. 延迟加载的配置2.1 使用

python解析HTML并提取span标签中的文本

《python解析HTML并提取span标签中的文本》在网页开发和数据抓取过程中,我们经常需要从HTML页面中提取信息,尤其是span元素中的文本,span标签是一个行内元素,通常用于包装一小段文本或... 目录一、安装相关依赖二、html 页面结构三、使用 BeautifulSoup javascript