您的位置:首页 > 其它

10.2 自定义类加载器

2011-01-29 12:04 155 查看
p { margin-bottom: 0.21cm; }
自定义类加载器的类需要继承ClassLoader

重写findClass
方法
重写findClass
方法后
才能够正确的优先父类寻找

@Override

protected

Class<?> findClass(String name)

throws

ClassNotFoundException {

//

得到文件的路径名

String
fileName =

ClassDir

+

"//"

+ name.substring(name.lastIndexOf(

'.'

)+1)
+

".class"

;

try

{

//

加载这个文件

FileInputStream
fis =

new

FileInputStream(fileName);

//

定义字节数组流

ByteArrayOutputStream
bos =

new

ByteArrayOutputStream();

//

给文件解密
解密到
bos

里面

cypher

(fis,
bos);

fis.close();

//

得到字节数组

byte

[]
bytes = bos.toByteArray();

//

把一个字节数组生成
class

return

defineClass

(bytes,
0, bytes.

length

);

}

catch

(Exception e) {

e.printStackTrace();

}

return

super

.findClass(name);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: