bootstrap类加载原理

让我们一起跟随ClassLoader里的本地方法 findBootstrapClass(), 进入jvm执行启动类加载器加载类的内部实现。

**private** **Class** findBootstrapClass0(String name)  **throws** ClassNotFoundException {
    check();
    **if** (!checkName(name))
        **throw** **new** ClassNotFoundException(name);
    **return** findBootstrapClass(name);
}

**private** **native** **Class** findBootstrapClass(String name) **throws** ClassNotFoundException;
_// __摘自__ j2sesrcshareclassesjavalangClassLoader.c_
_// __表示该函数将被__ java class __以__jni__方式调用_
JNIEXPORT jclass JNICALL
Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jobject loader, jstring classname) {
    char *clname;
    jclass cls = 0;
    char buf[128];

    if (classname == NULL) {
        _// __类名不能为空_
        JNU_ThrowClassNotFoundException(env, 0);
        return 0;
    }

    _// __将__java__的__string__转成__unicode__字符__, __如果__unicode__字符长度不超过__128_
    _// __则不分配内存,直接拿__buf__缓存来使用_
    _// __否则就__malloc__一块内存存放,__ __如果__malloc__失败,__ __则返回__NULL_
    clname = getUTF(env, classname, buf, sizeof(buf));
    if (clname == NULL) {
       _// __类名为空,__ __说明__ JVM __内存分配失败,__ __抛出邪恶的__OOM._
       JNU_ThrowOutOfMemoryError(env, NULL);
       return NULL;
    }

    _// __将__ '.' __转换成__ '/'_
    VerifyFixClassname(clname);

    if (!VerifyClassname(clname, JNI_TRUE)) {
      _// __如果指定的类名不合法,__ __抛出异常_
      JNU_ThrowClassNotFoundException(env, clname);
      goto done;
    }

    _// __让__jvm__使用启动类加载器加载类__, __第四位标志__0__表示使用启动类加载器__,_
    _// throwError__为__JNI_FALSE__时抛出__ ClassNotFoundException__,而__JNI_TRUE__抛出__NoClassDefFoundError_
    cls = JVM_FindClassFromClassLoader(env, clname, JNI_FALSE, 0, JNI_FALSE);

done:
    if (clname != buf) {
       _// __类名的格式有问题,且没有走__buf__缓存,则释放掉创建的内存__, __防止内存泄漏_
       free(clname);
    }

    return cls;
}
_// __摘自__ hotspotsrcsharevmprimsjvm.h_
_/*_
_* jvm.h__头文件__, __类似__java__接口的定义。_
_× __根据给定的__classLoader__来加载指定类_
_×_
_× *env JNI__运行环境_
_× *name __类名__(__路径__)_
_× init __是否需要初始化类的内部数据结构_
_× loader __类加载器标识_
_× throwError __抛出的异常类型_
_*/_
JNIEXPORT jclass JNICALL
JVM_FindClassFromClassLoader(JNIEnv *env, const char *name, jboolean init, jobject loader, jboolean throwError);
_// __摘自__ hotspotsrcsharevmprimsjvm.cpp_
JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, jboolean init, jobject loader,jboolean throwError))

JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name, throwError ? "error" : "exception");

_// __确保字符串不为__NULL__且长度不大于__ (1 << 16) -1 , __否则抛出异常_
if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
    if (throwError) {
       THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
    } else {
       THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
    }
}

_// __将类名放入一个__hashmap__,__ __标记符号用的__, __并构造为__ symbolHandle_
_// (__注意__: Handle__是一种间接的,__ __由线程变量分配空间的类,作用是防止__GC__回收)_
_// CHECK_0__是一个宏,__ __作用是判断是否有没有处理掉的异常,__ __如果有,__ __返回__0_
symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_0);

_// __将__classLoader__标识转换成__JVM__内部表示的数据结构__, __并构造为__ Handle_
Handle h_loader(THREAD, JNIHandles::resolve(loader));

jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
Handle(), throwError, thread);

if (TraceClassResolution && result != NULL) {
 trace_class_resolution(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(result)));
}

return result;
JVM_END
_// __摘自__ hotspotsrcsharevmprimsjvm.cpp_
jclass find_class_from_class_loader(JNIEnv* env, symbolHandle name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS) {

_// __生成类的内部数据结构__, __核心步骤,非常复杂__, __后期补充一下这里,__ __但是对核心逻辑没影响_
klassOop klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError, CHECK_0);

_// __将类的数据结构,__ __构造为__ KlassHandle_
KlassHandle klass_handle(THREAD, klass);

if (init && klass_handle->oop_is_instance()) {
_// __初始化类内部的数据结构_
klass_handle->initialize(CHECK_0);
}

_// __分配内存,__ __生成__class__对象_
return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
}

企业级互联网架构Aliware,让您的业务能力云化:https://www.aliyun.com/aliware