博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义ClassLoader
阅读量:4966 次
发布时间:2019-06-12

本文共 4408 字,大约阅读时间需要 14 分钟。

自定义classloader

MapleClassLoader

package com.maple;import java.io.*;public class MapleClassLoader extends ClassLoader {    private String path = "E:\\Workspaces\\loader\\target\\classes\\";    public MapleClassLoader(String path){        this.path = path ;    }    public MapleClassLoader(ClassLoader parent,String path){        super(parent);        this.path = path;    }    /*@Override    protected Class
findClass(String name) throws ClassNotFoundException { byte[] data = loadClassData(name); return defineClass(name,data,0,data.length); }*/ /*private byte[] loadClassData(String name) { try { name = name.replaceAll(".", "\\"); FileInputStream is = new FileInputStream(new File(path + name + ".class")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } return bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }*/ @Override public Class
loadClass(String name) throws ClassNotFoundException { try { String fileName = name.replaceAll("\\.", "\\/"); FileInputStream is; try { is = new FileInputStream(new File(path + fileName + ".class")); } catch (Exception e) { is = null; } if (is == null) { return super.loadClass(name); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } byte[] data = bos.toByteArray(); return defineClass(name, data, 0, data.length); } catch (Exception e) { throw new ClassNotFoundException(name); } }}

测试

public class Demo1 {    public static void main(String[] args) throws Exception {        MapleClassLoader mc = new MapleClassLoader(Thread.currentThread().getContextClassLoader(),"E:\\Workspaces\\loader\\target\\classes\\");        Object obj = (Object)mc.loadClass("com.maple.Demo1").newInstance();        System.out.println("load: "+ obj.getClass());        System.out.println("local: "+ Demo1.NUM);        System.out.println(obj instanceof Demo1);    }    private static int NUM = 1;    private static Integer addNum(){        return NUM++;    }}

测试结果

load: class com.maple.Demo1local: 1false

优化版ClassLoader

package com.maple.cl;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.net.URL;import java.net.URLClassLoader;/** * App Class Loader * * @author craneding * @date 16/1/28 */public class AppClassLoader extends ClassLoader {    private String path = "E:\\Workspaces\\loader\\target\\classes\\";    public AppClassLoader(String path) {        super(ClassLoader.getSystemClassLoader());        this.path = path;    }    public AppClassLoader(ClassLoader parent,String path) {        super(parent);        this.path = path;    }    @Override    public Class
loadClass(String name) throws ClassNotFoundException { if (name.startsWith("com.maple.test.S") || name.startsWith("com.isuwang.org.apache.thrift") || name.startsWith("com.isuwang.dapeng.transaction.api") || name.startsWith("com.google.gson")) return ClassLoaderManager.shareClassLoader.loadClass(name); try { Class
c = findLoadedClass(name); if(c==null){ String fileName = name.replaceAll("\\.", "\\/"); FileInputStream is; try { is = new FileInputStream(new File(path + fileName + ".class")); } catch (Exception e) { is = null; } if (is == null) { return super.loadClass(name); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } byte[] data = bos.toByteArray(); return defineClass(name, data, 0, data.length); } return c; } catch (Exception e) { throw new ClassNotFoundException(name); } }}

转载于:https://www.cnblogs.com/leihuazhe/p/8157506.html

你可能感兴趣的文章
OEA 2.11 支持单机版数据库 - SQLite与SQLCE对比
查看>>
仓位管理 V4.3
查看>>
Lucene.net 搜索引擎中中文词组分词的实现
查看>>
BZOJ4651 NOI2016网格(割点)
查看>>
settimeout--原来定时器是有三个及以上参数的
查看>>
Linux安装yum的痛苦路程(失败,慎入)
查看>>
三 分析easyswoole源码(启动服务&TableManager,略提及Cache工具的原理)
查看>>
数据操作之排序
查看>>
JS模块化
查看>>
hdoj_1711Number Sequence
查看>>
关于PreparedStatement.addBatch()方法
查看>>
Javascript 10种遍历方法
查看>>
迭代器模式的应用
查看>>
向页面动态添加外部js
查看>>
【学习】基础知识:数组和矢量计量【Numpy】
查看>>
avalon全选效果分析讲解
查看>>
spring boot 日志导出到文件
查看>>
【科普杂谈】计算机按下电源后发生了什么
查看>>
linux命令缩写及全称
查看>>
Oracle数据库的约束,序列,索引
查看>>