java 高新技术【10.1】 编写和测试自己编写的解密类加载器

当父类的class文件找到时,就用父类的加载,而当父类的class文件找不到时,就用子类的加载。


[java]  view plain copy print ?
  1. <span style="font-size:18px;">package com.itm.one;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9.   
  10. public class MyClassLoader extends ClassLoader{  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) throws Exception {  
  16.         // TODO Auto-generated method stub  
  17.         String srcPath = args[0];  
  18.         String destDir = args[1];  
  19.         FileInputStream fis = new FileInputStream(srcPath);  
  20.         String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);  
  21.         String destPath = destDir + "\\" + destFileName;  
  22.         FileOutputStream fos = new FileOutputStream(destPath);  
  23.         cypher(fis,fos);  
  24.         fis.close();  
  25.         fos.close();  
  26.     }  
  27.       
  28.     private static void cypher(InputStream ips ,OutputStream ops) throws Exception{  
  29.         int b = -1;  
  30.         while((b=ips.read())!=-1){  
  31.             ops.write(b ^ 0xff);  
  32.         }  
  33.     }  
  34.   
  35.     <span style="color:#CC0000;">private String classDir;  
  36.   
  37.     @Override  
  38.     protected Class<?> findClass(String name) throws ClassNotFoundException {  
  39.         String classFileName = classDir + "\\" + name.substring(name.lastIndexOf(".")+1) + ".class";  
  40.         try {  
  41.             FileInputStream fis = new FileInputStream(classFileName);  
  42.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  43.             cypher(fis,bos);  
  44.             fis.close();  
  45.             System.out.println("-------");  
  46.             byte[] bytes = bos.toByteArray();  
  47.             return defineClass(bytes,0,bytes.length);  
  48.         } catch (Exception e) {  
  49.             // TODO Auto-generated catch block  
  50.             e.printStackTrace();  
  51.         }  
  52.         return super.findClass(name);  
  53.     }  
  54.       
  55.     public MyClassLoader(){  
  56.           
  57.     }  
  58.       
  59.     public MyClassLoader(String classDir){  
  60.         this.classDir = classDir;  
  61.     }</span>  
  62. }</span>  

[java]  view plain copy print ?
  1. <span style="font-size:18px;">public class ClassLoaderAttachment extends Date {  
  2.     public String toString(){  
  3.         return "Hello World";  
  4.     }  
  5. }</span>  


test测试:

[java]  view plain copy print ?
  1. <span style="font-size:18px;">public static void main(String[] args) throws Exception{  
  2.           
  3.         Class clazz = new MyClassLoader("itmlib").loadClass("com.itm.one.ClassLoaderAttachment");  
  4.         // 为何 用父类Date 而不用  ClassLoaderAttachment  
  5.         Date d1 = (Date)clazz.newInstance();  
  6.         System.out.println(d1);  
  7.           
  8.     }</span>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值