classloader.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 417 行 · 第 1/2 页

JAVA
417
字号
     * Class. Before the Class can be used it must be resolved.
     *
     * @param	   name     the expected name of the class; null if unknown;
     *                      using '.' and not '/' as separator, and without
     *                      a trailing ".class" suffix.
     * @param      data     the bytes that make up the <code>Class</code>.
     * @param      offset   the start offset of the <code>Class</code> data.
     * @param      length   the length of the <code>Class</code> data.
     * @return     the <code>Class</code> object that was created from the data.
     * @exception  ClassFormatError  if the data does not contain a valid Class.
     * @see        ClassLoader#loadClass(java.lang.String, boolean)
     * @see        ClassLoader#resolveClass(java.lang.Class)
     * @since      JDK1.1
     */
    protected final Class defineClass(String name,
				      byte data[], int offset, int length) { 
	check();
	Class result = defineClass0(name, data, offset, length);
	if (result != null) 
	    classes.put(result.getName(), result);
	return result;
    }

    /**
     * Resolves the class so that an instance of the class can be 
     * created, or so that one of its methods can be called. This method 
     * should be called by <code>loadClass</code> if the resolve flag is 
     * <code>true</code>. 
     *
     * @param   c   the <code>Class</code> instance to be resolved.
     * @see 	java.lang.ClassLoader#defineClass(java.lang.String, byte[], int, int)
     * @since   JDK1.0
     */
    protected final void resolveClass(Class c) { 
	check();
	resolveClass0(c);
    }

    /**
     * Finds the system class with the specified name, loading it in if 
     * necessary. 
     * <p>
     * A system class is a class loaded from the local file system in a 
     * platform- dependent way. It has no class loader. 
     *
     * @param      name   the name of the system <code>class</code>.
     * @return     a system class with the given name.
     * @exception  ClassNotFoundException  if it could not find a definition
     *               for the class.
     * @exception  NoClassDefFoundError    if the class is not found.
     * @since      JDK1.0
     */
    protected final Class findSystemClass(String name) 
    throws ClassNotFoundException {
	check();
	return findSystemClass0(name);
    }

    /**
     * Sets the signers of a class. This is called after defining a class,
     * by signature-aware class loading code.
     *
     * @since   JDK1.1
     */
    protected final void setSigners(Class cl, Object[] signers) {
        check();
	// make a check which will take cl.getClassLoader and
	// check if it is != this.
	cl.setSigners(signers);
    }

    /**
     * Initializes the Class loader.
     */
    private native void init();
    private native Class defineClass0(String name, 
				      byte data[], int offset, int length);
    private native void resolveClass0(Class c);
    private native Class findSystemClass0(String name) 
        throws ClassNotFoundException;

    private void check() { 
	if (initialized == true) 
	    return;
	throw new SecurityException("ClassLoader object not initialized.");
    }

    /**
     * @since   JDK1.1
     */
    final protected Class findLoadedClass(String name) { 
	return (Class)classes.get(name);
    }

    /**
     * Load and resolve a class.
     */
    final Class loadClassInternal(String name, boolean resolve) 
                    throws ClassNotFoundException {
	name = name.replace('/', '.');
	Class cl = (Class)classes.get(name);
	if (cl == null) {
	    cl = loadClass(name, false);
	    if (cl == null) 
		throw new ClassNotFoundException(name);
	    String realName = cl.getName();
	    if (!realName.equals(name)) { 
		throw new ClassNotFoundException(name);
	    }
	    classes.put(realName, cl);
	}
	if (resolve) 
	    resolveClass(cl);
	return cl;
    }

    /**
     * A resource is some data (images, audio, text, etc) that wants to be
     * accessed by some class code in a way that is independent of the
     * location of the code.  Resources are found with cooperation of the
     * class loaders, since they are the only ones who know where the class
     * actually came from. <p>
     *
     * System resources are those that are handled by the host implemenation
     * directly.  For example, they may be located in the CLASSPATH.<p>
     *
     * The name of a resource is a "/"-separated sequence of identifiers.
     * The class Class provides convenience methods for accessing resources;
     * the methods implement a convention where the package name is prefixed
     * to the short name of the resource.<p>
     * 
     * Resources can be accessed as an InputStream, or as a URL.
     *
     * @see	Class
     */

    /**
     * Get an InputStream on a given resource..  Will return null if no
     * resource with this name is found. <p>
     *
     * The resource name may be any system resource (e.g. follows CLASSPATH
     * order).
     *
     * @param	name	the name of the resource, to be used as is.
     * @return	an InputStream on the resource, or null if not found.
     * @since   JDK1.1
     */
    public static final InputStream getSystemResourceAsStream(String name) {
	// REMIND - This is equivalent to getSystemResource() call plus a openStream()
	return getSystemResourceAsStream0(name);
    }

    /**
     * Find a resource with a given name.  The return is a URL to the resource
     * Doing a getContent() on the URL may return an Image, an AudioClip, or
     * an InputStream.<p>
     *
     * The resource name may be any system resource (e.g. follows CLASSPATH
     * order).
     *
     * @param	name	the name of the resource, to be used as is.
     * @return	the URL on the resource, or null if not found.
     * @since   JDK1.1
     */
    public static final java.net.URL getSystemResource(String name) {
	String s = getSystemResourceAsName0(name);
	java.net.URL back;
	try {
	    back = new java.net.URL(s);
	} catch (Exception ex) {
	    back = null;
	}
	return back;
    }

    /**
     * Get an InputStream on a given resource.  Will return null if no
     * resource with this name is found. <p>
     *
     * The class loader can choose what to do to locate the resource.
     *
     * @param	name	the name of the resource, to be used as is.
     * @return	an InputStream on the resource, or null if not found.
     * @since   JDK1.1
     */
    public InputStream getResourceAsStream(String name) {
	return null;
    }

    /**
     * Find a resource with a given name.  The return is a URL to the resource.
     * Doing a getContent() on the URL may return an Image, an AudioClip,
     * or an InputStream.<p>
     *
     * The class loader can choose what to do to locate the resource.
     *
     * @param	name	the name of the resource, to be used as is.
     * @return	an InputStream on the resource, or null if not found.
     * @since   JDK1.1
     */
    public java.net.URL getResource(String name) {
	return null;
    }

    private static native InputStream getSystemResourceAsStream0(String name);
    private static native String getSystemResourceAsName0(String name);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?