classloader.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,855 行 · 第 1/5 页
JAVA
1,855 行
* * @see #defineClass(String, byte[], int, int) */ protected final void resolveClass(Class c) { check(); resolveClass0(c); } private native void resolveClass0(Class c); /** * Finds a class with the specified name, loading it if necessary. * * <p> This method loads the class through the system class loader (see * {@link #getSystemClassLoader()}). The <tt>Class</tt> object returned * might have more than one <tt>ClassLoader</tt> associated with it. * Subclasses of <tt>ClassLoader</tt> need not usually invoke this method, * because most class loaders need to override just {@link * #findClass(String)}. </p> * * @param name * The name of the class that is to be found * * @return The <tt>Class</tt> object for the specified <tt>name</tt> * * @throws ClassNotFoundException * If the class could not be found * * @see #ClassLoader(ClassLoader) * @see #getParent() */ protected final Class findSystemClass(String name) throws ClassNotFoundException { check(); ClassLoader system = getSystemClassLoader(); if (system == null) { return loadBootstrapClass(name); } return system.loadClass(name); } /** * Used only by profiling code to load classes via reflections */ private Class findBootstrapClass(String name) throws ClassNotFoundException { return loadBootstrapClass(name); } /** * Returns a bootstrap Class, or throws a ClassNotFoundException */ static Class loadBootstrapClass(String name) throws ClassNotFoundException { Class c = loadBootstrapClassOrNull(name); if (c == null) throw new ClassNotFoundException(name); return c; } /* * Returns a Class or null if class not found. * Can throw ClassNotFoundException for various other * faux pas */ private static Class loadBootstrapClassOrNull(String name) throws ClassNotFoundException { if (!checkName(name)) throw new ClassNotFoundException(name); synchronized(ClassLoader.class) { Class c = loadBootstrapClass0(name); if (c != null && !c.superClassesLoaded()) { c.loadSuperClasses(); } return c; } } private static native Class loadBootstrapClass0(String name) throws ClassNotFoundException; // Check to make sure the class loader has been initialized. private void check() { if (!initialized) { throw new SecurityException("ClassLoader object not initialized"); } } /** * Returns the class with the given name if this loader has been recorded * by the Java virtual machine as an initiating loader of a class with * that name. Otherwise <tt>null</tt> is returned. </p> * * @param name * The class name * * @return The <tt>Class</tt> object, or <tt>null</tt> if the class has * not been loaded * * @since 1.1 */ protected final Class findLoadedClass(String name) { check(); if (!checkName(name)) return null; return findLoadedClass0(name); } private native final Class findLoadedClass0(String name); /** * Sets the signers of a class. This should be invoked after defining a * class. </p> * * @param c * The <tt>Class</tt> object * * @param signers * The signers for the class * * @since 1.1 */ protected final void setSigners(Class c, Object[] signers) { check(); c.setSigners(signers); } // -- Resource -- /** * Finds the resource with the given name. A resource is some data * (images, audio, text, etc) that can be accessed by class code in a way * that is independent of the location of the code. * * <p> The name of a resource is a '<tt>/</tt>'-separated path name that * identifies the resource. * * <p> This method will first search the parent class loader for the * resource; if the parent is <tt>null</tt> the path of the class loader * built-in to the virtual machine is searched. That failing, this method * will invoke {@link #findResource(String)} to find the resource. </p> * * @param name * The resource name * * @return A <tt>URL</tt> object for reading the resource, or * <tt>null</tt> if the resource could not be found or the invoker * doesn't have adequate privileges to get the resource. * * @since 1.1 */ public URL getResource(String name) { URL url; if (parent != null) { url = parent.getResource(name); } else { url = getBootstrapResource(name); } if (url == null) { url = findResource(name); } return url; } /** * Finds all the resources with the given name. A resource is some data * (images, audio, text, etc) that can be accessed by class code in a way * that is independent of the location of the code. * * <p>The name of a resource is a <tt>/</tt>-separated path name that * identifies the resource. * * <p> The search order is described in the documentation for {@link * #getResource(String)}. </p> * * @param name * The resource name * * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for * the resource. If no resources could be found, the enumeration * will be empty. Resources that the class loader doesn't have * access to will not be in the enumeration. * * @throws IOException * If I/O errors occur * * @see #findResources(String) * * @since 1.2 */ public final Enumeration getResources(String name) throws IOException { Enumeration[] tmp = new Enumeration[2]; if (parent != null) { tmp[0] = parent.getResources(name); } else { tmp[0] = getBootstrapResources(name); } tmp[1] = findResources(name); return new CompoundEnumeration(tmp); } /** * Finds the resource with the given name. Class loader implementations * should override this method to specify where to find resources. </p> * * @param name * The resource name * * @return A <tt>URL</tt> object for reading the resource, or * <tt>null</tt> if the resource could not be found * * @since 1.2 */ protected URL findResource(String name) { return null; } /** * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects * representing all the resources with the given name. Class loader * implementations should override this method to specify where to load * resources from. </p> * * @param name * The resource name * * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for * the resources * * @throws IOException * If I/O errors occur * * @since 1.2 */ protected Enumeration findResources(String name) throws IOException { return new CompoundEnumeration(new Enumeration[0]); } /** * Find a resource of the specified name from the search path used to load * classes. This method locates the resource through the system class * loader (see {@link #getSystemClassLoader()}). </p> * * @param name * The resource name * * @return A {@link java.net.URL <tt>URL</tt>} object for reading the * resource, or <tt>null</tt> if the resource could not be found * * @since 1.1 */ public static URL getSystemResource(String name) { ClassLoader system = getSystemClassLoader(); if (system == null) { return getBootstrapResource(name); } return system.getResource(name); } /** * Finds all resources of the specified name from the search path used to * load classes. The resources thus found are returned as an * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link * java.net.URL <tt>URL</tt>} objects. * * <p> The search order is described in the documentation for {@link * #getSystemResource(String)}. </p> * * @param name * The resource name * * @return An enumeration of resource {@link java.net.URL <tt>URL</tt>} * objects * * @throws IOException * If I/O errors occur * @since 1.2 */ public static Enumeration getSystemResources(String name) throws IOException { ClassLoader system = getSystemClassLoader(); if (system == null) { return getBootstrapResources(name); } return system.getResources(name); } /** * Find resources from the VM's built-in classloader. */ private static URL getBootstrapResource(String name) { URLClassPath ucp = getBootstrapClassPath(); Resource res = ucp.getResource(name); return res != null ? res.getURL() : null; } /** * Find resources from the VM's built-in classloader. */ private static Enumeration getBootstrapResources(String name) throws IOException { final Enumeration e = getBootstrapClassPath().getResources(name); return new Enumeration () { public Object nextElement() { return ((Resource)e.nextElement()).getURL(); } public boolean hasMoreElements() { return e.hasMoreElements(); } }; } // Returns the URLClassPath that is used for finding system resources. static URLClassPath getBootstrapClassPath() { return sun.misc.Launcher.getBootstrapClassPath(); } /** * Returns an input stream for reading the specified resource. * * <p> The search order is described in the documentation for {@link * #getResource(String)}. </p> * * @param name * The resource name * * @return An input stream for reading the resource, or <tt>null</tt> * if the resource could not be found * * @since 1.1 */ public InputStream getResourceAsStream(String name) { URL url = getResource(name); try { return url != null ? url.openStream() : null; } catch (IOException e) { return null; } } /** * Open for reading, a resource of the specified name from the search path * used to load classes. This method locates the resource through the * system class loader (see {@link #getSystemClassLoader()}). </p> * * @param name * The resource name * * @return An input stream for reading the resource, or <tt>null</tt> * if the resource could not be found * * @since 1.1 */ public static InputStream getSystemResourceAsStream(String name) { URL url = getSystemResource(name); try { return url != null ? url.openStream() : null; } catch (IOException e) { return null; } } // -- Hierarchy --
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?