------- android培训、java培训、期待与您交流! ----------
类加载器:
系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap、ExtClassLoader、AppClassLoader。
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