JAVA CLASS LOADING技术研究---整理后的代码

以下是整理后的代码部分,欢迎批评指正。

MyClassLoader.java
/**/ /*
 * @MyClassLoader.java    07/04/17
 *
 * Copyright Zhao Jiucheng. All rights reserved.
 
*/

package  com.neusoft.classloader;

import  java.io.File;
import  java.io.FileInputStream;
import  java.io.IOException;
import  java.util.Hashtable;
import  java.util.jar.JarEntry;
import  java.util.jar.JarInputStream;

/** */ /**
 * A class loader is an object that is responsible for loading classes. Given
 * the binary name of a class, a class loader should attempt to locate or
 * generate data that constitutes a definition for the class. A typical strategy
 * is to transform the name into a file name and then read a "class file" of
 * that name from a file system.
 * 
 * 
@version 1.0, 07/04/17
 * 
@author Zhao Jiucheng
 * 
 
*/

public   class  MyClassLoader  extends  ClassLoader  {

    
// a classpath for search
    private static String myClasspath = new String("");

    
// hashtable that memory the loaded classes
    private static Hashtable<String, Class<?>> loadClassHashTable = new Hashtable<String, Class<?>>();

    
// hashtable that memory the time of loading a class
    private static Hashtable<String, Long> loadClassTime = new Hashtable<String, Long>();

    
// the null constructor
    public MyClassLoader() {

    }


    
/** *//**
     * create a classloader and specify a classpath.
     * 
     * 
@param myClasspath
     *            the specified classpath name.
     
*/

    
public MyClassLoader(String myClasspath) {
        
if (!myClasspath.endsWith("\\")) {
            myClasspath 
= myClasspath + "\\";
        }

        MyClassLoader.myClasspath 
= myClasspath;
    }


    
/** *//**
     * set the classpath
     * 
     * 
@param myClasspath
     *            the specified classpath name
     
*/

    
public void SetmyClasspath(String myClasspath) {
        
if (!myClasspath.endsWith("\\")) {
            myClasspath 
= myClasspath + "\\";
        }

        MyClassLoader.myClasspath 
= myClasspath;
    }


    
/** *//**
     * Loads the class with the specified binary name. This method searches for
     * classes in the same manner as the loadClass(String, boolean) method.
     * Invoking this method is equivalent to invoking {loadClass(name,false)}.
     * 
     * 
@param className
     *            The binary name of the class.
     * 
     * 
@return The resulting <tt>Class</tt> object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class was not found.
     
*/

    @SuppressWarnings(
"unchecked")
    
public Class loadClass(String className) throws ClassNotFoundException {
        
return loadClass(className, false);
    }


    
/** *//**
     * Loads the class with the specified binary name. The default
     * implementation of this method searches for classes in the following
     * order:
     * 
     * Invoke {findLoadedClass(String)} to check if the class has already been
     * loaded.
     * 
     * Invoke {findSystemClass(String)} to load the system class.
     * 
     * Invoke the {findClass(String)} method to find the class.
     * 
     * If the class was found using the above steps, and the resolve flag is
     * true, this method will then invoke the {resolveClass(Class)} method on
     * the resulting Class object.
     * 
     * 
@param name
     *            The binary name of the class.
     * 
     * 
@param resolve
     *            If true then resolve the class.
     * 
     * 
@return The resulting Class object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class could not be found.
     
*/

    @SuppressWarnings(
"unchecked")
    
protected Class loadClass(String name, boolean resolve)
            
throws ClassNotFoundException {

        
try {
            Class foundClass 
= findLoadedClass(name);

            
// check if the class has already been loaded.
            if (foundClass != null{
                System.out.println(
"Complete to load the class: " + name);
                
return foundClass;
            }


            
// if the class is systemClass, load the system class by system
            if (name.startsWith("java.")) {
                foundClass 
= findSystemClass(name);
                loadClassHashTable.put(name, foundClass);
                System.out.println(
"System is loading the class: " + name);
                
return foundClass;
            }


            
// invoke the findClass() method to load the class
            try {
                foundClass 
= findClass(name);
            }
 catch (Exception fnfe) {
            }


            
if (resolve && (foundClass != null)) {
                resolveClass(foundClass);
            }

            
return foundClass;
        }
 catch (Exception e) {
            
throw new ClassNotFoundException(e.toString());
        }

    }


    
/** *//**
     * Finds the class with the specified binary name.The default implementation
     * throws a ClassNotFoundException.
     * 
     * 
@param className
     *            The binary name of the class.
     * 
     * 
@return The resulting Class object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class could not be found.
     
*/

    @SuppressWarnings(
"unchecked")
    
public Class findClass(String className) {

        
byte[] classData = null;
        
try {
            classData 
= loadClassData(className);
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        
if( classData == null)

你可能感兴趣的:(java)