自定义类的加载器
来源:互联网 发布:如何避免社交网络 编辑:程序博客网 时间:2024/05/17 02:41
自定义类加载器类
package com.dasenlin.clazz;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * 自定义文件的类加载器 * @author Administrator * */public class FileSystemClassLoader extends ClassLoader { private String rootDir; public FileSystemClassLoader(String rootDir){ this.rootDir=rootDir; } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { Class<?> c = findLoadedClass(name); if(c!=null){ return c; }else{ ClassLoader parent=this.getParent(); try { c=parent.loadClass(name); } catch (Exception e) { //e.printStackTrace(); } //双亲委托机制,委派给父类加载。 if(c!=null){ return c; }else{ byte[]classData = getClassData(name); if(classData==null){ throw new ClassNotFoundException(); }else{ c=defineClass(name,classData,0,classData.length); } } } return c; } private byte[] getClassData(String name) { String path=rootDir+"/"+name.replace('.', '/')+".class"; InputStream is =null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { is=new FileInputStream(path); byte[]buffer =new byte[1024]; int temp=0; while((temp=is.read(buffer))!=-1){ baos.write(buffer, 0, temp); } return baos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); }finally{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }}
测试类
package com.dasenlin.clazz;public class TestClassLoader { /** * @param args */ /** * @param args */ public static void main(String[] args) { FileSystemClassLoader loader = new FileSystemClassLoader("E:/LearnJavaProjectText/myjava"); FileSystemClassLoader loader2 = new FileSystemClassLoader("E:/LearnJavaProjectText/myjava"); try { Class<?> c = loader.loadClass("HelloWorld"); Class<?> c2 = loader2.loadClass("HelloWorld"); Class<?> c3 = loader2.loadClass("java.lang.String"); Class<?> c4 = loader2.loadClass("com.dasenlin.bean.Employ"); System.out.println(c.hashCode()); System.out.println(c2.hashCode());//不同的类加载器加载同一个class文件,他加载过来不是同一个对象; System.out.println(c3.hashCode());//同一个类加载器加载不同的class文件,他加载过来的也不是同一个对象; System.out.println(c2.getClassLoader());//自定义的类加载器 com.dasenlin.clazz.FileSystemClassLoader@126be4cc System.out.println(c3.getClassLoader());//引导类加载器 null System.out.println(c4.getClassLoader());//系统默认类加载器 sun.misc.Launcher$AppClassLoader@270ad7c0 System.out.println(c); } catch (ClassNotFoundException e) { e.printStackTrace(); } }}
0 0
- 自定义类的加载器
- 自定义类的加载
- 类加载器的委托机制、自定义类加载器
- 自定义类加载器加载指定目录下的类
- 自定义类加载器的实现,类加载过程
- 自定义类加载器的实现,类加载过程
- 自定义JAVA的类加载器
- 刚学的:自定义类加载器
- Java自定义的类加载器
- 自定义类加载器的实现
- 自定义类加载器的使用实例
- 自定义Java类加载器的实现
- Java自定义的类加载器
- 自定义类加载器的编写原理
- jvm自定义类的加载器
- 自定义类加载器的问题
- 自定义类加载器
- 自定义类加载器
- 模板方法模式
- centos安装python3.5
- 监听EditText内容变化的两种方式
- uva1069 Always an integer【解法一】
- BroadcastReceiver学习(02)之有序广播
- 自定义类的加载器
- Collectd & InfluxDb & Grafana 之一: 常用系统统计
- nodejs curl 输出内容
- 使用微信公众号进行发红包
- AlarmManager 闹钟(全局定时器)
- 常用MD5工具类
- web -前端历程
- 【Unity】Android平台的代码热更新
- 30个你不可不知的css选择器