java jdk 类加载机制_JDK源码阅读之类加载
java類加載
類的生命周期(類加載過程)
LLIUU+VPR
加載(Loading)
鏈接(Linking)
驗證(Verification)
準備(Preparation)
解析(Resolution)
初始化(Initialization)
使用(Using)
卸載(Unloading) 類
類加載器種類
BootstrapClassLoader:C++編寫,負責加載java核心類庫
Launcher.ExtClassLoader:Launcher中的內部類,parent == null
Launcher.AppClassLoader:Launcher中的內部類,parent == ExtClassLoader
用戶自定義ClassLoader:繼承自ClassLoader,parent == AppClassLoader
類加載機制
java中默認的類加載機制是雙親委派模式。
ClassLoader中關鍵的方法說明:
loadClass // 類加載入口,包含下面這些步驟
=> findLoadedClass => findLoadedClass0 // 先從緩存中查詢一下,看看目標類是否已加載過
=> findBootstrapClassOrNull => findBootstrapClass // 用Bootstrap類加載器進行加載
=> findClass // 讀取字節碼文件,然后加載字節碼文件
=> defineClass // 加載字節碼文件
=> preDefineClass // 加載前的檢查
=> defineClassSourceLocation // 定義類加載的路徑
=> defineClass1/defineClass2 // 調用native方法加載類
=> postDefineClass //
=> resolveClass => resolveClass0
ClassLoader 部分源碼:
package java.lang;
import java.io.InputStream;
...
public abstract class ClassLoader {
private final ClassLoader parent;
// -- Class --
protected Class> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class> c = findLoadedClass(name); // 緩存機制
if (c == null) {
long t0 = System.nanoTime();
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.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
// findClass由子類去實現
protected Class> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
// defineClass 加載類
protected final Class> defineClass(String name, byte[] b, int off, int len,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
protectionDomain = preDefineClass(name, protectionDomain);
String source = defineClassSourceLocation(protectionDomain);
Class> c = defineClass1(name, b, off, len, protectionDomain, source);
postDefineClass(c, protectionDomain);
return c;
}
private ProtectionDomain preDefineClass(String name,
ProtectionDomain pd)
{
if (!checkName(name))
throw new NoClassDefFoundError("IllegalName: " + name);
// Note: Checking logic in java.lang.invoke.MemberName.checkForTypeAlias
// relies on the fact that spoofing is impossible if a class has a name
// of the form "java.*"
if ((name != null) && name.startsWith("java.")) {
throw new SecurityException
("Prohibited package name: " +
name.substring(0, name.lastIndexOf('.')));
}
if (pd == null) {
pd = defaultDomain;
}
if (name != null) checkCerts(name, pd.getCodeSource());
return pd;
}
private String defineClassSourceLocation(ProtectionDomain pd)
{
CodeSource cs = pd.getCodeSource();
String source = null;
if (cs != null && cs.getLocation() != null) {
source = cs.getLocation().toString();
}
return source;
}
private void postDefineClass(Class> c, ProtectionDomain pd)
{
if (pd.getCodeSource() != null) {
Certificate certs[] = pd.getCodeSource().getCertificates();
if (certs != null)
setSigners(c, certs);
}
}
private native Class> defineClass0(String name, byte[] b, int off, int len,
ProtectionDomain pd);
private native Class> defineClass1(String name, byte[] b, int off, int len,
ProtectionDomain pd, String source);
private native Class> defineClass2(String name, java.nio.ByteBuffer b,
int off, int len, ProtectionDomain pd,
String source);
protected final void resolveClass(Class> c) {
resolveClass0(c);
}
private native void resolveClass0(Class> c);
private Class> findBootstrapClassOrNull(String name) {
if (!checkName(name)) return null;
return findBootstrapClass(name);
}
// return null if not found
private native Class> findBootstrapClass(String name);
protected final Class> findLoadedClass(String name) {
if (!checkName(name))
return null;
return findLoadedClass0(name);
}
private native final Class> findLoadedClass0(String name);
// -- Resource --
...
// -- Hierarchy --
...
// -- Package --
...
// -- Native library access --
...
// -- Assertion management --
...
}
雙親委派
總結
以上是生活随笔為你收集整理的java jdk 类加载机制_JDK源码阅读之类加载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flex + tomcat + myEc
- 下一篇: 中科院分词系统大致流程