在上一篇博客中介紹了一下如何書寫自定義加載器.傳送門
這一篇要介紹的就是自定義類加載器來打破雙親委派機制.
思路就是加載類的時候不走parent屬性來找,直接從指定的classpath中找
package com.bonc.jvm;
import java.io.FileInputStream;
import java.lang.reflect.Method;
/**
* @Classname MyClassLoadTEst
* @Description TODO
* @Date 2020/6/14 16:31
* @Created by sz
*/
public class MyClassLoadTest {
static class MyClassLoad extends ClassLoader{
private String classPath;
public MyClassLoad(String classPath){
this.classPath=classPath;
}
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();
//只有自己寫的類走自己寫的類加載器,原來的類走自己類加載機制 這行代碼非常關鍵
if (!name.startsWith("com.bonc")){
c = super.loadClass(name,resolve);
}
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;
}
}
private byte[] loadByte(String name) throws Exception {
name = name.replaceAll("\\.", "/");
FileInputStream fis = new FileInputStream(classPath + "/" + name
+ ".class");
int len = fis.available();
byte[] data = new byte[len];
fis.read(data);
fis.close();
return data;
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
byte[] data = loadByte(name);
//defineClass將一個字節數組轉爲Class對象,這個字節數組是class文件讀取後最終的字節 數組。
return defineClass(name, data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
}
}
public static void main(String[] args) throws Exception {
MyClassLoad classLoader = new MyClassLoad("D:/tmp");
Class clazz = classLoader.loadClass("com.bonc.jvm.User1");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("print", null);
method.invoke(obj,null);
System.out.println(clazz.getClassLoader().getClass().getName());
System.out.println(clazz.getClassLoader().getParent().getClass().getName());
System.out.println(clazz.getClassLoader().getParent().getParent().getClass().getName());
}
}
代碼運行效果
在不打破雙親委派機制之前的代碼執行第一個的類加載器是AppClassLoader而不是自定義類加載器.因爲你的classpath目錄下已經存在一個Use1了.
那麼我們是否能夠重寫一個java.lang.String呢
package com.bonc.jvm;
import java.io.FileInputStream;
import java.lang.reflect.Method;
/**
* @Classname MyClassLoadTEst
* @Description TODO
* @Date 2020/6/14 16:31
* @Created by sz
*/
public class MyClassLoadTest {
static class MyClassLoad extends ClassLoader{
private String classPath;
public MyClassLoad(String classPath){
this.classPath=classPath;
}
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();
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;
}
}
private byte[] loadByte(String name) throws Exception {
name = name.replaceAll("\\.", "/");
FileInputStream fis = new FileInputStream(classPath + "/" + name
+ ".class");
int len = fis.available();
byte[] data = new byte[len];
fis.read(data);
fis.close();
return data;
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
byte[] data = loadByte(name);
//defineClass將一個字節數組轉爲Class對象,這個字節數組是class文件讀取後最終的字節 數組。
return defineClass(name, data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
}
}
public static void main(String[] args) throws Exception {
MyClassLoad classLoader = new MyClassLoad("D:/tmp");
Class clazz = classLoader.loadClass("java.lang.String");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("print", null);
method.invoke(obj,null);
System.out.println(clazz.getClassLoader().getClass().getName());
System.out.println(clazz.getClassLoader().getParent().getClass().getName());
System.out.println(clazz.getClassLoader().getParent().getParent().getClass().getName());
}
}
運行結果
結論:java爲了保護自己的核心類庫被篡權,核心的包下面的類不能被重寫