publicstatic ClassLoader createClassLoader(List<Repository> repositories, final ClassLoader parent) throws Exception {
if (log.isDebugEnabled()) log.debug("Creating new class loader");
// Construct the "class path" for this class loader Set<URL> set = new LinkedHashSet<>();
...
// Construct the class loader itself final URL[] array = set.toArray(new URL[set.size()]); if (log.isDebugEnabled()) for (int i = 0; i < array.length; i++) { log.debug(" location " + i + " is " + array[i]); }
return AccessController.doPrivileged( new PrivilegedAction<URLClassLoader>() { @Override public URLClassLoader run(){ if (parent == null) returnnew URLClassLoader(array); else returnnew URLClassLoader(array, parent); } }); }
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 { //如果父类加载器 不为空就 由父类加载器加载class 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; } }
核心逻辑就这段
1 2 3 4 5
if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); }