?? classloader.java
字號:
/* ClassLoader.java -- responsible for loading classes into the VM Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import java.io.InputStream;import java.io.IOException;import java.lang.reflect.Constructor;import java.net.URL;import java.security.CodeSource;import java.security.PermissionCollection;import java.security.Policy;import java.security.ProtectionDomain;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import gnu.java.util.DoubleEnumeration;import gnu.java.util.EmptyEnumeration;/** * The ClassLoader is a way of customizing the way Java gets its classes * and loads them into memory. The verifier and other standard Java things * still run, but the ClassLoader is allowed great flexibility in determining * where to get the classfiles and when to load and resolve them. For that * matter, a custom ClassLoader can perform on-the-fly code generation or * modification! * * <p>Every classloader has a parent classloader that is consulted before * the 'child' classloader when classes or resources should be loaded. * This is done to make sure that classes can be loaded from an hierarchy of * multiple classloaders and classloaders do not accidentially redefine * already loaded classes by classloaders higher in the hierarchy. * * <p>The grandparent of all classloaders is the bootstrap classloader, which * loads all the standard system classes as implemented by GNU Classpath. The * other special classloader is the system classloader (also called * application classloader) that loads all classes from the CLASSPATH * (<code>java.class.path</code> system property). The system classloader * is responsible for finding the application classes from the classpath, * and delegates all requests for the standard library classes to its parent * the bootstrap classloader. Most programs will load all their classes * through the system classloaders. * * <p>The bootstrap classloader in GNU Classpath is implemented as a couple of * static (native) methods on the package private class * <code>java.lang.VMClassLoader</code>, the system classloader is an * instance of <code>gnu.java.lang.SystemClassLoader</code> * (which is a subclass of <code>java.net.URLClassLoader</code>). * * <p>Users of a <code>ClassLoader</code> will normally just use the methods * <ul> * <li> <code>loadClass()</code> to load a class.</li> * <li> <code>getResource()</code> or <code>getResourceAsStream()</code> * to access a resource.</li> * <li> <code>getResources()</code> to get an Enumeration of URLs to all * the resources provided by the classloader and its parents with the * same name.</li> * </ul> * * <p>Subclasses should implement the methods * <ul> * <li> <code>findClass()</code> which is called by <code>loadClass()</code> * when the parent classloader cannot provide a named class.</li> * <li> <code>findResource()</code> which is called by * <code>getResource()</code> when the parent classloader cannot provide * a named resource.</li> * <li> <code>findResources()</code> which is called by * <code>getResource()</code> to combine all the resources with the * same name from the classloader and its parents.</li> * <li> <code>findLibrary()</code> which is called by * <code>Runtime.loadLibrary()</code> when a class defined by the * classloader wants to load a native library.</li> * </ul> * * @author John Keiser * @author Mark Wielaard * @author Eric Blake <ebb9@email.byu.edu> * @see Class * @since 1.0 * @status still missing 1.4 functionality */public abstract class ClassLoader{ /** * All packages defined by this classloader. It is not private in order to * allow native code (and trusted subclasses) access to this field. */ final Map definedPackages = new HashMap(); /** * The classloader that is consulted before this classloader. * If null then the parent is the bootstrap classloader. */ private final ClassLoader parent; /** * This is true if this classloader was successfully initialized. * This flag is needed to avoid a class loader attack: even if the * security manager rejects an attempt to create a class loader, the * malicious class could have a finalize method which proceeds to * define classes. */ private final boolean initialized; /** * System/Application classloader: defaults to an instance of * gnu.java.lang.SystemClassLoader, unless the first invocation of * getSystemClassLoader loads another class loader because of the * java.system.class.loader property. The initialization of this field * is somewhat circular - getSystemClassLoader() checks whether this * field is null in order to bypass a security check. */ static final ClassLoader systemClassLoader = VMClassLoader.getSystemClassLoader(); /** * The default protection domain, used when defining a class with a null * paramter for the domain. */ static final ProtectionDomain defaultProtectionDomain; static { CodeSource cs = new CodeSource(null, null); PermissionCollection perm = Policy.getPolicy().getPermissions(cs); defaultProtectionDomain = new ProtectionDomain(cs, perm); } /** * The desired assertion status of classes loaded by this loader, if not * overridden by package or class instructions. */ // Package visible for use by Class. boolean defaultAssertionStatus = VMClassLoader.defaultAssertionStatus(); /** * The command-line state of the package assertion status overrides. This * map is never modified, so it does not need to be synchronized. */ // Package visible for use by Class. static final Map systemPackageAssertionStatus = VMClassLoader.packageAssertionStatus(); /** * The map of package assertion status overrides, or null if no package * overrides have been specified yet. The values of the map should be * Boolean.TRUE or Boolean.FALSE, and the unnamed package is represented * by the null key. This map must be synchronized on this instance. */ // Package visible for use by Class. Map packageAssertionStatus; /** * The command-line state of the class assertion status overrides. This * map is never modified, so it does not need to be synchronized. */ // Package visible for use by Class. static final Map systemClassAssertionStatus = VMClassLoader.classAssertionStatus(); /** * The map of class assertion status overrides, or null if no class * overrides have been specified yet. The values of the map should be * Boolean.TRUE or Boolean.FALSE. This map must be synchronized on this * instance. */ // Package visible for use by Class. Map classAssertionStatus; /** * Create a new ClassLoader with as parent the system classloader. There * may be a security check for <code>checkCreateClassLoader</code>. * * @throws SecurityException if the security check fails */ protected ClassLoader() throws SecurityException { this(systemClassLoader); } /** * Create a new ClassLoader with the specified parent. The parent will * be consulted when a class or resource is requested through * <code>loadClass()</code> or <code>getResource()</code>. Only when the * parent classloader cannot provide the requested class or resource the * <code>findClass()</code> or <code>findResource()</code> method * of this classloader will be called. There may be a security check for * <code>checkCreateClassLoader</code>. * * @param parent the classloader's parent, or null for the bootstrap * classloader * @throws SecurityException if the security check fails * @since 1.2 */ protected ClassLoader(ClassLoader parent) { // May we create a new classloader? SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkCreateClassLoader(); this.parent = parent; this.initialized = true; } /** * Load a class using this ClassLoader or its parent, without resolving * it. Calls <code>loadClass(name, false)</code>. * * <p>Subclasses should not override this method but should override * <code>findClass()</code> which is called by this method. * * @param name the name of the class relative to this ClassLoader * @return the loaded class * @throws ClassNotFoundException if the class cannot be found */ public Class loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); } /** * Load a class using this ClassLoader or its parent, possibly resolving * it as well using <code>resolveClass()</code>. It first tries to find * out if the class has already been loaded through this classloader by * calling <code>findLoadedClass()</code>. Then it calls * <code>loadClass()</code> on the parent classloader (or when there is * no parent it uses the VM bootclassloader)</code>). If the class is still * not loaded it tries to create a new class by calling * <code>findClass()</code>. Finally when <code>resolve</code> is * <code>true</code> it also calls <code>resolveClass()</code> on the * newly loaded class. * * <p>Subclasses should not override this method but should override * <code>findClass()</code> which is called by this method. * * @param name the fully qualified name of the class to load * @param resolve whether or not to resolve the class * @return the loaded class * @throws ClassNotFoundException if the class cannot be found */ protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { // Have we already loaded this class? Class c = findLoadedClass(name); if (c != null) return c; // Can the class be loaded by a parent? try { if (parent == null) { c = VMClassLoader.loadClass(name, resolve); if (c != null) return c; } else { return parent.loadClass(name, resolve); } } catch (ClassNotFoundException e) { } // Still not found, we have to do it ourself. c = findClass(name); if (resolve) resolveClass(c); return c; } /** * Called for every class name that is needed but has not yet been * defined by this classloader or one of its parents. It is called by * <code>loadClass()</code> after both <code>findLoadedClass()</code> and * <code>parent.loadClass()</code> couldn't provide the requested class. * * <p>The default implementation throws a * <code>ClassNotFoundException</code>. Subclasses should override this
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -