黑马程序员---类加载器
来源:互联网 发布:php构造函数的作用 编辑:程序博客网 时间:2024/04/30 04:43
------- android培训、java培训、期待与您交流! ----------
类加载器的概念:
类加载器就是加载类的工具,JVM在运行的时候需要加载类的class文件进内存,生成类的字节码文件。
加载class文件并生成类的字节码文件的过程就是由类加载器来完成的。
因为类加载器本身也是java类,而任何的java类都需要被加载器加载,所以必然会有一个类加载器不是java类,
它就是Bootstrap。JVM中还内置了两个类加载器:ExtClassLoader和AppClassLoader,它们由Bootstrap加载。
类加载器的委托机制:
当Java虚拟机要加载一个类时,到底派出哪个类加载器去加载呢?
1.首先当前线程的类加载器去加载线程中的第一个类。
2.如果类A中引用了类B,Java虚拟机将使用加载类A的类装载器来加载类B。
3.还可以直接调用ClassLoader.loadClass()方法来指定某个类加载器去加载某个类。
在每个类加载器加载类时,会先委托给其上级类加载器。
AppClassLoader-->ExtClassLoader-->BootStrap
当所有上级类加载器没有加载到类,会回到发起者类加载器,还加载不了,则抛ClassNotFoundException。
自定义类加载器:
父类-->loadClass
子类1(自己干)
子类2(自己干)
原理:
继承ClassLoader为了保留原来的委托机制,不覆盖loadClass方法,直接继承自父类。
只需要覆盖findClass方法,
此方法中使用defineClass方法返回一个class。
实现步骤:
对ClassLoaderAttachment.class文件进行加密,
加密结果存放到另外一个目录ClassLoaderlib文件夹下,
需要用自定义的类加载器才能加载。
自定义的类加载器代码示例:
package cn.itcast.day2;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;public class MyClassLoader extends ClassLoader{/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubString srcPath = args[0];String destDir = args[1];FileInputStream fis = new FileInputStream(srcPath);String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);String destPath = destDir + "\\" + destFileName;FileOutputStream fos = new FileOutputStream(destPath);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;@Overrideprotected Class<?> findClass(String name) throws ClassNotFoundException {// TODO Auto-generated method stubString classFileName = classDir + "\\" + name.substring(name.lastIndexOf('.')+1) + ".class";try {FileInputStream fis = new FileInputStream(classFileName);ByteArrayOutputStream bos = new ByteArrayOutputStream();cypher(fis,bos);fis.close();System.out.println("aaa");byte[] bytes = bos.toByteArray();return defineClass(bytes, 0, bytes.length);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}public MyClassLoader(){}public MyClassLoader(String classDir){this.classDir = classDir;}}
- 黑马程序员-类加载器
- 黑马程序员-类加载器
- 黑马程序员-类加载器
- 黑马程序员-类加载器
- 黑马程序员:类加载器
- 黑马程序员--类加载器
- 黑马程序员---类加载器
- 黑马程序员-----类加载器
- 黑马程序员-----类加载器
- 黑马程序员-类加载器
- 黑马程序员-类加载器
- 黑马程序员------类加载器
- 黑马程序员-类加载器
- 黑马程序员--类加载器
- 黑马程序员---类加载器
- 黑马程序员---类加载器
- 黑马程序员-类加载器
- 黑马程序员--类加载器
- PHP Mysql查询
- 网关串口+EM310
- VS2010串口通讯架构设计初探
- feedbacks from ICB2013
- OSX: 10.9 Mavericks的重要更新技术细节(1)
- 黑马程序员---类加载器
- asp.net后台cs中的JSON格式变量在前台Js中调用方法!(前后台实例!)
- 面向对象一(类、对象、变量、构造函数、构造代码块、this、static)
- 黑马程序员-JAVA基础加强-枚举
- vc++ 编译错误“no appropriate default constructor available”解决方法
- unexpected end of file while looking for precompiled header directive
- boost asio中 error C1189: #error : WinSock.h has already been included 的问题
- 什么是C++成员初始化列表
- C++读写文件