ClassLoader源码阅读

news/2024/12/22 17:04:00/

安卓10

java层

libcore\ojluni\src\main\java\java\lang\ClassLoader.java

protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException
{// First, check if the class has already been loadedClass<?> c = findLoadedClass(name);if (c == null) {try {if (parent != null) {c = parent.loadClass(name, false);} else {c = findBootstrapClassOrNull(name);}} catch (ClassNotFoundException e) {// ClassNotFoundException thrown if class not found// from the non-null parent class loader}if (c == null) {// If still not found, then invoke findClass in order// to find the class.c = findClass(name);}}return c;
}/*** Returns the class with the given <a href="#name">binary name</a> if this* loader has been recorded by the Java virtual machine as an initiating* loader of a class with that <a href="#name">binary name</a>.  Otherwise* <tt>null</tt> is returned.** @param  name*         The <a href="#name">binary name</a> of the class** @return  The <tt>Class</tt> object, or <tt>null</tt> if the class has*          not been loaded** @since  1.1*/protected final Class<?> findLoadedClass(String name) {ClassLoader loader;if (this == BootClassLoader.getInstance())loader = null;elseloader = this;return VMClassLoader.findLoadedClass(loader, name);}

libcore\libart\src\main\java\java\lang\VMClassLoader.java

    @FastNativenative static Class findLoadedClass(ClassLoader cl, String name);

native层

art\runtime\native\java_lang_VMClassLoader.cc


static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,jstring javaName) {// Hard-coded performance optimization: We know that all failed libcore calls to findLoadedClass//                                      are followed by a call to the the classloader to actually//                                      load the class.if (loader != nullptr) {// Try the common case.StackHandleScope<1> hs(soa.Self());c = VMClassLoader::FindClassInPathClassLoader(cl,soa,soa.Self(),descriptor.c_str(),descriptor_hash,hs.NewHandle(loader));if (c != nullptr) {return soa.AddLocalReference<jclass>(c);}}// The class wasn't loaded, yet, and our fast-path did not apply (e.g., we didn't understand the// classloader chain).return nullptr;
}static ObjPtr<mirror::Class> FindClassInPathClassLoader(ClassLinker* cl,ScopedObjectAccessAlreadyRunnable& soa,Thread* self,const char* descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader)REQUIRES_SHARED(Locks::mutator_lock_) {ObjPtr<mirror::Class> result;if (cl->FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result)) {DCHECK(!self->IsExceptionPending());return result;}if (self->IsExceptionPending()) {self->ClearException();}return nullptr;}

art\runtime\class_linker.cc


bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,Thread* self,const char* descriptor,size_t hash,Handle<mirror::ClassLoader> class_loader,/*out*/ ObjPtr<mirror::Class>* result) {// Termination case: boot class loader.if (IsBootClassLoader(soa, class_loader.Get())) {*result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);return true;}if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) {// For regular path or dex class loader the search order is://    - parent//    - shared libraries//    - class loader dex files// Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).StackHandleScope<1> hs(self);Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) {return false;  // One of the parents is not supported.}if (*result != nullptr) {return true;  // Found the class up the chain.}if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {return false;  // One of the shared library loader is not supported.}if (*result != nullptr) {return true;  // Found the class in a shared library.}// Search the current class loader classpath.*result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);return !soa.Self()->IsExceptionPending();}if (IsDelegateLastClassLoader(soa, class_loader)) {// For delegate last, the search order is://    - boot class path//    - shared libraries//    - class loader dex files//    - parent*result = FindClassInBootClassLoaderClassPath(self, descriptor, hash);if (*result != nullptr) {return true;  // The class is part of the boot class path.}if (self->IsExceptionPending()) {// Pending exception means there was an error other than ClassNotFound that must be returned// to the caller.return false;}if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) {return false;  // One of the shared library loader is not supported.}if (*result != nullptr) {return true;  // Found the class in a shared library.}*result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader);if (*result != nullptr) {return true;  // Found the class in the current class loader}if (self->IsExceptionPending()) {// Pending exception means there was an error other than ClassNotFound that must be returned// to the caller.return false;}// Handles as RegisterDexFile may allocate dex caches (and cause thread suspension).StackHandleScope<1> hs(self);Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent()));return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result);}// Unsupported class loader.*result = nullptr;return false;
}

http://www.ppmy.cn/news/901832.html

相关文章

国内主要安全产品及厂商汇总

链接&#xff1a;https://www.zhihu.com/question/20334043/answer/183326406 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 国内哪些公司在做企业版安全产品开发&#xff1f; 问题太大了&#xff0c;做企业版安全…

ONES X 奥联|以精细化管理,高效交付软硬件产品

近日&#xff0c;ONES 签约密码安全企业——奥联。依托 ONES 高度灵活的自定义配置和统一配置的权限设计&#xff0c;奥联精细化管控研发过程&#xff0c;全流程保障数据安全&#xff0c;构建高效可信的持续交付能力。 深圳奥联信息安全技术有限公司&#xff08;以下简称奥联&a…

【网络安全带你练爬虫-100练】第13练:文件的创建、写入

目录 目标&#xff1a;将数据写入到文件中 网络安全O 目标&#xff1a;将数据写入到文件中 开干 &#xff08;始于颜值&#xff09;打开一个&#xff0c;没有就会创建 with open(data.csv, modew, newline) as file: &#xff08;忠于才华&#xff09;开始写入数据 writer cs…

【学会动态规划】不同路径(5)

目录 动态规划怎么学&#xff1f; 1. 题目解析 2. 算法原理 1. 状态表示 2. 状态转移方程 3. 初始化 4. 填表顺序 5. 返回值 3. 代码编写 写在最后&#xff1a; 动态规划怎么学&#xff1f; 学习一个算法没有捷径&#xff0c;更何况是学习动态规划&#xff0c; 跟我…

Android 串口调试助手

Android-SerialPort-Tool 项目地址&#xff1a;licheedev/Android-SerialPort-Tool 简介&#xff1a;Android 串口调试助手 更多&#xff1a;作者 提 Bug 标签&#xff1a; Android 串口调试助手 根据Android 官方提供的串口 API写的一个小工具&#xff0c; 没啥功能…

WIN10上 非常好用的 串口调试助手

完美适配高分屏. 解决大部分win32在高分屏模糊问题. 1.接收从串口进来的数据并在窗口显示。 2.接收到的数据可按 “字符串"或"HEX十六进制” 显示。 3.中文无乱码。可以在设置中更改字符串编码类型。支持多种字符编码 “ASCII”,“GB2312”,“UNICODE”,“UTF-8”…

一些网络调试助手和一些串口调试助手下载

网络调试助手&#xff08;NetAssist&#xff09; 串口调试助手&#xff08;UartAssist&#xff09; TCP&UDP调试助手 XCOM V2.6 下载地址 链接&#xff1a;https://pan.baidu.com/s/1cNx16kVEKVswQiC-Rn0Xvg 提取码&#xff1a;jmwj 可按需求下载

VOFA+ 串口调试助手

前言 人生如逆旅&#xff0c;我亦是行人。 最近看学习视频看到一位博主用了一个十分酷、之前都没怎么见过的串口调试助手&#xff0c;深深地被吸引了&#xff0c;然后去了解了一下&#xff0c;发现这款串口调试助手软件还挺不错的&#xff0c;最起码比我之前用过的都要更酷&…