1、在复写父类findClass()方法时,为什么最后还要返回:return super.findClass(name)?
2、另外顺便问一下:什么是二进制名称??
package cn.itcast.day2;
import java.io.*;
/**
* 自定义的类加载器MyclassLoader:
* * @author Administrator
*
*/
public class MyClassLoader extends ClassLoader {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String srcPath = args[0];
String destDir = args[1];
System.out.println(srcPath);
System.out.println(destDir);
FileInputStream fis = new FileInputStream(srcPath);
String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);
System.out.println(destFileName);
String destPath = destDir +"\\"+destFileName;
System.out.println(destPath);
FileOutputStream fos = new FileOutputStream(destPath);
System.out.println("谁加载我?"+MyClassLoader.class.getClassLoader().getClass().getName());
System.out.println("谁加载我?"+MyClassLoader.class.getSuperclass().getName());
/ /把.class加密,当要加载时,必须解密。
cypher(fis,fos);
//cypher(fis,fos);
fis.close();
fos.close();
}
//加密方法
private static void cypher(InputStream ips,OutputStream ops) throws Exception{
int b = -1;
while((b=ips.read())!=-1){
ops.write(b^0xff);
}
}
private String classDir;
/**
* 自定义的加载器必须复写findClass(String name)
*name这个参数为什么要是二进制名称,是什么意思?
* findClass()
*/
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// TODO Auto-generated method stub
String classFileName=classDir +"\\"+ name + ".class";
try{
FileInputStream fis = new FileInputStream(classFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//加载时解密
cypher(fis,bos);
fis.close();
byte[] bytes = bos.toByteArray();
return defineClass(bytes,0,bytes.length);
}catch(Exception e){
e.printStackTrace();
}
return super.findClass(name);
}
public MyClassLoader(){
}
public MyClassLoader(String classDir){
this.classDir = classDir;
}
}
|
|