loadClass()、findClass()、defineClass()
loadClass()中主要调用 findLoadedClass(String)调用这个方法
1、使用指定的二进制名称来加载
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // 首先,查看这个Class是否已经被加载 Class<?> c = findLoadedClass(name); //如果没有被加载,就查看父类加载器 if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) { //递归调用loadClass() c = parent.loadClass(name, false); } else { //父类加载器是null,说明是启动类加载器,查找对应的Class 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. //调用findClass() 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; } }
2、findClass 根据名称或者位置加载Class字节码,然后使用defineClass 通常由子类去实现
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name);
class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection } )
3、deineClass() 把字节码转换成Class
protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError { return defineClass(name, b, off, len, null); }