打破双亲委派机制

再来一个沙箱安全机制示例,尝试打破双亲委派机制,用自定义类加载器加载我们自己实现的 java.lang.String.class
public class MyClassload1 extends ClassLoader {


private String classPath;

public MyClassload1(String classPath) {
this.classPath = classPath;
}

@Override
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.example.jvm")) {
c = this.getParent().loadClass(name);
} else {
// 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;
}
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String pacakge = name.replaceAll("\\.", "/");
FileInputStream fis = null;
byte[] data = null;
try {
fis = new FileInputStream(classPath + "/" + pacakge
+ ".class");
int len = fis.available();
data = new byte[len];
fis.read(data);
fis.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
//defineClass将一个字节数组转为Class对象,这个字节数组是class文件读取后最终的字节数组。
return defineClass(name, data, 0, data.length);
}

public static void main(String[] args) throws Exception {
MyClassload1 myClassload = new MyClassload1("C:/test");
Class<?> aClass = myClassload.loadClass("java.lang.String");
Object user = aClass.newInstance();
Method sout = aClass.getDeclaredMethod("sout", null);
sout.invoke(user, null);
System.out.println(aClass.getClassLoader().getClass().getName());
}
}
posted @   朱俊升  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示