/** * Returns the class loader for the class. Some implementations may use * null to represent the bootstrap class loader. This method will return * null in such implementations if this class was loaded by the bootstrap * class loader. * * <p> If a security manager is present, and the caller's class loader is * not null and the caller's class loader is not the same as or an ancestor of * the class loader for the class whose class loader is requested, then * this method calls the security manager's {@code checkPermission} * method with a {@code RuntimePermission("getClassLoader")} * permission to ensure it's ok to access the class loader for the class. * * <p>If this object * represents a primitive type or void, null is returned. * * @return the class loader that loaded the class or interface * represented by this object. * @throws SecurityException * if a security manager exists and its * {@code checkPermission} method denies * access to the class loader for the class. * @see java.lang.ClassLoader * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission */ @CallerSensitive public ClassLoader getClassLoader() { ClassLoadercl= getClassLoader0(); if (cl == null) returnnull; SecurityManagersm= System.getSecurityManager(); if (sm != null) { ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass()); } return cl; }
/** * Returns the system class loader for delegation. This is the default * delegation parent for new <tt>ClassLoader</tt> instances, and is * typically the class loader used to start the application. * * <p> This method is first invoked early in the runtime's startup * sequence, at which point it creates the system class loader and sets it * as the context class loader of the invoking <tt>Thread</tt>. * * <p> The default system class loader is an implementation-dependent * instance of this class. * * <p> If the system property "<tt>java.system.class.loader</tt>" is defined * when this method is first invoked then the value of that property is * taken to be the name of a class that will be returned as the system * class loader. The class is loaded using the default system class loader * and must define a public constructor that takes a single parameter of * type <tt>ClassLoader</tt> which is used as the delegation parent. An * instance is then created using this constructor with the default system * class loader as the parameter. The resulting class loader is defined * to be the system class loader. * * <p> If a security manager is present, and the invoker's class loader is * not <tt>null</tt> and the invoker's class loader is not the same as or * an ancestor of the system class loader, then this method invokes the * security manager's {@link * SecurityManager#checkPermission(java.security.Permission) * <tt>checkPermission</tt>} method with a {@link * RuntimePermission#RuntimePermission(String) * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify * access to the system class loader. If not, a * <tt>SecurityException</tt> will be thrown. </p> * * @return The system <tt>ClassLoader</tt> for delegation, or * <tt>null</tt> if none * * @throws SecurityException * If a security manager exists and its <tt>checkPermission</tt> * method doesn't allow access to the system class loader. * * @throws IllegalStateException * If invoked recursively during the construction of the class * loader specified by the "<tt>java.system.class.loader</tt>" * property. * * @throws Error * If the system property "<tt>java.system.class.loader</tt>" * is defined but the named class could not be loaded, the * provider class does not define the required constructor, or an * exception is thrown by that constructor when it is invoked. The * underlying cause of the error can be retrieved via the * {@link Throwable#getCause()} method. * * @revised 1.4 */ @CallerSensitive publicstatic ClassLoader getSystemClassLoader() { initSystemClassLoader(); if (scl == null) { returnnull; } SecurityManagersm= System.getSecurityManager(); if (sm != null) { checkClassLoaderPermission(scl, Reflection.getCallerClass()); } return scl; } privatestaticsynchronizedvoidinitSystemClassLoader() { if (!sclSet) { if (scl != null) thrownewIllegalStateException("recursive invocation"); // 主要的第一步是这 sun.misc.Launcherl= sun.misc.Launcher.getLauncher(); if (l != null) { Throwableoops=null; // 然后是这 scl = l.getClassLoader(); try { scl = AccessController.doPrivileged( newSystemClassLoaderAction(scl)); } catch (PrivilegedActionException pae) { oops = pae.getCause(); if (oops instanceof InvocationTargetException) { oops = oops.getCause(); } } if (oops != null) { if (oops instanceof Error) { throw (Error) oops; } else { // wrap the exception thrownewError(oops); } } } sclSet = true; } } // 接着跟到sun.misc.Launcher#getClassLoader public ClassLoader getClassLoader() { returnthis.loader; } // 然后看到这 sun.misc.Launcher#Launcher publicLauncher() { Launcher.ExtClassLoader var1; try { var1 = Launcher.ExtClassLoader.getExtClassLoader(); } catch (IOException var10) { thrownewInternalError("Could not create extension class loader", var10); }
/** * Loads the class with the specified <a href="#name">binary name</a>. The * default implementation of this method searches for classes in the * following order: * * <ol> * * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class * has already been loaded. </p></li> * * <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method * on the parent class loader. If the parent is <tt>null</tt> the class * loader built-in to the virtual machine is used, instead. </p></li> * * <li><p> Invoke the {@link #findClass(String)} method to find the * class. </p></li> * * </ol> * * <p> If the class was found using the above steps, and the * <tt>resolve</tt> flag is true, this method will then invoke the {@link * #resolveClass(Class)} method on the resulting <tt>Class</tt> object. * * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link * #findClass(String)}, rather than this method. </p> * * <p> Unless overridden, this method synchronizes on the result of * {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method * during the entire class loading process. * * @param name * The <a href="#name">binary name</a> of the class * * @param resolve * If <tt>true</tt> then resolve the class * * @return The resulting <tt>Class</tt> object * * @throws ClassNotFoundException * If the class could not be found */ 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) { longt0= 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. longt1= System.nanoTime(); // 调用自己的 findClass() 方法尝试进行加载 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; } }