📄 classloader.java
字号:
// ClassLoader.java - Define policies for loading Java classes./* Copyright (C) 1998, 1999, 2000, 2001 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.net.URLConnection;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.Enumeration;import java.util.HashMap;import java.util.Stack;/** * The class <code>ClassLoader</code> is intended to be subclassed by * applications in order to describe new ways of loading classes, * such as over the network. * * @author Kresten Krab Thorup */public abstract class ClassLoader{ private ClassLoader parent; private HashMap definedPackages = new HashMap(); public final ClassLoader getParent () { /* FIXME: security */ return parent; } 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 { if (parent != null) return parent.loadClass (name, link); else c = gnu.gcj.runtime.VMClassLoader.instance.findClass (name); } 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; } /** Find a class. This should be overridden by subclasses; the * default implementation throws ClassNotFoundException. * * @param name Name of the class to find. * @return The class found. * @exception java.lang.ClassNotFoundException * @since 1.2 */ protected Class findClass (String name) throws ClassNotFoundException { throw new ClassNotFoundException (name); } // Protection Domain definitions // FIXME: should there be a special protection domain used for native code? // The permission required to check what a classes protection domain is. static final Permission protectionDomainPermission = new RuntimePermission("getProtectionDomain"); // The protection domain returned if we cannot determine it. static ProtectionDomain unknownProtectionDomain; // Protection domain to use when a class is defined without one specified. static ProtectionDomain defaultProtectionDomain; static { Permissions permissions = new Permissions(); permissions.add(new AllPermission()); unknownProtectionDomain = new ProtectionDomain(null, permissions); CodeSource cs = new CodeSource(null, null); defaultProtectionDomain = new ProtectionDomain(cs, Policy.getPolicy().getPermissions(cs)); } /** * Defines a class, given the class-data. According to the JVM, this * method should not be used; instead use the variant of this method * in which the name of the class being defined is specified * explicitly. * <P> * If the name of the class, as specified (implicitly) in the class * data, denotes a class which has already been loaded by this class * loader, an instance of * <code>java.lang.ClassNotFoundException</code> will be thrown. * * @param data bytes in class file format. * @param off offset to start interpreting data. * @param len length of data in class file. * @return the class defined. * @exception java.lang.ClassNotFoundException * @exception java.lang.LinkageError * @see ClassLoader#defineClass(String,byte[],int,int) */ protected final Class defineClass(byte[] data, int off, int len) throws ClassFormatError { return defineClass (null, data, off, len, defaultProtectionDomain); } protected final Class defineClass(String name, byte[] data, int off, int len) throws ClassFormatError { return defineClass (name, data, off, len, defaultProtectionDomain); } /** * Defines a class, given the class-data. This is preferable * over <code>defineClass(byte[],off,len)</code> since it is more * secure. If the expected name does not match that of the class * file, <code>ClassNotFoundException</code> is thrown. If * <code>name</code> denotes the name of an already loaded class, a * <code>LinkageError</code> is thrown. * <p> * * FIXME: How do we assure that the class-file data is not being * modified, simultaneously with the class loader running!? If this * was done in some very clever way, it might break security. * Right now I am thinking that defineclass should make sure never to * read an element of this array more than once, and that that would * assure the ``immutable'' appearance. It is still to be determined * if this is in fact how defineClass operates. * * @param name the expected name. * @param data bytes in class file format. * @param off offset to start interpreting data. * @param len length of data in class file. * @param protectionDomain security protection domain for the class. * @return the class defined. * @exception java.lang.ClassNotFoundException * @exception java.lang.LinkageError */ protected final synchronized Class defineClass(String name, byte[] data, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError { if (data==null || data.length < off+len || off<0 || len<0) throw new ClassFormatError ("arguments to defineClass " + "are meaningless"); // as per 5.3.5.1 if (name != null && findLoadedClass (name) != null) throw new java.lang.LinkageError ("class " + name + " already loaded"); if (protectionDomain == null) protectionDomain = defaultProtectionDomain; try { // Since we're calling into native code here, // we better make sure that any generated // exception is to spec! return defineClass0 (name, data, off, len, protectionDomain); } catch (LinkageError x) { throw x; // rethrow } catch (java.lang.VirtualMachineError x) { throw x; // rethrow } catch (java.lang.Throwable x) { // This should never happen, or we are beyond spec. throw new InternalError ("Unexpected exception " + "while defining class " + name + ": " + x.toString ()); } } /** This is the entry point of defineClass into the native code */ private native Class defineClass0 (String name, byte[] data, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError; /** * Link the given class. This will bring the class to a state where * the class initializer can be run. Linking involves the following * steps: * <UL> * <LI> Prepare (allocate and internalize) the constant strings that * are used in this class. * <LI> Allocate storage for static fields, and define the layout * of instance fields. * <LI> Perform static initialization of ``static final'' int, * long, float, double and String fields for which there is a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -