JVM学习笔记26——自定义系统类加载器源码分析与forName方法底层剖析
发布日期:2021-06-29 01:19:00 浏览次数:2 分类:技术文章

本文共 2066 字,大约阅读时间需要 6 分钟。

public static Class<?> forName(@NonNls String name,

                               boolean initialize,
                               ClassLoader loader)
                       throws ClassNotFoundException
External annotations available:
@org.jetbrains.annotations.NonNls
Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

If name denotes an array class, the component type of the array class is loaded but not initialized.

For example, in an instance method the expression:
      Class.forName("Foo")
is equivalent to:
      Class.forName("Foo", true, this.getClass().getClassLoader())
Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.

If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.

Parameters:
name - fully qualified name of the desired class
initialize - if true the class will be initialized. See Section 12.4 of The Java Language Specification.
loader - class loader from which the class must be loaded

Returns:
class object representing the desired class
Throws:

转载地址:https://blog.csdn.net/yshuoo/article/details/116542600 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JVM学习笔记30——线程上下文类加载器实战分析与难点剖析
下一篇:JVM学习笔记25——Launcher类源码分析

发表评论

最新留言

很好
[***.229.124.182]2024年04月07日 18时25分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章