再来一个沙箱安全机制示例,尝试打破双亲委派机制,用自定义类加载器加载我们自己实现的 java.lang.String.class
public class MyClassload1 extends ClassLoader {标签:委派,fis,String,classPath,打破,双亲,new,class,name From: https://www.cnblogs.com/avalanche/p/17143927.html
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());
}
}