- loadclass:判断是否已加载,使用双亲委派模型,请求父加载器,都为空,使用findclass
- findclass:根据名称或位置加载.class字节码,然后使用defineClass
- defineclass:解析定义.class字节流,返回class对象
- findclass:根据名称或位置加载.class字节码,然后使用defineClass
loadclass
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();
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.
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;
}
}
findclass
需要被子类重写
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
自定义实现findclass
引用https://www.cnblogs.com/FlyAway2013/p/5640586.html
protected Class<?> findClass(String name) throws ClassNotFoundException {
// return super.findClass(name);
System.out.println( "try findClass " + name);
InputStream is = null;
Class class1 = null;
try {
String classPath = name.replace(".", "\\") + ".class";
// String[] fqnArr = name.split("\\."); // split("."); 是不行的, 必须split("\\.")
// if (fqnArr == null || fqnArr.length == 0) {
// System.out.println("ClassLoaderLK.findClass()");
// fqnArr = name.split("\\.");
// } else {
// System.out.println( name + fqnArr.length);
// }
String classFile = path + classPath;
byte[] data = getClassFileBytes(classFile );
class1 = defineClass(name, data, 0, data.length);
if (class1 == null) {
System.out.println("ClassLoaderLK.findClass() ERR ");
throw new ClassFormatError();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return class1;
}
private byte[] getClassFileBytes(String classFile) throws Exception {
FileInputStream fis = new FileInputStream(classFile );
FileChannel fileC = fis.getChannel();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableByteChannel outC = Channels.newChannel(baos);
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (true) {
int i = fileC.read(buffer);
if (i == 0 || i == -1) {
break;
}
buffer.flip();
outC.write(buffer);
buffer.clear();
}
fis.close();
return baos.toByteArray();
}
}
definclass
protected final Class<?> defineClass(String name, byte[] b, int off, int len,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
protectionDomain = preDefineClass(name, protectionDomain);
String source = defineClassSourceLocation(protectionDomain);
Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);
postDefineClass(c, protectionDomain);
return c;
}