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

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

MyClassLoader.java
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * @MyClassLoader.java    07/04/17
InBlock.gif *
InBlock.gif * Copyright Zhao Jiucheng. All rights reserved.
ExpandedBlockEnd.gif 
*/

None.gif
package  com.neusoft.classloader;
None.gif
None.gif
import  java.io.File;
None.gif
import  java.io.FileInputStream;
None.gif
import  java.io.IOException;
None.gif
import  java.util.Hashtable;
None.gif
import  java.util.jar.JarEntry;
None.gif
import  java.util.jar.JarInputStream;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * A class loader is an object that is responsible for loading classes. Given
InBlock.gif * the binary name of a class, a class loader should attempt to locate or
InBlock.gif * generate data that constitutes a definition for the class. A typical strategy
InBlock.gif * is to transform the name into a file name and then read a "class file" of
InBlock.gif * that name from a file system.
InBlock.gif * 
InBlock.gif * 
@version 1.0, 07/04/17
InBlock.gif * 
@author Zhao Jiucheng
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  MyClassLoader  extends  ClassLoader  dot.gif {
InBlock.gif
InBlock.gif    
// a classpath for search
InBlock.gif
    private static String myClasspath = new String("");
InBlock.gif
InBlock.gif    
// hashtable that memory the loaded classes
InBlock.gif
    private static Hashtable<String, Class<?>> loadClassHashTable = new Hashtable<String, Class<?>>();
InBlock.gif
InBlock.gif    
// hashtable that memory the time of loading a class
InBlock.gif
    private static Hashtable<String, Long> loadClassTime = new Hashtable<String, Long>();
InBlock.gif
InBlock.gif    
// the null constructor
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public MyClassLoader() dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * create a classloader and specify a classpath.
InBlock.gif     * 
InBlock.gif     * 
@param myClasspath
InBlock.gif     *            the specified classpath name.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public MyClassLoader(String myClasspath) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!myClasspath.endsWith("\\")) dot.gif{
InBlock.gif            myClasspath 
= myClasspath + "\\";
ExpandedSubBlockEnd.gif        }

InBlock.gif        MyClassLoader.myClasspath 
= myClasspath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * set the classpath
InBlock.gif     * 
InBlock.gif     * 
@param myClasspath
InBlock.gif     *            the specified classpath name
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void SetmyClasspath(String myClasspath) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!myClasspath.endsWith("\\")) dot.gif{
InBlock.gif            myClasspath 
= myClasspath + "\\";
ExpandedSubBlockEnd.gif        }

InBlock.gif        MyClassLoader.myClasspath 
= myClasspath;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif    @SuppressWarnings(
"unchecked")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Class loadClass(String className) throws ClassNotFoundException dot.gif{
InBlock.gif        
return loadClass(className, false);
ExpandedSubBlockEnd.gif    }

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

InBlock.gif    @SuppressWarnings(
"unchecked")
InBlock.gif    
protected Class loadClass(String name, boolean resolve)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
throws ClassNotFoundException dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            Class foundClass 
= findLoadedClass(name);
InBlock.gif
InBlock.gif            
// check if the class has already been loaded.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (foundClass != nulldot.gif{
InBlock.gif                System.out.println(
"Complete to load the class: " + name);
InBlock.gif                
return foundClass;
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
InBlock.gif            
// invoke the findClass() method to load the class
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                foundClass 
= findClass(name);
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception fnfe) dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (resolve && (foundClass != null)) dot.gif{
InBlock.gif                resolveClass(foundClass);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return foundClass;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
InBlock.gif            
throw new ClassNotFoundException(e.toString());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

InBlock.gif    @SuppressWarnings(
"unchecked")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Class findClass(String className) dot.gif{
InBlock.gif
InBlock.gif        
byte[] classData = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            classData 
= loadClassData(className);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (IOException e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if( classData == null)dot.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值