📄 classloader.java
字号:
// ClassLoader.java - Define policies for loading Java classes./* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation This file is part of libgcj.This software is copyrighted work licensed under the terms of theLibgcj License. Please consult the file "LIBGCJ_LICENSE" fordetails. */package java.lang;import java.io.InputStream;import java.io.IOException;import java.net.URL;import java.security.AllPermission;import java.security.CodeSource;import java.security.Permission;import java.security.Permissions;import java.security.Policy;import java.security.ProtectionDomain;import java.util.*;/** * 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 * @author Kresten Krab Thorup * @see Class * @since 1.0 * @status still missing 1.4 functionality */public abstract class ClassLoader{ /** * All classes loaded by this classloader. VM's may choose to implement * this cache natively; but it is here available for use if necessary. It * is not private in order to allow native code (and trusted subclasses) * access to this field. */ final Map loadedClasses = new HashMap(); /** * 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; /** * The classloader that is consulted before this classloader. * If null then the parent is the bootstrap classloader. */ private final ClassLoader parent; /** * All packages defined by this classloader. It is not private in order to * allow native code (and trusted subclasses) access to this field. */ private HashMap definedPackages = new HashMap(); /** * Returns the parent of this classloader. If the parent of this * classloader is the bootstrap classloader then this method returns * <code>null</code>. A security check may be performed on * <code>RuntimePermission("getClassLoader")</code>. * * @throws SecurityException if the security check fails * @since 1.2 */ public final ClassLoader getParent () { // Check if we may return the parent classloader SecurityManager sm = System.getSecurityManager(); if (sm != null) { /* FIXME: security, getClassContext() not implemented. Class c = VMSecurityManager.getClassContext()[1]; ClassLoader cl = c.getClassLoader(); if (cl != null && cl != this) sm.checkPermission(new RuntimePermission("getClassLoader")); */ } return parent; } /** * Returns the system classloader. The system classloader (also called * the application classloader) is the classloader that was used to * load the application classes on the classpath (given by the system * property <code>java.class.path</code>. This is set as the context * class loader for a thread. The system property * <code>java.system.class.loader</code>, if defined, is taken to be the * name of the class to use as the system class loader, which must have * a public constructor which takes a ClassLoader as a parent; otherwise this * uses gnu.java.lang.SystemClassLoader. * * <p>Note that this is different from the bootstrap classloader that * actually loads all the real "system" classes (the bootstrap classloader * is the parent of the returned system classloader). * * <p>A security check will be performed for * <code>RuntimePermission("getClassLoader")</code> if the calling class * is not a parent of the system class loader. * * @return the system class loader * @throws SecurityException if the security check fails * @throws IllegalStateException if this is called recursively * @throws Error if <code>java.system.class.loader</code> fails to load * @since 1.2 */ public static ClassLoader getSystemClassLoader () { return gnu.gcj.runtime.VMClassLoader.instance; } /** * Creates a <code>ClassLoader</code> with no parent. * @exception java.lang.SecurityException if not allowed */ protected ClassLoader() { this (null); } /** * Creates a <code>ClassLoader</code> with the given parent. * The parent may be <code>null</code>. * The only thing this * constructor does, is to call * <code>checkCreateClassLoader</code> on the current * security manager. * @exception java.lang.SecurityException if not allowed * @since 1.2 */ protected ClassLoader(ClassLoader parent) { SecurityManager security = System.getSecurityManager (); if (security != null) security.checkCreateClassLoader (); this.parent = parent; } /** * Loads and link the class by the given name. * @param name the name of the class. * @return the class loaded. * @see ClassLoader#loadClass(String,boolean) * @exception java.lang.ClassNotFoundException */ public Class loadClass(String name) throws java.lang.ClassNotFoundException { return loadClass (name, false); } /** * Loads the class by the given name. The default implementation * will search for the class in the following order (similar to jdk 1.2) * <ul> * <li> First <code>findLoadedClass</code>. * <li> If parent is non-null, <code>parent.loadClass</code>; * otherwise <code>findSystemClass</code>. * <li> <code>findClass</code>. * </ul> * If <code>link</code> is true, <code>resolveClass</code> is then * called. <p> Normally, this need not be overridden; override * <code>findClass</code> instead. * @param name the name of the class. * @param link if the class should be linked. * @return the class loaded. * @exception java.lang.ClassNotFoundException * @deprecated */ protected Class loadClass(String name, boolean link) throws java.lang.ClassNotFoundException { Class c = findLoadedClass (name); if (c == null) { try { ClassLoader cl = parent; if (parent == null) cl = gnu.gcj.runtime.VMClassLoader.instance; if (cl != this) c = cl.loadClass (name, link); } catch (ClassNotFoundException ex) { /* ignore, we'll try findClass */; } } if (c == null) c = findClass (name); if (c == null) throw new ClassNotFoundException (name); if (link) 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 * method. An implementation of this method in a subclass should get the * class bytes of the class (if it can find them), if the package of the * requested class doesn't exist it should define the package and finally * it should call define the actual class. It does not have to resolve the * class. It should look something like the following:<br> * * <pre> * // Get the bytes that describe the requested class * byte[] classBytes = classLoaderSpecificWayToFindClassBytes(name); * // Get the package name * int lastDot = name.lastIndexOf('.'); * if (lastDot != -1) * { * String packageName = name.substring(0, lastDot); * // Look if the package already exists * if (getPackage(pkg) == null) * { * // define the package * definePackage(packageName, ...); * } * } * // Define and return the class * return defineClass(name, classBytes, 0, classBytes.length); * </pre> * * <p><code>loadClass()</code> makes sure that the <code>Class</code> * returned by <code>findClass()</code> will later be returned by * <code>findLoadedClass()</code> when the same class name is requested. * * @param name class name to find (including the package name)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -