📄 classloader.java
字号:
* <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 { checkInitialized(); if (domain == null) domain = StaticData.defaultProtectionDomain; return VMClassLoader.defineClass(this, name, data, offset, len, domain); } /** * Helper to define a class using the contents of a byte buffer. 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 buf a byte buffer containing bytes that form a class. * @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 NoClassDefFoundError if the supplied name is not the same as * the one specified by the byte buffer. * @throws SecurityException if name starts with "java.", or if certificates * do not match up * @since 1.5 */ protected final Class defineClass(String name, ByteBuffer buf, ProtectionDomain domain) throws ClassFormatError { byte[] data = new byte[buf.remaining()]; buf.get(data); return defineClass(name, data, 0, data.length, 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) { checkInitialized(); 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 { checkInitialized(); return Class.forName(name, false, StaticData.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>. * * @return the parent <code>ClassLoader</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 = SecurityManager.current; if (sm != null) { ClassLoader cl = VMStackWalker.getCallingClassLoader(); 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) { checkInitialized(); 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) { checkInitialized(); 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()</code> 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.</p> * * @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 StaticData.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 * @since 1.2 */ public static Enumeration getSystemResources(String name) throws IOException { return StaticData.systemClassLoader.getResources(name); } /** * Get a resource as stream using this classloader or one of its parents. * First calls <code>getResource()</code> and if that returns a URL to * the resource then it calls and returns the InputStream given by * <code>URL.openStream()</code>. * * <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 an InputStream to the resource, or null * @since 1.1 */ public InputStream getResourceAsStream(String name) { try { URL url = getResource(name); if (url == null) return null; return url.openStream(); } catch (IOException e) { return null; } } /** * Get a resource using the system classloader. * * @param name the name of the resource relative to the system classloader * @return an input stream for the resource, or null * @since 1.1 */ public static final InputStream getSystemResourceAsStream(String name) { try { URL url = getSystemResource(name); if (url == null) return null; return url.openStream(); } catch (IOException e) { return null; } } /** * Returns the system classloader. The system classloader (also called * the application classloader) is the classloader that is used to * load the application classes on the classpath (given by the system * property <code>java.class.path</code>. This is set as the context * class loader for a thread. The system property
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -