标签:深度 char 直接 new time extends launcher ini 空间
package jvm;
public class TestJDKClassLoader {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader());
System.out.println(com.sun.crypto.provider.DESKeyFactory.class.getClassLoader().getClass().getName());
System.out.println(TestJDKClassLoader.class.getClassLoader().getClass().getName());
System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());
}
结果:
null//启动类加载器是使用C++语言实现,所以无法打印
sun.misc.Launcher$ExtClassLoader
sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$AppClassLoader
package jvm;
import java.io.FileInputStream;
import java.io.IOException;
//自定义类加载器
public class MyClassloaderTest extends ClassLoader{
private String classPath;
//初始化时指定字节码目录所在目录
public MyClassloaderTest(String classPath) {
this.classPath = classPath;
}
//将字节码文件加载到内存中
private byte[] loadByte(String name) throws IOException {
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream(classPath+"/"+name+".class");
int len = fis.available();
byte[] bt = new byte[len];
fis.read(bt);
fis.close();
return bt;
}
//通过该类返回Class对象
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
byte[] data = loadByte(name);
return defineClass(name,data,0,data.length);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
}
}
package jvm;
import java.io.FileInputStream;
import java.io.IOException;
//自定义类加载器
public class MyClassloaderTest extends ClassLoader{
private String classPath;
public MyClassloaderTest(String classPath) {
this.classPath = classPath;
}
private byte[] loadByte(String name) throws IOException {
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream(classPath+"/"+name+".class");
int len = fis.available();
byte[] bt = new byte[len];
fis.read(bt);
fis.close();
return bt;
}
@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) {
// 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 {
try {
byte[] data = loadByte(name);
return defineClass(name,data,0,data.length);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
}
}
package jvm;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
MyClassloaderTest classloader = new MyClassloaderTest("F:\\test");
Class clazz = classloader.loadClass("java.lang.String");
Object o = clazz.newInstance();
Method method = clazz.getDeclaredMethod("sout", null);
method.invoke(o,null);
System.out.println(clazz.getClassLoader().getClass().getName());
}
}
标签:深度 char 直接 new time extends launcher ini 空间
原文地址:https://www.cnblogs.com/qishanmozi/p/c332d9e7c0c72831e4a4b0ae9f19e8f4.html