毕业设计-代码部分

研究JAVA CLASS LOADING已经好多天了。今天刚刚把代码初步整理了下。等待导师的指点。

MyClassLoader.class文件。
自定义CLASSLOADER类的实现:
ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
InBlock.gif * classloader single
InBlock.gif * 
@author zhaojiucheng,liji
InBlock.gif * 
@version 1.2
InBlock.gif *  在1.1的基础上实现实现动态Reload
ExpandedBlockEnd.gif 
*/

None.gif
package  zhao;
None.gif
None.gif
import  java.io.ByteArrayOutputStream;
None.gif
import  java.io.File;
None.gif
import  java.io.FileInputStream;
None.gif
import  java.io.IOException;
None.gif
import  java.io.InputStream;
None.gif
import  java.net.URL;
None.gif
import  java.net.URLClassLoader;
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.Enumeration;
None.gif
import  java.util.Hashtable;
None.gif
import  java.util.List;
None.gif
import  java.util.jar.JarEntry;
None.gif
import  java.util.jar.JarFile;
None.gif
import  java.util.jar.JarInputStream;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 自定义类加载器
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  MyClassLoader  extends  ClassLoader  dot.gif {
InBlock.gif
InBlock.gif    
private static String MyClasspath = new String("");
InBlock.gif
InBlock.gif    
private static ArrayList loadClassList = new ArrayList();
InBlock.gif
InBlock.gif    
private static Hashtable loadClassHashTable = new Hashtable();
InBlock.gif
InBlock.gif    
private static Hashtable loadClassTime = new Hashtable();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public MyClassLoader() dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 构造自定义的加载器 参数1:路径名
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        
this.MyClasspath = MyClasspath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 设置加载路径 参数1:路径名
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        
this.MyClasspath = MyClasspath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 查找类并加载 参数1:文件名 被 loadClass() 调用
ExpandedSubBlockEnd.gif     
*/

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

InBlock.gif        System.out.println(
"自定义加载器正在加载: " + name + "dot.gif");
InBlock.gif        Class c 
= defineClass(name, classData, 0, classData.length);
InBlock.gif        loadClassHashTable.put(name, c);
InBlock.gif        System.out.println(
"加载" + name + "类成功。");
InBlock.gif        
return c;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 读取文件字节码 参数1:文件名 被 findClass() 调用
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private byte[] loadClassData(String name) throws IOException dot.gif{
InBlock.gif        String filePath 
= searchFile(MyClasspath, name + ".class");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!(filePath == null || filePath == "")) dot.gif{
InBlock.gif            System.out.println(
"已经找到" + filePath + ",开始读取字节码并加载。");
InBlock.gif            FileInputStream inFile 
= new FileInputStream(filePath);
InBlock.gif            
byte[] classData = new byte[inFile.available()];
InBlock.gif            inFile.read(classData);
InBlock.gif            inFile.close();
InBlock.gif            loadClassTime.put(name, 
new File(filePath).lastModified());
InBlock.gif            
return classData;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
InBlock.gif            filePath 
= searchFile(MyClasspath, name + ".java");
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!(filePath == null || filePath == "")) dot.gif{
InBlock.gif                System.out.println(
"已经找到" + filePath + "并开始编译");
InBlock.gif                Runtime.getRuntime().exec(
"javac " + filePath);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try dot.gif{
InBlock.gif                    Thread.sleep(
1000);
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (InterruptedException e) dot.gif{
InBlock.gif                    e.printStackTrace();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return loadClassData(name);
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif{
InBlock.gif                System.out.println(
"未找到类文件,读取字节码失败!");
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 加载类 参数1:字节码数组 参数2:类名
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
public Class loadClass(byte[] classData, String className)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
throws ClassNotFoundException dot.gif{
InBlock.gif        System.out.println(
"自定义加载器正在加载: " + className + "dot.gif");
InBlock.gif        Class c 
= defineClass(className, classData, 0, classData.length);
InBlock.gif        loadClassHashTable.put(className, c);
InBlock.gif        System.out.println(
"加载" + className + "类成功。");
InBlock.gif        
return c;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 加载类 参数1:类名 类名从当前文件系统中查找指定的Class文件
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Class loadClass(String name) throws ClassNotFoundException dot.gif{
InBlock.gif        
return loadClass(name, false);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 加载类 参数1:类名 参数2:是否分析这个类
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
protected Class loadClass(String name, boolean resolve)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
throws ClassNotFoundException dot.gif{
InBlock.gif        
byte[] classData = null;
InBlock.gif        Class temp 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            temp 
=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值