protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // 首先,检测是否已经加载 Class<?> c = findLoadedClass(name); if (c == null) { // 若没有被加载 long t0 = System.nanoTime(); try { if (parent != null) { // 父加载器不为空则调用父加载器的loadClass c = parent.loadClass(name, false); } else { // 父加载器为空则调用BootstrapClassLoader 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; } }
publicLauncher(){ // Create the extension class loader // 创建ExtClassLoader ClassLoader extcl; try { extcl = ExtClassLoader.getExtClassLoader(); } catch (IOException e) { thrownew InternalError( "Could not create extension class loader", e); }
// Now create the class loader to use to launch the application try { // 通过ExtClassLoader创建AppClassLoader loader = AppClassLoader.getAppClassLoader(extcl); } catch (IOException e) { thrownew InternalError( "Could not create application class loader", e); }
/* * Returns the class loader used to launch the main application. */ public ClassLoader getClassLoader(){ return loader; } /* * The class loader used for loading installed extensions. */ staticclassExtClassLoaderextendsURLClassLoader{}
/** * The class loader used for loading from java.class.path. * runs in a restricted security context. */ staticclassAppClassLoaderextendsURLClassLoader{}
/** * create an ExtClassLoader. The ExtClassLoader is created * within a context that limits which files it can read */ publicstatic ExtClassLoader getExtClassLoader()throws IOException { // 获取ext目录下的所有文件 final File[] dirs = getExtDirs();
try { // Prior implementations of this doPrivileged() block supplied // aa synthesized ACC via a call to the private method // ExtClassLoader.getContext().
return AccessController.doPrivileged( new PrivilegedExceptionAction<ExtClassLoader>() { public ExtClassLoader run()throws IOException { int len = dirs.length; for (int i = 0; i < len; i++) { MetaIndex.registerDirectory(dirs[i]); } returnnew ExtClassLoader(dirs); } }); } catch (java.security.PrivilegedActionException e) { throw (IOException) e.getException(); } }
privatestatic File[] getExtDirs() { String s = System.getProperty("java.ext.dirs"); File[] dirs; if (s != null) { StringTokenizer st = new StringTokenizer(s, File.pathSeparator); int count = st.countTokens(); dirs = new File[count]; for (int i = 0; i < count; i++) { dirs[i] = new File(st.nextToken()); } } else { dirs = new File[0]; } return dirs; } }
/** * The class loader used for loading from java.class.path. * runs in a restricted security context. */ staticclassAppClassLoaderextendsURLClassLoader{
publicstatic ClassLoader getAppClassLoader(final ClassLoader extcl) throws IOException { final String s = System.getProperty("java.class.path"); final File[] path = (s == null) ? new File[0] : getClassPath(s);
return AccessController.doPrivileged( new PrivilegedAction<AppClassLoader>() { public AppClassLoader run(){ URL[] urls = (s == null) ? new URL[0] : pathToURLs(path); returnnew AppClassLoader(urls, extcl); } }); } }