类加载器
类与类加载器
类加载器分类:
主要分为Bootstrap ClassLoader、Extension ClassLoader、Application ClassLoader和User Defined ClassLoader。
启动类加载器(Bootstrap ClassLoader):
扩展类加载器(Extension ClassLoader):
应用程序类加载器(Application ClassLoader):
自定义类加载器(User Defined ClassLoader):
双亲委托模型:
public abstract class ClassLoader { private static native void registerNatives(); static { registerNatives(); } // The parent class loader for delegation private ClassLoader parent; // Hashtable that maps packages to certs private Hashtable package2certs = new Hashtable(11); }
登录后复制
类加载器SPI:
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // First, check if the class has already been loaded Class 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); } } if (resolve) { resolveClass(c); } return c; }
登录后复制
protected final Class<?> findLoadedClass(String name) { if (!checkName(name)) return null; return findLoadedClass0(name); } private native final Class findLoadedClass0(String name);
登录后复制
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
登录后复制
protected final Class<?> findSystemClass(String name) throws ClassNotFoundException { ClassLoader system = getSystemClassLoader(); if (system == null) { if (!checkName(name)) throw new ClassNotFoundException(name); Class cls = findBootstrapClass(name); if (cls == null) { throw new ClassNotFoundException(name); } return cls; } return system.loadClass(name); }
登录后复制
protected final Class<?> defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError { return defineClassCond(name, b, off, len, protectionDomain, true); } // Private method w/ an extra argument for skipping class verification private final Class<?> defineClassCond(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain, boolean verify) throws ClassFormatError { protectionDomain = preDefineClass(name, protectionDomain); Class c = null; String source = defineClassSourceLocation(protectionDomain); try { c = defineClass1(name, b, off, len, protectionDomain, source, verify); } catch (ClassFormatError cfe) { c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, source, verify); } postDefineClass(c, protectionDomain); return c; }
登录后复制
常见异常:
以上就是Java虚拟机学习 - 类加载器(ClassLoader) 的内容,更多相关内容请关注Work网(www.php.cn)!