黑马程序员_类加载器

------- android培训java培训、期待与您交流! ----------

类加载器:

系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrapExtClassLoaderAppClassLoader




package cn.hmm.classLoadTest;


public class classLoadDemo {

	public static void main(String[] args) {
		
		testClassLoad();
		
	}

	private static void testClassLoad() {
		System.out.println(classLoadDemo.class.getClassLoader().getClass()
				.getName());
		//System的加载类为空是因为他是BootStrap加载的
		System.out.println(System.class.getClassLoader());
		
		ClassLoader loader = classLoadDemo.class.getClassLoader();
		while(loader != null){
			System.out.println(loader.getClass().getName());
			loader = loader.getParent();
		}
		System.out.println(loader);
	}

}

我们可以编写自己专属的类加载器去加载加密处理的字节码

1.写一个简单的Person类,覆盖toString方法:

package cn.hmm.classLoadTest;

import java.util.Date;

public class Person extends Date {

	public String toString(){
		return "hello world";
	}

}

2.编写一个加密class字节码的类,拥有加密和解密的方法,为了方便,可以使用简单的异或

package cn.hmm.classLoadTest;

import java.io.*;


public class MyClassLoader {


	public static void main(String[] args) throws IOException {
		// TODO 自动生成的方法存根
		String srcPath = args[0];
		String destDir = args[1];
		copyClassTolib(srcPath, destDir);
		//System.out.println(new Person());
	}

	public static void copyClassTolib(String srcPath, String destDir)
			throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream(srcPath);
		//源目标
		String FileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);
		//目的地目标
		String destPath = destDir + "\\" + FileName;
		FileOutputStream fos = new FileOutputStream(destPath);
		//加密拷贝
		cypher(fis,fos);
		fis.close();
		fos.close();
		
	}

	public static void cypher(InputStream ips,OutputStream ops) throws IOException{
		int b = -1;
		while((b=ips.read())!=-1){
			//加密算法
			ops.write(b^0xff);
		}
	}
}

3.编写自己的类加载器,用自己的类加载器解密那份加密的字节码文件


package cn.hmm.classLoadTest;

import java.io.*;
import java.util.Date;

public class PersonLoader extends ClassLoader {


	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		// TODO 自动生成的方法存根
		//用自己的类加载器去加载加密后的类
		Class clazz = new PersonLoader("D:\\workspace\\高新技术\\lib").loadClass("Person.class");
		System.out.println(clazz.getClassLoader().getClass().getName());
		Date d1 = (Date) clazz.newInstance();
		System.out.println(d1.toString());
		//父类只能加载带包名的(默认的应该是AppClassLoader)
		/*System.out.println(new Person().getClass().getClassLoader().getClass()
				.getName());
		
		Class clazz = new PersonLoader().loadClass("cn.hmm.classLoadTest.Person");
		Date d1 = (Date) clazz.newInstance();
		System.out.println(d1.toString());*/
		//直接加载
		//System.out.println(new Person());
		
		
	}
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException{
		String destPath = Path+"\\"+name;
		try{
			FileInputStream ips = new FileInputStream(destPath);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			MyClassLoader.cypher(ips, bos);
			byte[] buf = bos.toByteArray();
			return defineClass(buf,0,buf.length);
			//return super.findClass(name);
		}catch(Exception e){
			//throw new RuntimeException(e);
			
		}
		return super.findClass(name);
	}
	private String Path;
	PersonLoader(){
		
	}
	PersonLoader(String Path){
		this.Path = Path;
	}
}

------- Windows Phone 7手机开发.Net培训、期待与您交流! -------
详细请查看:www.itheima.com


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值