Source Home >> Java Source 1.6.0 >> java.lang.ClassLoader V 0.09
  • 0001/*
  • 0002 * @(#)ClassLoader.java 1.189 05/11/17
  • 0003 *
  • 0004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 0005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 0006 */
  • 0007package java.lang;
  • 0008
  • 0009import java.io.InputStream;
  • 0010import java.io.IOException;
  • 0011import java.io.File;
  • 0012import java.lang.reflect.Constructor;
  • 0013import java.lang.reflect.InvocationTargetException;
  • 0014import java.net.MalformedURLException;
  • 0015import java.net.URL;
  • 0016import java.security.AccessController;
  • 0017import java.security.AccessControlContext;
  • 0018import java.security.CodeSource;
  • 0019import java.security.Policy;
  • 0020import java.security.PrivilegedAction;
  • 0021import java.security.PrivilegedActionException;
  • 0022import java.security.PrivilegedExceptionAction;
  • 0023import java.security.ProtectionDomain;
  • 0024import java.util.Enumeration;
  • 0025import java.util.Hashtable;
  • 0026import java.util.HashMap;
  • 0027import java.util.HashSet;
  • 0028import java.util.Set;
  • 0029import java.util.Stack;
  • 0030import java.util.Map;
  • 0031import java.util.Vector;
  • 0032import sun.misc.ClassFileTransformer;
  • 0033import sun.misc.CompoundEnumeration;
  • 0034import sun.misc.Resource;
  • 0035import sun.misc.URLClassPath;
  • 0036import sun.misc.VM;
  • 0037import sun.reflect.Reflection;
  • 0038import sun.security.util.SecurityConstants;
  • 0039
  • 0040/**
  • 0041 * A class loader is an object that is responsible for loading classes. The
  • 0042 * class <tt>ClassLoader</tt> is an abstract class. Given the <a
  • 0043 * href="#name">binary name</a> of a class, a class loader should attempt to
  • 0044 * locate or generate data that constitutes a definition for the class. A
  • 0045 * typical strategy is to transform the name into a file name and then read a
  • 0046 * "class file" of that name from a file system.
  • 0047 *
  • 0048 * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
  • 0049 * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
  • 0050 * it.
  • 0051 *
  • 0052 * <p> <tt>Class</tt> objects for array classes are not created by class
  • 0053 * loaders, but are created automatically as required by the Java runtime.
  • 0054 * The class loader for an array class, as returned by {@link
  • 0055 * Class#getClassLoader()} is the same as the class loader for its element
  • 0056 * type; if the element type is a primitive type, then the array class has no
  • 0057 * class loader.
  • 0058 *
  • 0059 * <p> Applications implement subclasses of <tt>ClassLoader</tt> in order to
  • 0060 * extend the manner in which the Java virtual machine dynamically loads
  • 0061 * classes.
  • 0062 *
  • 0063 * <p> Class loaders may typically be used by security managers to indicate
  • 0064 * security domains.
  • 0065 *
  • 0066 * <p> The <tt>ClassLoader</tt> class uses a delegation model to search for
  • 0067 * classes and resources. Each instance of <tt>ClassLoader</tt> has an
  • 0068 * associated parent class loader. When requested to find a class or
  • 0069 * resource, a <tt>ClassLoader</tt> instance will delegate the search for the
  • 0070 * class or resource to its parent class loader before attempting to find the
  • 0071 * class or resource itself. The virtual machine's built-in class loader,
  • 0072 * called the "bootstrap class loader", does not itself have a parent but may
  • 0073 * serve as the parent of a <tt>ClassLoader</tt> instance.
  • 0074 *
  • 0075 * <p> Normally, the Java virtual machine loads classes from the local file
  • 0076 * system in a platform-dependent manner. For example, on UNIX systems, the
  • 0077 * virtual machine loads classes from the directory defined by the
  • 0078 * <tt>CLASSPATH</tt> environment variable.
  • 0079 *
  • 0080 * <p> However, some classes may not originate from a file; they may originate
  • 0081 * from other sources, such as the network, or they could be constructed by an
  • 0082 * application. The method {@link #defineClass(String, byte[], int, int)
  • 0083 * <tt>defineClass</tt>} converts an array of bytes into an instance of class
  • 0084 * <tt>Class</tt>. Instances of this newly defined class can be created using
  • 0085 * {@link Class#newInstance <tt>Class.newInstance</tt>}.
  • 0086 *
  • 0087 * <p> The methods and constructors of objects created by a class loader may
  • 0088 * reference other classes. To determine the class(es) referred to, the Java
  • 0089 * virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
  • 0090 * the class loader that originally created the class.
  • 0091 *
  • 0092 * <p> For example, an application could create a network class loader to
  • 0093 * download class files from a server. Sample code might look like:
  • 0094 *
  • 0095 * <blockquote><pre>
  • 0096 * ClassLoader loader = new NetworkClassLoader(host, port);
  • 0097 * Object main = loader.loadClass("Main", true).newInstance();
  • 0098 *  . . .
  • 0099 * </pre></blockquote>
  • 0100 *
  • 0101 * <p> The network class loader subclass must define the methods {@link
  • 0102 * #findClass <tt>findClass</tt>} and <tt>loadClassData</tt> to load a class
  • 0103 * from the network. Once it has downloaded the bytes that make up the class,
  • 0104 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
  • 0105 * create a class instance. A sample implementation is:
  • 0106 *
  • 0107 * <blockquote><pre>
  • 0108 * class NetworkClassLoader extends ClassLoader {
  • 0109 * String host;
  • 0110 * int port;
  • 0111 *
  • 0112 * public Class findClass(String name) {
  • 0113 * byte[] b = loadClassData(name);
  • 0114 * return defineClass(name, b, 0, b.length);
  • 0115 * }
  • 0116 *
  • 0117 * private byte[] loadClassData(String name) {
  • 0118 * // load the class data from the connection
  • 0119 *  . . .
  • 0120 * }
  • 0121 * }
  • 0122 * </pre></blockquote>
  • 0123 *
  • 0124 * <h4> <a name="name">Binary names</a> </h4>
  • 0125 *
  • 0126 * <p> Any class name provided as a {@link String} parameter to methods in
  • 0127 * <tt>ClassLoader</tt> must be a binary name as defined by the <a
  • 0128 * href="http://java.sun.com/docs/books/jls/">Java Language Specification</a>.
  • 0129 *
  • 0130 * <p> Examples of valid class names include:
  • 0131 * <blockquote><pre>
  • 0132 * "java.lang.String"
  • 0133 * "javax.swing.JSpinner$DefaultEditor"
  • 0134 * "java.security.KeyStore$Builder$FileBuilder$1"
  • 0135 * "java.net.URLClassLoader$3$1"
  • 0136 * </pre></blockquote>
  • 0137 *
  • 0138 * @version 1.189, 11/17/05
  • 0139 * @see #resolveClass(Class)
  • 0140 * @since 1.0
  • 0141 */
  • 0142public abstract class ClassLoader {
  • 0143
  • 0144 private static native void registerNatives();
  • 0145 static {
  • 0146 registerNatives();
  • 0147 }
  • 0148
  • 0149 // If initialization succeed this is set to true and security checks will
  • 0150 // succeed. Otherwise the object is not initialized and the object is
  • 0151 // useless.
  • 0152 private boolean initialized = false;
  • 0153
  • 0154 // The parent class loader for delegation
  • 0155 private ClassLoader parent;
  • 0156
  • 0157 // Hashtable that maps packages to certs
  • 0158 private Hashtable package2certs = new Hashtable(11);
  • 0159
  • 0160 // Shared among all packages with unsigned classes
  • 0161 java.security.cert.Certificate[] nocerts;
  • 0162
  • 0163 // The classes loaded by this class loader. The only purpose of this table
  • 0164 // is to keep the classes from being GC'ed until the loader is GC'ed.
  • 0165 private Vector classes = new Vector();
  • 0166
  • 0167 // The initiating protection domains for all classes loaded by this loader
  • 0168 private Set domains = new HashSet();
  • 0169
  • 0170 // Invoked by the VM to record every loaded class with this loader.
  • 0171 void addClass(Class c) {
  • 0172 classes.addElement(c);
  • 0173 }
  • 0174
  • 0175 // The packages defined in this class loader. Each package name is mapped
  • 0176 // to its corresponding Package object.
  • 0177 private HashMap packages = new HashMap();
  • 0178
  • 0179 /**
  • 0180 * Creates a new class loader using the specified parent class loader for
  • 0181 * delegation.
  • 0182 *
  • 0183 * <p> If there is a security manager, its {@link
  • 0184 * SecurityManager#checkCreateClassLoader()
  • 0185 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
  • 0186 * a security exception. </p>
  • 0187 *
  • 0188 * @param parent
  • 0189 * The parent class loader
  • 0190 *
  • 0191 * @throws SecurityException
  • 0192 * If a security manager exists and its
  • 0193 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
  • 0194 * of a new class loader.
  • 0195 *
  • 0196 * @since 1.2
  • 0197 */
  • 0198 protected ClassLoader(ClassLoader parent) {
  • 0199 SecurityManager security = System.getSecurityManager();
  • 0200 if (security != null) {
  • 0201 security.checkCreateClassLoader();
  • 0202 }
  • 0203 this.parent = parent;
  • 0204 initialized = true;
  • 0205 }
  • 0206
  • 0207 /**
  • 0208 * Creates a new class loader using the <tt>ClassLoader</tt> returned by
  • 0209 * the method {@link #getSystemClassLoader()
  • 0210 * <tt>getSystemClassLoader()</tt>} as the parent class loader.
  • 0211 *
  • 0212 * <p> If there is a security manager, its {@link
  • 0213 * SecurityManager#checkCreateClassLoader()
  • 0214 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
  • 0215 * a security exception. </p>
  • 0216 *
  • 0217 * @throws SecurityException
  • 0218 * If a security manager exists and its
  • 0219 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
  • 0220 * of a new class loader.
  • 0221 */
  • 0222 protected ClassLoader() {
  • 0223 SecurityManager security = System.getSecurityManager();
  • 0224 if (security != null) {
  • 0225 security.checkCreateClassLoader();
  • 0226 }
  • 0227 this.parent = getSystemClassLoader();
  • 0228 initialized = true;
  • 0229 }
  • 0230
  • 0231
  • 0232 // -- Class --
  • 0233
  • 0234 /**
  • 0235 * Loads the class with the specified <a href="#name">binary name</a>.
  • 0236 * This method searches for classes in the same manner as the {@link
  • 0237 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
  • 0238 * machine to resolve class references. Invoking this method is equivalent
  • 0239 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
  • 0240 * false)</tt>}. </p>
  • 0241 *
  • 0242 * @param name
  • 0243 * The <a href="#name">binary name</a> of the class
  • 0244 *
  • 0245 * @return The resulting <tt>Class</tt> object
  • 0246 *
  • 0247 * @throws ClassNotFoundException
  • 0248 * If the class was not found
  • 0249 */
  • 0250 public Class<?> loadClass(String name) throws ClassNotFoundException {
  • 0251 return loadClass(name, false);
  • 0252 }
  • 0253
  • 0254 /**
  • 0255 * Loads the class with the specified <a href="#name">binary name</a>. The
  • 0256 * default implementation of this method searches for classes in the
  • 0257 * following order:
  • 0258 *
  • 0259 * <p><ol>
  • 0260 *
  • 0261 * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
  • 0262 * has already been loaded. </p></li>
  • 0263 *
  • 0264 * <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
  • 0265 * on the parent class loader. If the parent is <tt>null</tt> the class
  • 0266 * loader built-in to the virtual machine is used, instead. </p></li>
  • 0267 *
  • 0268 * <li><p> Invoke the {@link #findClass(String)} method to find the
  • 0269 * class. </p></li>
  • 0270 *
  • 0271 * </ol>
  • 0272 *
  • 0273 * <p> If the class was found using the above steps, and the
  • 0274 * <tt>resolve</tt> flag is true, this method will then invoke the {@link
  • 0275 * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
  • 0276 *
  • 0277 * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
  • 0278 * #findClass(String)}, rather than this method. </p>
  • 0279 *
  • 0280 * @param name
  • 0281 * The <a href="#name">binary name</a> of the class
  • 0282 *
  • 0283 * @param resolve
  • 0284 * If <tt>true</tt> then resolve the class
  • 0285 *
  • 0286 * @return The resulting <tt>Class</tt> object
  • 0287 *
  • 0288 * @throws ClassNotFoundException
  • 0289 * If the class could not be found
  • 0290 */
  • 0291 protected synchronized Class<?> loadClass(String name, boolean resolve)
  • 0292 throws ClassNotFoundException
  • 0293 {
  • 0294 // First, check if the class has already been loaded
  • 0295 Class c = findLoadedClass(name);
  • 0296 if (c == null) {
  • 0297 try {
  • 0298 if (parent != null) {
  • 0299 c = parent.loadClass(name, false);
  • 0300 } else {
  • 0301 c = findBootstrapClass0(name);
  • 0302 }
  • 0303 } catch (ClassNotFoundException e) {
  • 0304 // If still not found, then invoke findClass in order
  • 0305 // to find the class.
  • 0306 c = findClass(name);
  • 0307 }
  • 0308 }
  • 0309 if (resolve) {
  • 0310 resolveClass(c);
  • 0311 }
  • 0312 return c;
  • 0313 }
  • 0314
  • 0315 // This method is invoked by the virtual machine to load a class.
  • 0316 private synchronized Class loadClassInternal(String name)
  • 0317 throws ClassNotFoundException
  • 0318 {
  • 0319 return loadClass(name);
  • 0320 }
  • 0321
  • 0322 private void checkPackageAccess(Class cls, ProtectionDomain pd) {
  • 0323 final SecurityManager sm = System.getSecurityManager();
  • 0324 if (sm != null) {
  • 0325 final String name = cls.getName();
  • 0326 final int i = name.lastIndexOf('.');
  • 0327 if (i != -1) {
  • 0328 AccessController.doPrivileged(new PrivilegedAction() {
  • 0329 public Object run() {
  • 0330 sm.checkPackageAccess(name.substring(0, i));
  • 0331 return null;
  • 0332 }
  • 0333 }, new AccessControlContext(new ProtectionDomain[] {pd}));
  • 0334 }
  • 0335 }
  • 0336 domains.add(pd);
  • 0337 }
  • 0338
  • 0339 /**
  • 0340 * Finds the class with the specified <a href="#name">binary name</a>.
  • 0341 * This method should be overridden by class loader implementations that
  • 0342 * follow the delegation model for loading classes, and will be invoked by
  • 0343 * the {@link #loadClass <tt>loadClass</tt>} method after checking the
  • 0344 * parent class loader for the requested class. The default implementation
  • 0345 * throws a <tt>ClassNotFoundException</tt>. </p>
  • 0346 *
  • 0347 * @param name
  • 0348 * The <a href="#name">binary name</a> of the class
  • 0349 *
  • 0350 * @return The resulting <tt>Class</tt> object
  • 0351 *
  • 0352 * @throws ClassNotFoundException
  • 0353 * If the class could not be found
  • 0354 *
  • 0355 * @since 1.2
  • 0356 */
  • 0357 protected Class<?> findClass(String name) throws ClassNotFoundException {
  • 0358 throw new ClassNotFoundException(name);
  • 0359 }
  • 0360
  • 0361 /**
  • 0362 * Converts an array of bytes into an instance of class <tt>Class</tt>.
  • 0363 * Before the <tt>Class</tt> can be used it must be resolved. This method
  • 0364 * is deprecated in favor of the version that takes a <a
  • 0365 * href="#name">binary name</a> as its first argument, and is more secure.
  • 0366 *
  • 0367 * @param b
  • 0368 * The bytes that make up the class data. The bytes in positions
  • 0369 * <tt>off</tt> through <tt>off+len-1</tt> should have the format
  • 0370 * of a valid class file as defined by the <a
  • 0371 * href="http://java.sun.com/docs/books/vmspec/">Java Virtual
  • 0372 * Machine Specification</a>.
  • 0373 *
  • 0374 * @param off
  • 0375 * The start offset in <tt>b</tt> of the class data
  • 0376 *
  • 0377 * @param len
  • 0378 * The length of the class data
  • 0379 *
  • 0380 * @return The <tt>Class</tt> object that was created from the specified
  • 0381 * class data
  • 0382 *
  • 0383 * @throws ClassFormatError
  • 0384 * If the data did not contain a valid class
  • 0385 *
  • 0386 * @throws IndexOutOfBoundsException
  • 0387 * If either <tt>off</tt> or <tt>len</tt> is negative, or if
  • 0388 * <tt>off+len</tt> is greater than <tt>b.length</tt>.
  • 0389 *
  • 0390 * @see #loadClass(String, boolean)
  • 0391 * @see #resolveClass(Class)
  • 0392 *
  • 0393 * @deprecated Replaced by {@link #defineClass(String, byte[], int, int)
  • 0394 * defineClass(String, byte[], int, int)}
  • 0395 */
  • 0396 @Deprecated
  • 0397 protected final Class<?> defineClass(byte[] b, int off, int len)
  • 0398 throws ClassFormatError
  • 0399 {
  • 0400 return defineClass(null, b, off, len, null);
  • 0401 }
  • 0402
  • 0403 /**
  • 0404 * Converts an array of bytes into an instance of class <tt>Class</tt>.
  • 0405 * Before the <tt>Class</tt> can be used it must be resolved.
  • 0406 *
  • 0407 * <p> This method assigns a default {@link java.security.ProtectionDomain
  • 0408 * <tt>ProtectionDomain</tt>} to the newly defined class. The
  • 0409 * <tt>ProtectionDomain</tt> is effectively granted the same set of
  • 0410 * permissions returned when {@link
  • 0411 * java.security.Policy#getPermissions(java.security.CodeSource)
  • 0412 * <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt>}
  • 0413 * is invoked. The default domain is created on the first invocation of
  • 0414 * {@link #defineClass(String, byte[], int, int) <tt>defineClass</tt>},
  • 0415 * and re-used on subsequent invocations.
  • 0416 *
  • 0417 * <p> To assign a specific <tt>ProtectionDomain</tt> to the class, use
  • 0418 * the {@link #defineClass(String, byte[], int, int,
  • 0419 * java.security.ProtectionDomain) <tt>defineClass</tt>} method that takes a
  • 0420 * <tt>ProtectionDomain</tt> as one of its arguments. </p>
  • 0421 *
  • 0422 * @param name
  • 0423 * The expected <a href="#name">binary name</a> of the class, or
  • 0424 * <tt>null</tt> if not known
  • 0425 *
  • 0426 * @param b
  • 0427 * The bytes that make up the class data. The bytes in positions
  • 0428 * <tt>off</tt> through <tt>off+len-1</tt> should have the format
  • 0429 * of a valid class file as defined by the <a
  • 0430 * href="http://java.sun.com/docs/books/vmspec/">Java Virtual
  • 0431 * Machine Specification</a>.
  • 0432 *
  • 0433 * @param off
  • 0434 * The start offset in <tt>b</tt> of the class data
  • 0435 *
  • 0436 * @param len
  • 0437 * The length of the class data
  • 0438 *
  • 0439 * @return The <tt>Class</tt> object that was created from the specified
  • 0440 * class data.
  • 0441 *
  • 0442 * @throws ClassFormatError
  • 0443 * If the data did not contain a valid class
  • 0444 *
  • 0445 * @throws IndexOutOfBoundsException
  • 0446 * If either <tt>off</tt> or <tt>len</tt> is negative, or if
  • 0447 * <tt>off+len</tt> is greater than <tt>b.length</tt>.
  • 0448 *
  • 0449 * @throws SecurityException
  • 0450 * If an attempt is made to add this class to a package that
  • 0451 * contains classes that were signed by a different set of
  • 0452 * certificates than this class (which is unsigned), or if
  • 0453 * <tt>name</tt> begins with "<tt>java.</tt>".
  • 0454 *
  • 0455 * @see #loadClass(String, boolean)
  • 0456 * @see #resolveClass(Class)
  • 0457 * @see java.security.CodeSource
  • 0458 * @see java.security.SecureClassLoader
  • 0459 *
  • 0460 * @since 1.1
  • 0461 */
  • 0462 protected final Class<?> defineClass(String name, byte[] b, int off, int len)
  • 0463 throws ClassFormatError
  • 0464 {
  • 0465 return defineClass(name, b, off, len, null);
  • 0466 }
  • 0467
  • 0468 /* Determine protection domain, and check that:
  • 0469 - not define java.* class,
  • 0470 - signer of this class matches signers for the rest of the classes in package.
  • 0471 */
  • 0472 private ProtectionDomain preDefineClass(String name,
  • 0473 ProtectionDomain protectionDomain)
  • 0474 {
  • 0475 if (!checkName(name))
  • 0476 throw new NoClassDefFoundError("IllegalName: " + name);
  • 0477
  • 0478 if ((name != null) && name.startsWith("java.")) {
  • 0479 throw new SecurityException("Prohibited package name: " +
  • 0480 name.substring(0, name.lastIndexOf('.')));
  • 0481 }
  • 0482 if (protectionDomain == null) {
  • 0483 protectionDomain = getDefaultDomain();
  • 0484 }
  • 0485
  • 0486 if (name != null)
  • 0487 checkCerts(name, protectionDomain.getCodeSource());
  • 0488
  • 0489 return protectionDomain;
  • 0490 }
  • 0491
  • 0492 private String defineClassSourceLocation(ProtectionDomain protectionDomain)
  • 0493 {
  • 0494 CodeSource cs = protectionDomain.getCodeSource();
  • 0495 String source = null;
  • 0496 if (cs != null && cs.getLocation() != null) {
  • 0497 source = cs.getLocation().toString();
  • 0498 }
  • 0499 return source;
  • 0500 }
  • 0501
  • 0502 private Class defineTransformedClass(String name, byte[] b, int off, int len,
  • 0503 ProtectionDomain protectionDomain,
  • 0504 ClassFormatError cfe, String source)
  • 0505 throws ClassFormatError
  • 0506 {
  • 0507 // Class format error - try to transform the bytecode and
  • 0508 // define the class again
  • 0509 //
  • 0510 Object[] transformers = ClassFileTransformer.getTransformers();
  • 0511 Class c = null;
  • 0512
  • 0513 for (int i = 0; transformers != null && i < transformers.length; i++) {
  • 0514 try {
  • 0515 // Transform byte code using transformer
  • 0516 byte[] tb = ((ClassFileTransformer) transformers[i]).transform(b, off, len);
  • 0517 c = defineClass1(name, tb, 0, tb.length, protectionDomain, source);
  • 0518 break;
  • 0519 } catch (ClassFormatError cfe2) {
  • 0520 // If ClassFormatError occurs, try next transformer
  • 0521 }
  • 0522 }
  • 0523
  • 0524 // Rethrow original ClassFormatError if unable to transform
  • 0525 // bytecode to well-formed
  • 0526 //
  • 0527 if (c == null)
  • 0528 throw cfe;
  • 0529
  • 0530 return c;
  • 0531 }
  • 0532
  • 0533 private void postDefineClass(Class c, ProtectionDomain protectionDomain)
  • 0534 {
  • 0535 if (protectionDomain.getCodeSource() != null) {
  • 0536 java.security.cert.Certificate certs[] =
  • 0537 protectionDomain.getCodeSource().getCertificates();
  • 0538 if (certs != null)
  • 0539 setSigners(c, certs);
  • 0540 }
  • 0541 }
  • 0542
  • 0543 /**
  • 0544 * Converts an array of bytes into an instance of class <tt>Class</tt>,
  • 0545 * with an optional <tt>ProtectionDomain</tt>. If the domain is
  • 0546 * <tt>null</tt>, then a default domain will be assigned to the class as
  • 0547 * specified in the documentation for {@link #defineClass(String, byte[],
  • 0548 * int, int)}. Before the class can be used it must be resolved.
  • 0549 *
  • 0550 * <p> The first class defined in a package determines the exact set of
  • 0551 * certificates that all subsequent classes defined in that package must
  • 0552 * contain. The set of certificates for a class is obtained from the
  • 0553 * {@link java.security.CodeSource <tt>CodeSource</tt>} within the
  • 0554 * <tt>ProtectionDomain</tt> of the class. Any classes added to that
  • 0555 * package must contain the same set of certificates or a
  • 0556 * <tt>SecurityException</tt> will be thrown. Note that if
  • 0557 * <tt>name</tt> is <tt>null</tt>, this check is not performed.
  • 0558 * You should always pass in the <a href="#name">binary name</a> of the
  • 0559 * class you are defining as well as the bytes. This ensures that the
  • 0560 * class you are defining is indeed the class you think it is.
  • 0561 *
  • 0562 * <p> The specified <tt>name</tt> cannot begin with "<tt>java.</tt>", since
  • 0563 * all classes in the "<tt>java.*</tt> packages can only be defined by the
  • 0564 * bootstrap class loader. If <tt>name</tt> is not <tt>null</tt>, it
  • 0565 * must be equal to the <a href="#name">binary name</a> of the class
  • 0566 * specified by the byte array "<tt>b</tt>", otherwise a {@link
  • 0567 * <tt>NoClassDefFoundError</tt>} will be thrown. </p>
  • 0568 *
  • 0569 * @param name
  • 0570 * The expected <a href="#name">binary name</a> of the class, or
  • 0571 * <tt>null</tt> if not known
  • 0572 *
  • 0573 * @param b
  • 0574 * The bytes that make up the class data. The bytes in positions
  • 0575 * <tt>off</tt> through <tt>off+len-1</tt> should have the format
  • 0576 * of a valid class file as defined by the <a
  • 0577 * href="http://java.sun.com/docs/books/vmspec/">Java Virtual
  • 0578 * Machine Specification</a>.
  • 0579 *
  • 0580 * @param off
  • 0581 * The start offset in <tt>b</tt> of the class data
  • 0582 *
  • 0583 * @param len
  • 0584 * The length of the class data
  • 0585 *
  • 0586 * @param protectionDomain
  • 0587 * The ProtectionDomain of the class
  • 0588 *
  • 0589 * @return The <tt>Class</tt> object created from the data,
  • 0590 * and optional <tt>ProtectionDomain</tt>.
  • 0591 *
  • 0592 * @throws ClassFormatError
  • 0593 * If the data did not contain a valid class
  • 0594 *
  • 0595 * @throws NoClassDefFoundError
  • 0596 * If <tt>name</tt> is not equal to the <a href="#name">binary
  • 0597 * name</a> of the class specified by <tt>b</tt>
  • 0598 *
  • 0599 * @throws IndexOutOfBoundsException
  • 0600 * If either <tt>off</tt> or <tt>len</tt> is negative, or if
  • 0601 * <tt>off+len</tt> is greater than <tt>b.length</tt>.
  • 0602 *
  • 0603 * @throws SecurityException
  • 0604 * If an attempt is made to add this class to a package that
  • 0605 * contains classes that were signed by a different set of
  • 0606 * certificates than this class, or if <tt>name</tt> begins with
  • 0607 * "<tt>java.</tt>".
  • 0608 */
  • 0609 protected final Class<?> defineClass(String name, byte[] b, int off, int len,
  • 0610 ProtectionDomain protectionDomain)
  • 0611 throws ClassFormatError
  • 0612 {
  • 0613 check();
  • 0614 protectionDomain = preDefineClass(name, protectionDomain);
  • 0615
  • 0616 Class c = null;
  • 0617 String source = defineClassSourceLocation(protectionDomain);
  • 0618
  • 0619 try {
  • 0620 c = defineClass1(name, b, off, len, protectionDomain, source);
  • 0621 } catch (ClassFormatError cfe) {
  • 0622 c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, source);
  • 0623 }
  • 0624
  • 0625 postDefineClass(c, protectionDomain);
  • 0626 return c;
  • 0627 }
  • 0628
  • 0629 /**
  • 0630 * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
  • 0631 * into an instance of class <tt>Class</tt>,
  • 0632 * with an optional <tt>ProtectionDomain</tt>. If the domain is
  • 0633 * <tt>null</tt>, then a default domain will be assigned to the class as
  • 0634 * specified in the documentation for {@link #defineClass(String, byte[],
  • 0635 * int, int)}. Before the class can be used it must be resolved.
  • 0636 *
  • 0637 * <p>The rules about the first class defined in a package determining the set of
  • 0638 * certificates for the package, and the restrictions on class names are identical
  • 0639 * to those specified in the documentation for {@link #defineClass(String, byte[],
  • 0640 * int, int, ProtectionDomain)}.
  • 0641 *
  • 0642 * <p> An invocation of this method of the form
  • 0643 * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
  • 0644 * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
  • 0645 * result as the statements
  • 0646 *
  • 0647 * <blockquote><tt>
  • 0648 * ...<br>
  • 0649 * byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#remaining
  • 0650 * remaining}()];<br>
  • 0651 * </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[])
  • 0652 * get}(temp);<br>
  • 0653 * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
  • 0654 * </tt><i>cl</i><tt>.defineClass}(</tt><i>name</i><tt>, temp, 0, temp.length, </tt><i>pd</i><tt>);<br>
  • 0655 * </tt></blockquote>
  • 0656 *
  • 0657 * @param name
  • 0658 * The expected <a href="#name">binary name</a. of the class, or
  • 0659 * <tt>null</tt> if not known
  • 0660 *
  • 0661 * @param b
  • 0662 * The bytes that make up the class data. The bytes from positions
  • 0663 * <tt>b.position()</tt> through <tt>b.position() + b.limit() -1 </tt>
  • 0664 * should have the format of a valid class file as defined by the <a
  • 0665 * href="http://java.sun.com/docs/books/vmspec/">Java Virtual
  • 0666 * Machine Specification</a>.
  • 0667 *
  • 0668 * @param protectionDomain
  • 0669 * The ProtectionDomain of the class, or <tt>null</tt>.
  • 0670 *
  • 0671 * @return The <tt>Class</tt> object created from the data,
  • 0672 * and optional <tt>ProtectionDomain</tt>.
  • 0673 *
  • 0674 * @throws ClassFormatError
  • 0675 * If the data did not contain a valid class.
  • 0676 *
  • 0677 * @throws NoClassDefFoundError
  • 0678 * If <tt>name</tt> is not equal to the <a href="#name">binary
  • 0679 * name</a> of the class specified by <tt>b</tt>
  • 0680 *
  • 0681 * @throws SecurityException
  • 0682 * If an attempt is made to add this class to a package that
  • 0683 * contains classes that were signed by a different set of
  • 0684 * certificates than this class, or if <tt>name</tt> begins with
  • 0685 * "<tt>java.</tt>".
  • 0686 *
  • 0687 * @see #defineClass(String, byte[], int, int, ProtectionDomain)
  • 0688 *
  • 0689 * @since 1.5
  • 0690 */
  • 0691 protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
  • 0692 ProtectionDomain protectionDomain)
  • 0693 throws ClassFormatError
  • 0694 {
  • 0695 check();
  • 0696
  • 0697 int len = b.remaining();
  • 0698
  • 0699 // Use byte[] if not a direct ByteBufer:
  • 0700 if (!b.isDirect()) {
  • 0701 if (b.hasArray()) {
  • 0702 return defineClass(name, b.array(),
  • 0703 b.position() + b.arrayOffset(), len,
  • 0704 protectionDomain);
  • 0705 } else {
  • 0706 // no array, or read-only array
  • 0707 byte[] tb = new byte[len];
  • 0708 b.get(tb); // get bytes out of byte buffer.
  • 0709 return defineClass(name, tb, 0, len, protectionDomain);
  • 0710 }
  • 0711 }
  • 0712
  • 0713 protectionDomain = preDefineClass(name, protectionDomain);
  • 0714
  • 0715 Class c = null;
  • 0716 String source = defineClassSourceLocation(protectionDomain);
  • 0717
  • 0718 try {
  • 0719 c = defineClass2(name, b, b.position(), len, protectionDomain, source);
  • 0720 } catch (ClassFormatError cfe) {
  • 0721 byte[] tb = new byte[len];
  • 0722 b.get(tb); // get bytes out of byte buffer.
  • 0723 c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe, source);
  • 0724 }
  • 0725
  • 0726 postDefineClass(c, protectionDomain);
  • 0727 return c;
  • 0728 }
  • 0729
  • 0730 private native Class defineClass0(String name, byte[] b, int off, int len,
  • 0731 ProtectionDomain pd);
  • 0732
  • 0733 private native Class defineClass1(String name, byte[] b, int off, int len,
  • 0734 ProtectionDomain pd, String source);
  • 0735
  • 0736 private native Class defineClass2(String name, java.nio.ByteBuffer b,
  • 0737 int off, int len, ProtectionDomain pd,
  • 0738 String source);
  • 0739
  • 0740 // true if the name is null or has the potential to be a valid binary name
  • 0741 private boolean checkName(String name) {
  • 0742 if ((name == null) || (name.length() == 0))
  • 0743 return true;
  • 0744 if ((name.indexOf('/') != -1)
  • 0745 || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
  • 0746 return false;
  • 0747 return true;
  • 0748 }
  • 0749
  • 0750 private synchronized void checkCerts(String name, CodeSource cs) {
  • 0751 int i = name.lastIndexOf('.');
  • 0752 String pname = (i == -1) ? "" : name.substring(0, i);
  • 0753 java.security.cert.Certificate[] pcerts =
  • 0754 (java.security.cert.Certificate[]) package2certs.get(pname);
  • 0755 if (pcerts == null) {
  • 0756 // first class in this package gets to define which
  • 0757 // certificates must be the same for all other classes
  • 0758 // in this package
  • 0759 if (cs != null) {
  • 0760 pcerts = cs.getCertificates();
  • 0761 }
  • 0762 if (pcerts == null) {
  • 0763 if (nocerts == null)
  • 0764 nocerts = new java.security.cert.Certificate[0];
  • 0765 pcerts = nocerts;
  • 0766 }
  • 0767 package2certs.put(pname, pcerts);
  • 0768 } else {
  • 0769 java.security.cert.Certificate[] certs = null;
  • 0770 if (cs != null) {
  • 0771 certs = cs.getCertificates();
  • 0772 }
  • 0773
  • 0774 if (!compareCerts(pcerts, certs)) {
  • 0775 throw new SecurityException("class \""+ name +
  • 0776 "\"'s signer information does not match signer information of other classes in the same package");
  • 0777 }
  • 0778 }
  • 0779 }
  • 0780
  • 0781 /**
  • 0782 * check to make sure the certs for the new class (certs) are the same as
  • 0783 * the certs for the first class inserted in the package (pcerts)
  • 0784 */
  • 0785 private boolean compareCerts(java.security.cert.Certificate[] pcerts,
  • 0786 java.security.cert.Certificate[] certs)
  • 0787 {
  • 0788 // certs can be null, indicating no certs.
  • 0789 if ((certs == null) || (certs.length == 0)) {
  • 0790 return pcerts.length == 0;
  • 0791 }
  • 0792
  • 0793 // the length must be the same at this point
  • 0794 if (certs.length != pcerts.length)
  • 0795 return false;
  • 0796
  • 0797 // go through and make sure all the certs in one array
  • 0798 // are in the other and vice-versa.
  • 0799 boolean match;
  • 0800 for (int i = 0; i < certs.length; i++) {
  • 0801 match = false;
  • 0802 for (int j = 0; j < pcerts.length; j++) {
  • 0803 if (certs[i].equals(pcerts[j])) {
  • 0804 match = true;
  • 0805 break;
  • 0806 }
  • 0807 }
  • 0808 if (!match) return false;
  • 0809 }
  • 0810
  • 0811 // now do the same for pcerts
  • 0812 for (int i = 0; i < pcerts.length; i++) {
  • 0813 match = false;
  • 0814 for (int j = 0; j < certs.length; j++) {
  • 0815 if (pcerts[i].equals(certs[j])) {
  • 0816 match = true;
  • 0817 break;
  • 0818 }
  • 0819 }
  • 0820 if (!match) return false;
  • 0821 }
  • 0822
  • 0823 return true;
  • 0824 }
  • 0825
  • 0826 /**
  • 0827 * Links the specified class. This (misleadingly named) method may be
  • 0828 * used by a class loader to link a class. If the class <tt>c</tt> has
  • 0829 * already been linked, then this method simply returns. Otherwise, the
  • 0830 * class is linked as described in the "Execution" chapter of the <a
  • 0831 * href="http://java.sun.com/docs/books/jls/">Java Language
  • 0832 * Specification</a>.
  • 0833 * </p>
  • 0834 *
  • 0835 * @param c
  • 0836 * The class to link
  • 0837 *
  • 0838 * @throws NullPointerException
  • 0839 * If <tt>c</tt> is <tt>null</tt>.
  • 0840 *
  • 0841 * @see #defineClass(String, byte[], int, int)
  • 0842 */
  • 0843 protected final void resolveClass(Class<?> c) {
  • 0844 check();
  • 0845 resolveClass0(c);
  • 0846 }
  • 0847
  • 0848 private native void resolveClass0(Class c);
  • 0849
  • 0850 /**
  • 0851 * Finds a class with the specified <a href="#name">binary name</a>,
  • 0852 * loading it if necessary.
  • 0853 *
  • 0854 * <p> This method loads the class through the system class loader (see
  • 0855 * {@link #getSystemClassLoader()}). The <tt>Class</tt> object returned
  • 0856 * might have more than one <tt>ClassLoader</tt> associated with it.
  • 0857 * Subclasses of <tt>ClassLoader</tt> need not usually invoke this method,
  • 0858 * because most class loaders need to override just {@link
  • 0859 * #findClass(String)}. </p>
  • 0860 *
  • 0861 * @param name
  • 0862 * The <a href="#name">binary name</a> of the class
  • 0863 *
  • 0864 * @return The <tt>Class</tt> object for the specified <tt>name</tt>
  • 0865 *
  • 0866 * @throws ClassNotFoundException
  • 0867 * If the class could not be found
  • 0868 *
  • 0869 * @see #ClassLoader(ClassLoader)
  • 0870 * @see #getParent()
  • 0871 */
  • 0872 protected final Class<?> findSystemClass(String name)
  • 0873 throws ClassNotFoundException
  • 0874 {
  • 0875 check();
  • 0876 ClassLoader system = getSystemClassLoader();
  • 0877 if (system == null) {
  • 0878 if (!checkName(name))
  • 0879 throw new ClassNotFoundException(name);
  • 0880 return findBootstrapClass(name);
  • 0881 }
  • 0882 return system.loadClass(name);
  • 0883 }
  • 0884
  • 0885 private Class findBootstrapClass0(String name)
  • 0886 throws ClassNotFoundException
  • 0887 {
  • 0888 check();
  • 0889 if (!checkName(name))
  • 0890 throw new ClassNotFoundException(name);
  • 0891 return findBootstrapClass(name);
  • 0892 }
  • 0893
  • 0894 private native Class findBootstrapClass(String name)
  • 0895 throws ClassNotFoundException;
  • 0896
  • 0897 // Check to make sure the class loader has been initialized.
  • 0898 private void check() {
  • 0899 if (!initialized) {
  • 0900 throw new SecurityException("ClassLoader object not initialized");
  • 0901 }
  • 0902 }
  • 0903
  • 0904 /**
  • 0905 * Returns the class with the given <a href="#name">binary name</a> if this
  • 0906 * loader has been recorded by the Java virtual machine as an initiating
  • 0907 * loader of a class with that <a href="#name">binary name</a>. Otherwise
  • 0908 * <tt>null</tt> is returned. </p>
  • 0909 *
  • 0910 * @param name
  • 0911 * The <a href="#name">binary name</a> of the class
  • 0912 *
  • 0913 * @return The <tt>Class</tt> object, or <tt>null</tt> if the class has
  • 0914 * not been loaded
  • 0915 *
  • 0916 * @since 1.1
  • 0917 */
  • 0918 protected final Class<?> findLoadedClass(String name) {
  • 0919 check();
  • 0920 if (!checkName(name))
  • 0921 return null;
  • 0922 return findLoadedClass0(name);
  • 0923 }
  • 0924
  • 0925 private native final Class findLoadedClass0(String name);
  • 0926
  • 0927 /**
  • 0928 * Sets the signers of a class. This should be invoked after defining a
  • 0929 * class. </p>
  • 0930 *
  • 0931 * @param c
  • 0932 * The <tt>Class</tt> object
  • 0933 *
  • 0934 * @param signers
  • 0935 * The signers for the class
  • 0936 *
  • 0937 * @since 1.1
  • 0938 */
  • 0939 protected final void setSigners(Class<?> c, Object[] signers) {
  • 0940 check();
  • 0941 c.setSigners(signers);
  • 0942 }
  • 0943
  • 0944
  • 0945 // -- Resource --
  • 0946
  • 0947 /**
  • 0948 * Finds the resource with the given name. A resource is some data
  • 0949 * (images, audio, text, etc) that can be accessed by class code in a way
  • 0950 * that is independent of the location of the code.
  • 0951 *
  • 0952 * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
  • 0953 * identifies the resource.
  • 0954 *
  • 0955 * <p> This method will first search the parent class loader for the
  • 0956 * resource; if the parent is <tt>null</tt> the path of the class loader
  • 0957 * built-in to the virtual machine is searched. That failing, this method
  • 0958 * will invoke {@link #findResource(String)} to find the resource. </p>
  • 0959 *
  • 0960 * @param name
  • 0961 * The resource name
  • 0962 *
  • 0963 * @return A <tt>URL</tt> object for reading the resource, or
  • 0964 * <tt>null</tt> if the resource could not be found or the invoker
  • 0965 * doesn't have adequate privileges to get the resource.
  • 0966 *
  • 0967 * @since 1.1
  • 0968 */
  • 0969 public URL getResource(String name) {
  • 0970 URL url;
  • 0971 if (parent != null) {
  • 0972 url = parent.getResource(name);
  • 0973 } else {
  • 0974 url = getBootstrapResource(name);
  • 0975 }
  • 0976 if (url == null) {
  • 0977 url = findResource(name);
  • 0978 }
  • 0979 return url;
  • 0980 }
  • 0981
  • 0982 /**
  • 0983 * Finds all the resources with the given name. A resource is some data
  • 0984 * (images, audio, text, etc) that can be accessed by class code in a way
  • 0985 * that is independent of the location of the code.
  • 0986 *
  • 0987 * <p>The name of a resource is a <tt>/</tt>-separated path name that
  • 0988 * identifies the resource.
  • 0989 *
  • 0990 * <p> The search order is described in the documentation for {@link
  • 0991 * #getResource(String)}. </p>
  • 0992 *
  • 0993 * @param name
  • 0994 * The resource name
  • 0995 *
  • 0996 * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  • 0997 * the resource. If no resources could be found, the enumeration
  • 0998 * will be empty. Resources that the class loader doesn't have
  • 0999 * access to will not be in the enumeration.
  • 1000 *
  • 1001 * @throws IOException
  • 1002 * If I/O errors occur
  • 1003 *
  • 1004 * @see #findResources(String)
  • 1005 *
  • 1006 * @since 1.2
  • 1007 */
  • 1008 public Enumeration<URL> getResources(String name) throws IOException {
  • 1009 Enumeration[] tmp = new Enumeration[2];
  • 1010 if (parent != null) {
  • 1011 tmp[0] = parent.getResources(name);
  • 1012 } else {
  • 1013 tmp[0] = getBootstrapResources(name);
  • 1014 }
  • 1015 tmp[1] = findResources(name);
  • 1016
  • 1017 return new CompoundEnumeration(tmp);
  • 1018 }
  • 1019
  • 1020 /**
  • 1021 * Finds the resource with the given name. Class loader implementations
  • 1022 * should override this method to specify where to find resources. </p>
  • 1023 *
  • 1024 * @param name
  • 1025 * The resource name
  • 1026 *
  • 1027 * @return A <tt>URL</tt> object for reading the resource, or
  • 1028 * <tt>null</tt> if the resource could not be found
  • 1029 *
  • 1030 * @since 1.2
  • 1031 */
  • 1032 protected URL findResource(String name) {
  • 1033 return null;
  • 1034 }
  • 1035
  • 1036 /**
  • 1037 * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
  • 1038 * representing all the resources with the given name. Class loader
  • 1039 * implementations should override this method to specify where to load
  • 1040 * resources from. </p>
  • 1041 *
  • 1042 * @param name
  • 1043 * The resource name
  • 1044 *
  • 1045 * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  • 1046 * the resources
  • 1047 *
  • 1048 * @throws IOException
  • 1049 * If I/O errors occur
  • 1050 *
  • 1051 * @since 1.2
  • 1052 */
  • 1053 protected Enumeration<URL> findResources(String name) throws IOException {
  • 1054 return new CompoundEnumeration(new Enumeration[0]);
  • 1055 }
  • 1056
  • 1057 /**
  • 1058 * Find a resource of the specified name from the search path used to load
  • 1059 * classes. This method locates the resource through the system class
  • 1060 * loader (see {@link #getSystemClassLoader()}). </p>
  • 1061 *
  • 1062 * @param name
  • 1063 * The resource name
  • 1064 *
  • 1065 * @return A {@link java.net.URL <tt>URL</tt>} object for reading the
  • 1066 * resource, or <tt>null</tt> if the resource could not be found
  • 1067 *
  • 1068 * @since 1.1
  • 1069 */
  • 1070 public static URL getSystemResource(String name) {
  • 1071 ClassLoader system = getSystemClassLoader();
  • 1072 if (system == null) {
  • 1073 return getBootstrapResource(name);
  • 1074 }
  • 1075 return system.getResource(name);
  • 1076 }
  • 1077
  • 1078 /**
  • 1079 * Finds all resources of the specified name from the search path used to
  • 1080 * load classes. The resources thus found are returned as an
  • 1081 * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
  • 1082 * java.net.URL <tt>URL</tt>} objects.
  • 1083 *
  • 1084 * <p> The search order is described in the documentation for {@link
  • 1085 * #getSystemResource(String)}. </p>
  • 1086 *
  • 1087 * @param name
  • 1088 * The resource name
  • 1089 *
  • 1090 * @return An enumeration of resource {@link java.net.URL <tt>URL</tt>}
  • 1091 * objects
  • 1092 *
  • 1093 * @throws IOException
  • 1094 * If I/O errors occur
  • 1095
  • 1096 * @since 1.2
  • 1097 */
  • 1098 public static Enumeration<URL> getSystemResources(String name)
  • 1099 throws IOException
  • 1100 {
  • 1101 ClassLoader system = getSystemClassLoader();
  • 1102 if (system == null) {
  • 1103 return getBootstrapResources(name);
  • 1104 }
  • 1105 return system.getResources(name);
  • 1106 }
  • 1107
  • 1108 /**
  • 1109 * Find resources from the VM's built-in classloader.
  • 1110 */
  • 1111 private static URL getBootstrapResource(String name) {
  • 1112 URLClassPath ucp = getBootstrapClassPath();
  • 1113 Resource res = ucp.getResource(name);
  • 1114 return res != null ? res.getURL() : null;
  • 1115 }
  • 1116
  • 1117 /**
  • 1118 * Find resources from the VM's built-in classloader.
  • 1119 */
  • 1120 private static Enumeration getBootstrapResources(String name)
  • 1121 throws IOException
  • 1122 {
  • 1123 final Enumeration e = getBootstrapClassPath().getResources(name);
  • 1124 return new Enumeration () {
  • 1125 public Object nextElement() {
  • 1126 return ((Resource)e.nextElement()).getURL();
  • 1127 }
  • 1128 public boolean hasMoreElements() {
  • 1129 return e.hasMoreElements();
  • 1130 }
  • 1131 };
  • 1132 }
  • 1133
  • 1134 // Returns the URLClassPath that is used for finding system resources.
  • 1135 static URLClassPath getBootstrapClassPath() {
  • 1136 if (bootstrapClassPath == null) {
  • 1137 bootstrapClassPath = sun.misc.Launcher.getBootstrapClassPath();
  • 1138 }
  • 1139 return bootstrapClassPath;
  • 1140 }
  • 1141
  • 1142 private static URLClassPath bootstrapClassPath;
  • 1143
  • 1144 /**
  • 1145 * Returns an input stream for reading the specified resource.
  • 1146 *
  • 1147 * <p> The search order is described in the documentation for {@link
  • 1148 * #getResource(String)}. </p>
  • 1149 *
  • 1150 * @param name
  • 1151 * The resource name
  • 1152 *
  • 1153 * @return An input stream for reading the resource, or <tt>null</tt>
  • 1154 * if the resource could not be found
  • 1155 *
  • 1156 * @since 1.1
  • 1157 */
  • 1158 public InputStream getResourceAsStream(String name) {
  • 1159 URL url = getResource(name);
  • 1160 try {
  • 1161 return url != null ? url.openStream() : null;
  • 1162 } catch (IOException e) {
  • 1163 return null;
  • 1164 }
  • 1165 }
  • 1166
  • 1167 /**
  • 1168 * Open for reading, a resource of the specified name from the search path
  • 1169 * used to load classes. This method locates the resource through the
  • 1170 * system class loader (see {@link #getSystemClassLoader()}). </p>
  • 1171 *
  • 1172 * @param name
  • 1173 * The resource name
  • 1174 *
  • 1175 * @return An input stream for reading the resource, or <tt>null</tt>
  • 1176 * if the resource could not be found
  • 1177 *
  • 1178 * @since 1.1
  • 1179 */
  • 1180 public static InputStream getSystemResourceAsStream(String name) {
  • 1181 URL url = getSystemResource(name);
  • 1182 try {
  • 1183 return url != null ? url.openStream() : null;
  • 1184 } catch (IOException e) {
  • 1185 return null;
  • 1186 }
  • 1187 }
  • 1188
  • 1189
  • 1190 // -- Hierarchy --
  • 1191
  • 1192 /**
  • 1193 * Returns the parent class loader for delegation. Some implementations may
  • 1194 * use <tt>null</tt> to represent the bootstrap class loader. This method
  • 1195 * will return <tt>null</tt> in such implementations if this class loader's
  • 1196 * parent is the bootstrap class loader.
  • 1197 *
  • 1198 * <p> If a security manager is present, and the invoker's class loader is
  • 1199 * not <tt>null</tt> and is not an ancestor of this class loader, then this
  • 1200 * method invokes the security manager's {@link
  • 1201 * SecurityManager#checkPermission(java.security.Permission)
  • 1202 * <tt>checkPermission</tt>} method with a {@link
  • 1203 * RuntimePermission#RuntimePermission(String)
  • 1204 * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
  • 1205 * access to the parent class loader is permitted. If not, a
  • 1206 * <tt>SecurityException</tt> will be thrown. </p>
  • 1207 *
  • 1208 * @return The parent <tt>ClassLoader</tt>
  • 1209 *
  • 1210 * @throws SecurityException
  • 1211 * If a security manager exists and its <tt>checkPermission</tt>
  • 1212 * method doesn't allow access to this class loader's parent class
  • 1213 * loader.
  • 1214 *
  • 1215 * @since 1.2
  • 1216 */
  • 1217 public final ClassLoader getParent() {
  • 1218 if (parent == null)
  • 1219 return null;
  • 1220 SecurityManager sm = System.getSecurityManager();
  • 1221 if (sm != null) {
  • 1222 ClassLoader ccl = getCallerClassLoader();
  • 1223 if (ccl != null && !isAncestor(ccl)) {
  • 1224 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
  • 1225 }
  • 1226 }
  • 1227 return parent;
  • 1228 }
  • 1229
  • 1230 /**
  • 1231 * Returns the system class loader for delegation. This is the default
  • 1232 * delegation parent for new <tt>ClassLoader</tt> instances, and is
  • 1233 * typically the class loader used to start the application.
  • 1234 *
  • 1235 * <p> This method is first invoked early in the runtime's startup
  • 1236 * sequence, at which point it creates the system class loader and sets it
  • 1237 * as the context class loader of the invoking <tt>Thread</tt>.
  • 1238 *
  • 1239 * <p> The default system class loader is an implementation-dependent
  • 1240 * instance of this class.
  • 1241 *
  • 1242 * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
  • 1243 * when this method is first invoked then the value of that property is
  • 1244 * taken to be the name of a class that will be returned as the system
  • 1245 * class loader. The class is loaded using the default system class loader
  • 1246 * and must define a public constructor that takes a single parameter of
  • 1247 * type <tt>ClassLoader</tt> which is used as the delegation parent. An
  • 1248 * instance is then created using this constructor with the default system
  • 1249 * class loader as the parameter. The resulting class loader is defined
  • 1250 * to be the system class loader.
  • 1251 *
  • 1252 * <p> If a security manager is present, and the invoker's class loader is
  • 1253 * not <tt>null</tt> and the invoker's class loader is not the same as or
  • 1254 * an ancestor of the system class loader, then this method invokes the
  • 1255 * security manager's {@link
  • 1256 * SecurityManager#checkPermission(java.security.Permission)
  • 1257 * <tt>checkPermission</tt>} method with a {@link
  • 1258 * RuntimePermission#RuntimePermission(String)
  • 1259 * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
  • 1260 * access to the system class loader. If not, a
  • 1261 * <tt>SecurityException</tt> will be thrown. </p>
  • 1262 *
  • 1263 * @return The system <tt>ClassLoader</tt> for delegation, or
  • 1264 * <tt>null</tt> if none
  • 1265 *
  • 1266 * @throws SecurityException
  • 1267 * If a security manager exists and its <tt>checkPermission</tt>
  • 1268 * method doesn't allow access to the system class loader.
  • 1269 *
  • 1270 * @throws IllegalStateException
  • 1271 * If invoked recursively during the construction of the class
  • 1272 * loader specified by the "<tt>java.system.class.loader</tt>"
  • 1273 * property.
  • 1274 *
  • 1275 * @throws Error
  • 1276 * If the system property "<tt>java.system.class.loader</tt>"
  • 1277 * is defined but the named class could not be loaded, the
  • 1278 * provider class does not define the required constructor, or an
  • 1279 * exception is thrown by that constructor when it is invoked. The
  • 1280 * underlying cause of the error can be retrieved via the
  • 1281 * {@link Throwable#getCause()} method.
  • 1282 *
  • 1283 * @revised 1.4
  • 1284 */
  • 1285 public static ClassLoader getSystemClassLoader() {
  • 1286 initSystemClassLoader();
  • 1287 if (scl == null) {
  • 1288 return null;
  • 1289 }
  • 1290 SecurityManager sm = System.getSecurityManager();
  • 1291 if (sm != null) {
  • 1292 ClassLoader ccl = getCallerClassLoader();
  • 1293 if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) {
  • 1294 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
  • 1295 }
  • 1296 }
  • 1297 return scl;
  • 1298 }
  • 1299
  • 1300 private static synchronized void initSystemClassLoader() {
  • 1301 if (!sclSet) {
  • 1302 if (scl != null)
  • 1303 throw new IllegalStateException("recursive invocation");
  • 1304 sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
  • 1305 if (l != null) {
  • 1306 Throwable oops = null;
  • 1307 scl = l.getClassLoader();
  • 1308 try {
  • 1309 PrivilegedExceptionAction a;
  • 1310 a = new SystemClassLoaderAction(scl);
  • 1311 scl = (ClassLoader) AccessController.doPrivileged(a);
  • 1312 } catch (PrivilegedActionException pae) {
  • 1313 oops = pae.getCause();
  • 1314 if (oops instanceof InvocationTargetException) {
  • 1315 oops = oops.getCause();
  • 1316 }
  • 1317 }
  • 1318 if (oops != null) {
  • 1319 if (oops instanceof Error) {
  • 1320 throw (Error) oops;
  • 1321 } else {
  • 1322 // wrap the exception
  • 1323 throw new Error(oops);
  • 1324 }
  • 1325 }
  • 1326 }
  • 1327 sclSet = true;
  • 1328 }
  • 1329 }
  • 1330
  • 1331 // Returns true if the specified class loader can be found in this class
  • 1332 // loader's delegation chain.
  • 1333 boolean isAncestor(ClassLoader cl) {
  • 1334 ClassLoader acl = this;
  • 1335 do {
  • 1336 acl = acl.parent;
  • 1337 if (cl == acl) {
  • 1338 return true;
  • 1339 }
  • 1340 } while (acl != null);
  • 1341 return false;
  • 1342 }
  • 1343
  • 1344 // Returns the invoker's class loader, or null if none.
  • 1345 // NOTE: This must always be invoked when there is exactly one intervening
  • 1346 // frame from the core libraries on the stack between this method's
  • 1347 // invocation and the desired invoker.
  • 1348 static ClassLoader getCallerClassLoader() {
  • 1349 // NOTE use of more generic Reflection.getCallerClass()
  • 1350 Class caller = Reflection.getCallerClass(3);
  • 1351 // This can be null if the VM is requesting it
  • 1352 if (caller == null) {
  • 1353 return null;
  • 1354 }
  • 1355 // Circumvent security check since this is package-private
  • 1356 return caller.getClassLoader0();
  • 1357 }
  • 1358
  • 1359 // The class loader for the system
  • 1360 private static ClassLoader scl;
  • 1361
  • 1362 // Set to true once the system class loader has been set
  • 1363 private static boolean sclSet;
  • 1364
  • 1365
  • 1366 // -- Package --
  • 1367
  • 1368 /**
  • 1369 * Defines a package by name in this <tt>ClassLoader</tt>. This allows
  • 1370 * class loaders to define the packages for their classes. Packages must
  • 1371 * be created before the class is defined, and package names must be
  • 1372 * unique within a class loader and cannot be redefined or changed once
  • 1373 * created. </p>
  • 1374 *
  • 1375 * @param name
  • 1376 * The package name
  • 1377 *
  • 1378 * @param specTitle
  • 1379 * The specification title
  • 1380 *
  • 1381 * @param specVersion
  • 1382 * The specification version
  • 1383 *
  • 1384 * @param specVendor
  • 1385 * The specification vendor
  • 1386 *
  • 1387 * @param implTitle
  • 1388 * The implementation title
  • 1389 *
  • 1390 * @param implVersion
  • 1391 * The implementation version
  • 1392 *
  • 1393 * @param implVendor
  • 1394 * The implementation vendor
  • 1395 *
  • 1396 * @param sealBase
  • 1397 * If not <tt>null</tt>, then this package is sealed with
  • 1398 * respect to the given code source {@link java.net.URL
  • 1399 * <tt>URL</tt>} object. Otherwise, the package is not sealed.
  • 1400 *
  • 1401 * @return The newly defined <tt>Package</tt> object
  • 1402 *
  • 1403 * @throws IllegalArgumentException
  • 1404 * If package name duplicates an existing package either in this
  • 1405 * class loader or one of its ancestors
  • 1406 *
  • 1407 * @since 1.2
  • 1408 */
  • 1409 protected Package definePackage(String name, String specTitle,
  • 1410 String specVersion, String specVendor,
  • 1411 String implTitle, String implVersion,
  • 1412 String implVendor, URL sealBase)
  • 1413 throws IllegalArgumentException
  • 1414 {
  • 1415 synchronized (packages) {
  • 1416 Package pkg = getPackage(name);
  • 1417 if (pkg != null) {
  • 1418 throw new IllegalArgumentException(name);
  • 1419 }
  • 1420 pkg = new Package(name, specTitle, specVersion, specVendor,
  • 1421 implTitle, implVersion, implVendor,
  • 1422 sealBase, this);
  • 1423 packages.put(name, pkg);
  • 1424 return pkg;
  • 1425 }
  • 1426 }
  • 1427
  • 1428 /**
  • 1429 * Returns a <tt>Package</tt> that has been defined by this class loader
  • 1430 * or any of its ancestors. </p>
  • 1431 *
  • 1432 * @param name
  • 1433 * The package name
  • 1434 *
  • 1435 * @return The <tt>Package</tt> corresponding to the given name, or
  • 1436 * <tt>null</tt> if not found
  • 1437 *
  • 1438 * @since 1.2
  • 1439 */
  • 1440 protected Package getPackage(String name) {
  • 1441 synchronized (packages) {
  • 1442 Package pkg = (Package)packages.get(name);
  • 1443 if (pkg == null) {
  • 1444 if (parent != null) {
  • 1445 pkg = parent.getPackage(name);
  • 1446 } else {
  • 1447 pkg = Package.getSystemPackage(name);
  • 1448 }
  • 1449 if (pkg != null) {
  • 1450 packages.put(name, pkg);
  • 1451 }
  • 1452 }
  • 1453 return pkg;
  • 1454 }
  • 1455 }
  • 1456
  • 1457 /**
  • 1458 * Returns all of the <tt>Packages</tt> defined by this class loader and
  • 1459 * its ancestors. </p>
  • 1460 *
  • 1461 * @return The array of <tt>Package</tt> objects defined by this
  • 1462 * <tt>ClassLoader</tt>
  • 1463 *
  • 1464 * @since 1.2
  • 1465 */
  • 1466 protected Package[] getPackages() {
  • 1467 Map map;
  • 1468 synchronized (packages) {
  • 1469 map = (Map)packages.clone();
  • 1470 }
  • 1471 Package[] pkgs;
  • 1472 if (parent != null) {
  • 1473 pkgs = parent.getPackages();
  • 1474 } else {
  • 1475 pkgs = Package.getSystemPackages();
  • 1476 }
  • 1477 if (pkgs != null) {
  • 1478 for (int i = 0; i < pkgs.length; i++) {
  • 1479 String pkgName = pkgs[i].getName();
  • 1480 if (map.get(pkgName) == null) {
  • 1481 map.put(pkgName, pkgs[i]);
  • 1482 }
  • 1483 }
  • 1484 }
  • 1485 return (Package[])map.values().toArray(new Package[map.size()]);
  • 1486 }
  • 1487
  • 1488
  • 1489 // -- Native library access --
  • 1490
  • 1491 /**
  • 1492 * Returns the absolute path name of a native library. The VM invokes this
  • 1493 * method to locate the native libraries that belong to classes loaded with
  • 1494 * this class loader. If this method returns <tt>null</tt>, the VM
  • 1495 * searches the library along the path specified as the
  • 1496 * "<tt>java.library.path</tt>" property. </p>
  • 1497 *
  • 1498 * @param libname
  • 1499 * The library name
  • 1500 *
  • 1501 * @return The absolute path of the native library
  • 1502 *
  • 1503 * @see System#loadLibrary(String)
  • 1504 * @see System#mapLibraryName(String)
  • 1505 *
  • 1506 * @since 1.2
  • 1507 */
  • 1508 protected String findLibrary(String libname) {
  • 1509 return null;
  • 1510 }
  • 1511
  • 1512 /**
  • 1513 * The inner class NativeLibrary denotes a loaded native library instance.
  • 1514 * Every classloader contains a vector of loaded native libraries in the
  • 1515 * private field <tt>nativeLibraries</tt>. The native libraries loaded
  • 1516 * into the system are entered into the <tt>systemNativeLibraries</tt>
  • 1517 * vector.
  • 1518 *
  • 1519 * <p> Every native library requires a particular version of JNI. This is
  • 1520 * denoted by the private <tt>jniVersion</tt> field. This field is set by
  • 1521 * the VM when it loads the library, and used by the VM to pass the correct
  • 1522 * version of JNI to the native methods. </p>
  • 1523 *
  • 1524 * @version 1.189 11/17/05
  • 1525 * @see ClassLoader
  • 1526 * @since 1.2
  • 1527 */
  • 1528 static class NativeLibrary {
  • 1529 // opaque handle to native library, used in native code.
  • 1530 long handle;
  • 1531 // the version of JNI environment the native library requires.
  • 1532 private int jniVersion;
  • 1533 // the class from which the library is loaded, also indicates
  • 1534 // the loader this native library belongs.
  • 1535 private Class fromClass;
  • 1536 // the canonicalized name of the native library.
  • 1537 String name;
  • 1538
  • 1539 native void load(String name);
  • 1540 native long find(String name);
  • 1541 native void unload();
  • 1542
  • 1543 public NativeLibrary(Class fromClass, String name) {
  • 1544 this.name = name;
  • 1545 this.fromClass = fromClass;
  • 1546 }
  • 1547
  • 1548 protected void finalize() {
  • 1549 synchronized (loadedLibraryNames) {
  • 1550 if (fromClass.getClassLoader() != null && handle != 0) {
  • 1551 /* remove the native library name */
  • 1552 int size = loadedLibraryNames.size();
  • 1553 for (int i = 0; i < size; i++) {
  • 1554 if (name.equals(loadedLibraryNames.elementAt(i))) {
  • 1555 loadedLibraryNames.removeElementAt(i);
  • 1556 break;
  • 1557 }
  • 1558 }
  • 1559 /* unload the library. */
  • 1560 ClassLoader.nativeLibraryContext.push(this);
  • 1561 try {
  • 1562 unload();
  • 1563 } finally {
  • 1564 ClassLoader.nativeLibraryContext.pop();
  • 1565 }
  • 1566 }
  • 1567 }
  • 1568 }
  • 1569 // Invoked in the VM to determine the context class in
  • 1570 // JNI_Load/JNI_Unload
  • 1571 static Class getFromClass() {
  • 1572 return ((NativeLibrary)
  • 1573 (ClassLoader.nativeLibraryContext.peek())).fromClass;
  • 1574 }
  • 1575 }
  • 1576
  • 1577 // The "default" domain. Set as the default ProtectionDomain on newly
  • 1578 // created classes.
  • 1579 private ProtectionDomain defaultDomain = null;
  • 1580
  • 1581 // Returns (and initializes) the default domain.
  • 1582 private synchronized ProtectionDomain getDefaultDomain() {
  • 1583 if (defaultDomain == null) {
  • 1584 CodeSource cs =
  • 1585 new CodeSource(null, (java.security.cert.Certificate[]) null);
  • 1586 defaultDomain = new ProtectionDomain(cs, null, this, null);
  • 1587 }
  • 1588 return defaultDomain;
  • 1589 }
  • 1590
  • 1591 // All native library names we've loaded.
  • 1592 private static Vector loadedLibraryNames = new Vector();
  • 1593 // Native libraries belonging to system classes.
  • 1594 private static Vector systemNativeLibraries = new Vector();
  • 1595 // Native libraries associated with the class loader.
  • 1596 private Vector nativeLibraries = new Vector();
  • 1597
  • 1598 // native libraries being loaded/unloaded.
  • 1599 private static Stack nativeLibraryContext = new Stack();
  • 1600
  • 1601 // The paths searched for libraries
  • 1602 static private String usr_paths[];
  • 1603 static private String sys_paths[];
  • 1604
  • 1605 private static String[] initializePath(String propname) {
  • 1606 String ldpath = System.getProperty(propname, "");
  • 1607 String ps = File.pathSeparator;
  • 1608 int ldlen = ldpath.length();
  • 1609 int i, j, n;
  • 1610 // Count the separators in the path
  • 1611 i = ldpath.indexOf(ps);
  • 1612 n = 0;
  • 1613 while (i >= 0) {
  • 1614 n++;
  • 1615 i = ldpath.indexOf(ps, i + 1);
  • 1616 }
  • 1617
  • 1618 // allocate the array of paths - n :'s = n + 1 path elements
  • 1619 String[] paths = new String[n + 1];
  • 1620
  • 1621 // Fill the array with paths from the ldpath
  • 1622 n = i = 0;
  • 1623 j = ldpath.indexOf(ps);
  • 1624 while (j >= 0) {
  • 1625 if (j - i > 0) {
  • 1626 paths[n++] = ldpath.substring(i, j);
  • 1627 } else if (j - i == 0) {
  • 1628 paths[n++] = ".";
  • 1629 }
  • 1630 i = j + 1;
  • 1631 j = ldpath.indexOf(ps, i);
  • 1632 }
  • 1633 paths[n] = ldpath.substring(i, ldlen);
  • 1634 return paths;
  • 1635 }
  • 1636
  • 1637 // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
  • 1638 static void loadLibrary(Class fromClass, String name,
  • 1639 boolean isAbsolute) {
  • 1640 ClassLoader loader =
  • 1641 (fromClass == null) ? null : fromClass.getClassLoader();
  • 1642 if (sys_paths == null) {
  • 1643 usr_paths = initializePath("java.library.path");
  • 1644 sys_paths = initializePath("sun.boot.library.path");
  • 1645 }
  • 1646 if (isAbsolute) {
  • 1647 if (loadLibrary0(fromClass, new File(name))) {
  • 1648 return;
  • 1649 }
  • 1650 throw new UnsatisfiedLinkError("Can't load library: " + name);
  • 1651 }
  • 1652 if (loader != null) {
  • 1653 String libfilename = loader.findLibrary(name);
  • 1654 if (libfilename != null) {
  • 1655 File libfile = new File(libfilename);
  • 1656 if (!libfile.isAbsolute()) {
  • 1657 throw new UnsatisfiedLinkError(
  • 1658 "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
  • 1659 }
  • 1660 if (loadLibrary0(fromClass, libfile)) {
  • 1661 return;
  • 1662 }
  • 1663 throw new UnsatisfiedLinkError("Can't load " + libfilename);
  • 1664 }
  • 1665 }
  • 1666 for (int i = 0 ; i < sys_paths.length ; i++) {
  • 1667 File libfile = new File(sys_paths[i], System.mapLibraryName(name));
  • 1668 if (loadLibrary0(fromClass, libfile)) {
  • 1669 return;
  • 1670 }
  • 1671 }
  • 1672 if (loader != null) {
  • 1673 for (int i = 0 ; i < usr_paths.length ; i++) {
  • 1674 File libfile = new File(usr_paths[i],
  • 1675 System.mapLibraryName(name));
  • 1676 if (loadLibrary0(fromClass, libfile)) {
  • 1677 return;
  • 1678 }
  • 1679 }
  • 1680 }
  • 1681 // Oops, it failed
  • 1682 throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
  • 1683 }
  • 1684
  • 1685 private static boolean loadLibrary0(Class fromClass, final File file) {
  • 1686 Boolean exists = (Boolean)
  • 1687 AccessController.doPrivileged(new PrivilegedAction() {
  • 1688 public Object run() {
  • 1689 return new Boolean(file.exists());
  • 1690 }
  • 1691 });
  • 1692 if (!exists.booleanValue()) {
  • 1693 return false;
  • 1694 }
  • 1695 String name;
  • 1696 try {
  • 1697 name = file.getCanonicalPath();
  • 1698 } catch (IOException e) {
  • 1699 return false;
  • 1700 }
  • 1701 ClassLoader loader =
  • 1702 (fromClass == null) ? null : fromClass.getClassLoader();
  • 1703 Vector libs =
  • 1704 loader != null ? loader.nativeLibraries : systemNativeLibraries;
  • 1705 synchronized (libs) {
  • 1706 int size = libs.size();
  • 1707 for (int i = 0; i < size; i++) {
  • 1708 NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
  • 1709 if (name.equals(lib.name)) {
  • 1710 return true;
  • 1711 }
  • 1712 }
  • 1713
  • 1714 synchronized (loadedLibraryNames) {
  • 1715 if (loadedLibraryNames.contains(name)) {
  • 1716 throw new UnsatisfiedLinkError
  • 1717 ("Native Library " +
  • 1718 name +
  • 1719 " already loaded in another classloader");
  • 1720 }
  • 1721 /* If the library is being loaded (must be by the same thread,
  • 1722 * because Runtime.load and Runtime.loadLibrary are
  • 1723 * synchronous). The reason is can occur is that the JNI_OnLoad
  • 1724 * function can cause another loadLibrary invocation.
  • 1725 *
  • 1726 * Thus we can use a static stack to hold the list of libraries
  • 1727 * we are loading.
  • 1728 *
  • 1729 * If there is a pending load operation for the library, we
  • 1730 * immediately return success; otherwise, we raise
  • 1731 * UnsatisfiedLinkError.
  • 1732 */
  • 1733 int n = nativeLibraryContext.size();
  • 1734 for (int i = 0; i < n; i++) {
  • 1735 NativeLibrary lib = (NativeLibrary)
  • 1736 nativeLibraryContext.elementAt(i);
  • 1737 if (name.equals(lib.name)) {
  • 1738 if (loader == lib.fromClass.getClassLoader()) {
  • 1739 return true;
  • 1740 } else {
  • 1741 throw new UnsatisfiedLinkError
  • 1742 ("Native Library " +
  • 1743 name +
  • 1744 " is being loaded in another classloader");
  • 1745 }
  • 1746 }
  • 1747 }
  • 1748 NativeLibrary lib = new NativeLibrary(fromClass, name);
  • 1749 nativeLibraryContext.push(lib);
  • 1750 try {
  • 1751 lib.load(name);
  • 1752 } finally {
  • 1753 nativeLibraryContext.pop();
  • 1754 }
  • 1755 if (lib.handle != 0) {
  • 1756 loadedLibraryNames.addElement(name);
  • 1757 libs.addElement(lib);
  • 1758 return true;
  • 1759 }
  • 1760 return false;
  • 1761 }
  • 1762 }
  • 1763 }
  • 1764
  • 1765 // Invoked in the VM class linking code.
  • 1766 static long findNative(ClassLoader loader, String name) {
  • 1767 Vector libs =
  • 1768 loader != null ? loader.nativeLibraries : systemNativeLibraries;
  • 1769 synchronized (libs) {
  • 1770 int size = libs.size();
  • 1771 for (int i = 0; i < size; i++) {
  • 1772 NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
  • 1773 long entry = lib.find(name);
  • 1774 if (entry != 0)
  • 1775 return entry;
  • 1776 }
  • 1777 }
  • 1778 return 0;
  • 1779 }
  • 1780
  • 1781
  • 1782 // -- Assertion management --
  • 1783
  • 1784 // The default toggle for assertion checking.
  • 1785 private boolean defaultAssertionStatus = false;
  • 1786
  • 1787 // Maps String packageName to Boolean package default assertion status Note
  • 1788 // that the default package is placed under a null map key. If this field
  • 1789 // is null then we are delegating assertion status queries to the VM, i.e.,
  • 1790 // none of this ClassLoader's assertion status modification methods have
  • 1791 // been invoked.
  • 1792 private Map packageAssertionStatus = null;
  • 1793
  • 1794 // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
  • 1795 // field is null then we are delegating assertion status queries to the VM,
  • 1796 // i.e., none of this ClassLoader's assertion status modification methods
  • 1797 // have been invoked.
  • 1798 Map classAssertionStatus = null;
  • 1799
  • 1800 /**
  • 1801 * Sets the default assertion status for this class loader. This setting
  • 1802 * determines whether classes loaded by this class loader and initialized
  • 1803 * in the future will have assertions enabled or disabled by default.
  • 1804 * This setting may be overridden on a per-package or per-class basis by
  • 1805 * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
  • 1806 * #setClassAssertionStatus(String, boolean)}. </p>
  • 1807 *
  • 1808 * @param enabled
  • 1809 * <tt>true</tt> if classes loaded by this class loader will
  • 1810 * henceforth have assertions enabled by default, <tt>false</tt>
  • 1811 * if they will have assertions disabled by default.
  • 1812 *
  • 1813 * @since 1.4
  • 1814 */
  • 1815 public synchronized void setDefaultAssertionStatus(boolean enabled) {
  • 1816 if (classAssertionStatus == null)
  • 1817 initializeJavaAssertionMaps();
  • 1818
  • 1819 defaultAssertionStatus = enabled;
  • 1820 }
  • 1821
  • 1822 /**
  • 1823 * Sets the package default assertion status for the named package. The
  • 1824 * package default assertion status determines the assertion status for
  • 1825 * classes initialized in the future that belong to the named package or
  • 1826 * any of its "subpackages".
  • 1827 *
  • 1828 * <p> A subpackage of a package named p is any package whose name begins
  • 1829 * with "<tt>p.</tt>". For example, <tt>javax.swing.text</tt> is a
  • 1830 * subpackage of <tt>javax.swing</tt>, and both <tt>java.util</tt> and
  • 1831 * <tt>java.lang.reflect</tt> are subpackages of <tt>java</tt>.
  • 1832 *
  • 1833 * <p> In the event that multiple package defaults apply to a given class,
  • 1834 * the package default pertaining to the most specific package takes
  • 1835 * precedence over the others. For example, if <tt>javax.lang</tt> and
  • 1836 * <tt>javax.lang.reflect</tt> both have package defaults associated with
  • 1837 * them, the latter package default applies to classes in
  • 1838 * <tt>javax.lang.reflect</tt>.
  • 1839 *
  • 1840 * <p> Package defaults take precedence over the class loader's default
  • 1841 * assertion status, and may be overridden on a per-class basis by invoking
  • 1842 * {@link #setClassAssertionStatus(String, boolean)}. </p>
  • 1843 *
  • 1844 * @param packageName
  • 1845 * The name of the package whose package default assertion status
  • 1846 * is to be set. A <tt>null</tt> value indicates the unnamed
  • 1847 * package that is "current"
  • 1848 * (<a href="http://java.sun.com/docs/books/jls/">Java Language
  • 1849 * Specification</a>, section 7.4.2).
  • 1850 *
  • 1851 * @param enabled
  • 1852 * <tt>true</tt> if classes loaded by this classloader and
  • 1853 * belonging to the named package or any of its subpackages will
  • 1854 * have assertions enabled by default, <tt>false</tt> if they will
  • 1855 * have assertions disabled by default.
  • 1856 *
  • 1857 * @since 1.4
  • 1858 */
  • 1859 public synchronized void setPackageAssertionStatus(String packageName,
  • 1860 boolean enabled)
  • 1861 {
  • 1862 if (packageAssertionStatus == null)
  • 1863 initializeJavaAssertionMaps();
  • 1864
  • 1865 packageAssertionStatus.put(packageName, Boolean.valueOf(enabled));
  • 1866 }
  • 1867
  • 1868 /**
  • 1869 * Sets the desired assertion status for the named top-level class in this
  • 1870 * class loader and any nested classes contained therein. This setting
  • 1871 * takes precedence over the class loader's default assertion status, and
  • 1872 * over any applicable per-package default. This method has no effect if
  • 1873 * the named class has already been initialized. (Once a class is
  • 1874 * initialized, its assertion status cannot change.)
  • 1875 *
  • 1876 * <p> If the named class is not a top-level class, this invocation will
  • 1877 * have no effect on the actual assertion status of any class. </p>
  • 1878 *
  • 1879 * @param className
  • 1880 * The fully qualified class name of the top-level class whose
  • 1881 * assertion status is to be set.
  • 1882 *
  • 1883 * @param enabled
  • 1884 * <tt>true</tt> if the named class is to have assertions
  • 1885 * enabled when (and if) it is initialized, <tt>false</tt> if the
  • 1886 * class is to have assertions disabled.
  • 1887 *
  • 1888 * @since 1.4
  • 1889 */
  • 1890 public synchronized void setClassAssertionStatus(String className,
  • 1891 boolean enabled)
  • 1892 {
  • 1893 if (classAssertionStatus == null)
  • 1894 initializeJavaAssertionMaps();
  • 1895
  • 1896 classAssertionStatus.put(className, Boolean.valueOf(enabled));
  • 1897 }
  • 1898
  • 1899 /**
  • 1900 * Sets the default assertion status for this class loader to
  • 1901 * <tt>false</tt> and discards any package defaults or class assertion
  • 1902 * status settings associated with the class loader. This method is
  • 1903 * provided so that class loaders can be made to ignore any command line or
  • 1904 * persistent assertion status settings and "start with a clean slate."
  • 1905 * </p>
  • 1906 *
  • 1907 * @since 1.4
  • 1908 */
  • 1909 public synchronized void clearAssertionStatus() {
  • 1910 /*
  • 1911 * Whether or not "Java assertion maps" are initialized, set
  • 1912 * them to empty maps, effectively ignoring any present settings.
  • 1913 */
  • 1914 classAssertionStatus = new HashMap();
  • 1915 packageAssertionStatus = new HashMap();
  • 1916
  • 1917 defaultAssertionStatus = false;
  • 1918 }
  • 1919
  • 1920 /**
  • 1921 * Returns the assertion status that would be assigned to the specified
  • 1922 * class if it were to be initialized at the time this method is invoked.
  • 1923 * If the named class has had its assertion status set, the most recent
  • 1924 * setting will be returned; otherwise, if any package default assertion
  • 1925 * status pertains to this class, the most recent setting for the most
  • 1926 * specific pertinent package default assertion status is returned;
  • 1927 * otherwise, this class loader's default assertion status is returned.
  • 1928 * </p>
  • 1929 *
  • 1930 * @param className
  • 1931 * The fully qualified class name of the class whose desired
  • 1932 * assertion status is being queried.
  • 1933 *
  • 1934 * @return The desired assertion status of the specified class.
  • 1935 *
  • 1936 * @see #setClassAssertionStatus(String, boolean)
  • 1937 * @see #setPackageAssertionStatus(String, boolean)
  • 1938 * @see #setDefaultAssertionStatus(boolean)
  • 1939 *
  • 1940 * @since 1.4
  • 1941 */
  • 1942 synchronized boolean desiredAssertionStatus(String className) {
  • 1943 Boolean result;
  • 1944
  • 1945 // assert classAssertionStatus != null;
  • 1946 // assert packageAssertionStatus != null;
  • 1947
  • 1948 // Check for a class entry
  • 1949 result = (Boolean)classAssertionStatus.get(className);
  • 1950 if (result != null)
  • 1951 return result.booleanValue();
  • 1952
  • 1953 // Check for most specific package entry
  • 1954 int dotIndex = className.lastIndexOf(".");
  • 1955 if (dotIndex < 0) { // default package
  • 1956 result = (Boolean)packageAssertionStatus.get(null);
  • 1957 if (result != null)
  • 1958 return result.booleanValue();
  • 1959 }
  • 1960 while(dotIndex > 0) {
  • 1961 className = className.substring(0, dotIndex);
  • 1962 result = (Boolean)packageAssertionStatus.get(className);
  • 1963 if (result != null)
  • 1964 return result.booleanValue();
  • 1965 dotIndex = className.lastIndexOf(".", dotIndex-1);
  • 1966 }
  • 1967
  • 1968 // Return the classloader default
  • 1969 return defaultAssertionStatus;
  • 1970 }
  • 1971
  • 1972 // Set up the assertions with information provided by the VM.
  • 1973 private void initializeJavaAssertionMaps() {
  • 1974 // assert Thread.holdsLock(this);
  • 1975
  • 1976 classAssertionStatus = new HashMap();
  • 1977 packageAssertionStatus = new HashMap();
  • 1978 AssertionStatusDirectives directives = retrieveDirectives();
  • 1979
  • 1980 for(int i = 0; i < directives.classes.length; i++)
  • 1981 classAssertionStatus.put(directives.classes[i],
  • 1982 Boolean.valueOf(directives.classEnabled[i]));
  • 1983
  • 1984 for(int i = 0; i < directives.packages.length; i++)
  • 1985 packageAssertionStatus.put(directives.packages[i],
  • 1986 Boolean.valueOf(directives.packageEnabled[i]));
  • 1987
  • 1988 defaultAssertionStatus = directives.deflt;
  • 1989 }
  • 1990
  • 1991 // Retrieves the assertion directives from the VM.
  • 1992 private static native AssertionStatusDirectives retrieveDirectives();
  • 1993}
  • 1994
  • 1995
  • 1996class SystemClassLoaderAction implements PrivilegedExceptionAction {
  • 1997 private ClassLoader parent;
  • 1998
  • 1999 SystemClassLoaderAction(ClassLoader parent) {
  • 2000 this.parent = parent;
  • 2001 }
  • 2002
  • 2003 public Object run() throws Exception {
  • 2004 ClassLoader sys;
  • 2005 Constructor ctor;
  • 2006 Class c;
  • 2007 Class cp[] = { ClassLoader.class };
  • 2008 Object params[] = { parent };
  • 2009
  • 2010 String cls = System.getProperty("java.system.class.loader");
  • 2011 if (cls == null) {
  • 2012 return parent;
  • 2013 }
  • 2014
  • 2015 c = Class.forName(cls, true, parent);
  • 2016 ctor = c.getDeclaredConstructor(cp);
  • 2017 sys = (ClassLoader) ctor.newInstance(params);
  • 2018 Thread.currentThread().setContextClassLoader(sys);
  • 2019 return sys;
  • 2020 }
  • 2021}

文件:ClassLoader.java
包名:java.lang
类名:ClassLoader
继承:
接口: