?? classloader.java
字號:
* 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) * @return the requested Class * @throws ClassNotFoundException when the class can not be found * @since 1.2 */ protected Class findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); } /** * Helper to define a class using a string of bytes. This version is not * secure. * * @param data the data representing the classfile, in classfile format * @param offset the offset into the data where the classfile starts * @param len the length of the classfile data in the array * @return the class that was defined * @throws ClassFormatError if data is not in proper classfile format * @throws IndexOutOfBoundsException if offset or len is negative, or * offset + len exceeds data * @deprecated use {@link #defineClass(String, byte[], int, int)} instead */ protected final Class defineClass(byte[] data, int offset, int len) throws ClassFormatError { return defineClass(null, data, offset, len); } /** * Helper to define a class using a string of bytes without a * ProtectionDomain. Subclasses should call this method from their * <code>findClass()</code> implementation. The name should use '.' * separators, and discard the trailing ".class". The default protection * domain has the permissions of * <code>Policy.getPolicy().getPermissions(new CodeSource(null, null))<code>. * * @param name the name to give the class, or null if unknown * @param data the data representing the classfile, in classfile format * @param offset the offset into the data where the classfile starts * @param len the length of the classfile data in the array * @return the class that was defined * @throws ClassFormatError if data is not in proper classfile format * @throws IndexOutOfBoundsException if offset or len is negative, or * offset + len exceeds data * @throws SecurityException if name starts with "java." * @since 1.1 */ protected final Class defineClass(String name, byte[] data, int offset, int len) throws ClassFormatError { return defineClass(name, data, offset, len, null); } /** * Helper to define a class using a string of bytes. Subclasses should call * this method from their <code>findClass()</code> implementation. If the * domain is null, the default of * <code>Policy.getPolicy().getPermissions(new CodeSource(null, null))<code> * is used. Once a class has been defined in a package, all further classes * in that package must have the same set of certificates or a * SecurityException is thrown. * * @param name the name to give the class. null if unknown * @param data the data representing the classfile, in classfile format * @param offset the offset into the data where the classfile starts * @param len the length of the classfile data in the array * @param domain the ProtectionDomain to give to the class, null for the * default protection domain * @return the class that was defined * @throws ClassFormatError if data is not in proper classfile format * @throws IndexOutOfBoundsException if offset or len is negative, or * offset + len exceeds data * @throws SecurityException if name starts with "java.", or if certificates * do not match up * @since 1.2 */ protected final synchronized Class defineClass(String name, byte[] data, int offset, int len, ProtectionDomain domain) throws ClassFormatError { if (domain == null) domain = defaultProtectionDomain; if (! initialized) throw new SecurityException("attempt to define class from uninitialized class loader"); return VMClassLoader.defineClass(this, name, data, offset, len, domain); } /** * Links the class, if that has not already been done. Linking basically * resolves all references to other classes made by this class. * * @param c the class to resolve * @throws NullPointerException if c is null * @throws LinkageError if linking fails */ protected final void resolveClass(Class c) { VMClassLoader.resolveClass(c); } /** * Helper to find a Class using the system classloader, possibly loading it. * A subclass usually does not need to call this, if it correctly * overrides <code>findClass(String)</code>. * * @param name the name of the class to find * @return the found class * @throws ClassNotFoundException if the class cannot be found */ protected final Class findSystemClass(String name) throws ClassNotFoundException { return Class.forName(name, false, systemClassLoader); } /** * 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) { Class c = VMSecurityManager.getClassContext()[1]; ClassLoader cl = c.getClassLoader(); if (cl != null && ! cl.isAncestorOf(this)) sm.checkPermission(new RuntimePermission("getClassLoader")); } return parent; } /** * Helper to set the signers of a class. This should be called after * defining the class. * * @param c the Class to set signers of * @param signers the signers to set * @since 1.1 */ protected final void setSigners(Class c, Object[] signers) { c.setSigners(signers); } /** * Helper to find an already-loaded class in this ClassLoader. * * @param name the name of the class to find * @return the found Class, or null if it is not found * @since 1.1 */ protected final synchronized Class findLoadedClass(String name) { return VMClassLoader.findLoadedClass(this, name); } /** * Get the URL to a resource using this classloader or one of its parents. * First tries to get the resource by calling <code>getResource()</code> * on the parent classloader. If the parent classloader returns null then * it tries finding the resource by calling <code>findResource()</code> on * this classloader. The resource name should be separated by '/' for path * elements. * * <p>Subclasses should not override this method but should override * <code>findResource()</code> which is called by this method. * * @param name the name of the resource relative to this classloader * @return the URL to the resource or null when not found */ public URL getResource(String name) { URL result; if (parent == null) result = VMClassLoader.getResource(name); else result = parent.getResource(name); if (result == null) result = findResource(name); return result; } /** * Returns an Enumeration of all resources with a given name that can * be found by this classloader and its parents. Certain classloaders * (such as the URLClassLoader when given multiple jar files) can have * multiple resources with the same name that come from multiple locations. * It can also occur that a parent classloader offers a resource with a * certain name and the child classloader also offers a resource with that * same name. <code>getResource() only offers the first resource (of the * parent) with a given name. This method lists all resources with the * same name. The name should use '/' as path separators. * * <p>The Enumeration is created by first calling <code>getResources()</code> * on the parent classloader and then calling <code>findResources()</code> * on this classloader. * * @param name the resource name * @return an enumaration of all resources found * @throws IOException if I/O errors occur in the process * @since 1.2 */ public final Enumeration getResources(String name) throws IOException { Enumeration parentResources; if (parent == null) parentResources = VMClassLoader.getResources(name); else parentResources = parent.getResources(name); return new DoubleEnumeration(parentResources, findResources(name)); } /** * Called whenever all locations of a named resource are needed. * It is called by <code>getResources()</code> after it has called * <code>parent.getResources()</code>. The results are combined by * the <code>getResources()</code> method. * * <p>The default implementation always returns an empty Enumeration. * Subclasses should override it when they can provide an Enumeration of * URLs (possibly just one element) to the named resource. * The first URL of the Enumeration should be the same as the one * returned by <code>findResource</code>. * * @param name the name of the resource to be found * @return a possibly empty Enumeration of URLs to the named resource * @throws IOException if I/O errors occur in the process * @since 1.2 */ protected Enumeration findResources(String name) throws IOException { return EmptyEnumeration.getInstance(); } /** * Called whenever a resource is needed that could not be provided by * one of the parents of this classloader. It is called by * <code>getResource()</code> after <code>parent.getResource()</code> * couldn't provide the requested resource. * * <p>The default implementation always returns null. Subclasses should * override this method when they can provide a way to return a URL * to a named resource. * * @param name the name of the resource to be found * @return a URL to the named resource or null when not found * @since 1.2 */ protected URL findResource(String name) { return null; } /** * Get the URL to a resource using the system classloader. * * @param name the name of the resource relative to the system classloader * @return the URL to the resource * @since 1.1 */ public static final URL getSystemResource(String name) { return systemClassLoader.getResource(name); } /** * Get an Enumeration of URLs to resources with a given name using the * the system classloader. The enumeration firsts lists the resources with * the given name that can be found by the bootstrap classloader followed * by the resources with the given name that can be found on the classpath. * * @param name the name of the resource relative to the system classloader * @return an Enumeration of URLs to the resources * @throws IOException if I/O errors occur in the process
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -