雙親委托
某個(gè)類加載時(shí),首先委托給parent加載,依次遞歸,如果parent可以拿到對(duì)象則返回,否則由子類獲取
優(yōu)點(diǎn)
- 避免重復(fù)加載
- 安全考慮,防止核心API被修改
protected 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);
}
}
return c;
}
熱修復(fù)
類加載是有先后順序,
首先獲取到當(dāng)前應(yīng)用PathClassLoader,通過反射獲取到DexPathList的pathList;修改pathList中的dexElements,
- 把補(bǔ)丁包patch.dex轉(zhuǎn)化為Element[] patch
- 獲取到dexElement對(duì)象屬性
-
patch+Dexelement合并,并反射賦值給dexElements
image.png

image.png
AndFix
反射 類加載

image.png
