private static Map<String, Object> loadAllJarFromAbsolute(String directoryPath) throws
NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { File directory = new File(directoryPath);
// 判断是否为文件夹,如果是文件,直接用单个jar解析的方法去解析
if (!directory.isDirectory()) {
// 添加jar扫描路径
addUrl(directory);
return loadJarFromAbsolute(directoryPath);
}
// 如果是文件夹,则需要循环加载当前文件夹下面的所有jar
Map<String, Object> clazzMap = new HashMap<>(16);
File[] jars = directory.listFiles();
if (jars != null && jars.length > 0) {
List<String> jarPath = new LinkedList<>();
for (File file : jars) {
String fPath = file.getPath();
// 只加载jar
if (fPath.endsWith(".jar")) {
addUrl(file);
jarPath.add(fPath);
}
}
if (jarPath.size() > 0) {
for (String path : jarPath) {
clazzMap.putAll(loadJarFromAbsolute(path));
}
}
}
return clazzMap;
} private static Map<String, Object> loadJarFromAbsolute(String path) throws IOException {
JarFile jar = new JarFile(path);
Enumeration<JarEntry> entryEnumeration = jar.entries();
Map<String, Object> clazzMap = new HashMap<>(32);
while (entryEnumeration.hasMoreElements()) {
JarEntry entry = entryEnumeration.nextElement();
// 先获取类的名称,符合条件之后再做处理,避免处理不符合条件的类
String clazzName = entry.getName();
if (clazzName.endsWith(".class")) {
// 去掉文件名的后缀
clazzName = clazzName.substring(0, clazzName.length() - 6);
// 替换分隔符
clazzName = clazzName.replace("/", ".");
// 加载类,如果失败直接跳过
try {
Class<?> clazz = Class.forName(clazzName);
Class superClass = clazz.getSuperclass();
if (superClass.equals(ComponentExtend.class)) {
Object o = clazz.newInstance();
Method fieldExtendComponentId = clazz.getMethod("extendComponentId");
String id = (String) fieldExtendComponentId.invoke(o);
clazzMap.put(id, o);
}
} catch (Throwable e) {
// 这里可能出现有些类是依赖不全的,直接跳过,不做处理,也没法做处理
}
}
}
return clazzMap;
} private static void addUrl(File jarPath) throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, MalformedURLException { URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
if (!method.isAccessible()) {
method.setAccessible(true);
}
URL url = jarPath.toURI().toURL();
// 把当前jar的路径加入到类加载器需要扫描的路径
method.invoke(classLoader, url);
}

最新文章

  1. spring定时任务(转载)
  2. cron表达式详解
  3. Linux内核中的Kconfig、xx.defconfig、xx.config、Makefile
  4. iOS 文件读写
  5. 二、 C#调用存储过程
  6. About javascript closure
  7. firefly 环境配置所需工具
  8. Git 版本控制工具使用介绍------Windows系统下使用
  9. linux性能调优概述
  10. (转)UML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图
  11. 用JavaScript+css制作下拉式菜单
  12. python的切片
  13. Unity正交模式摄像机与屏幕适配的方法
  14. 公共语言运行时支持(/clr)
  15. mysql如何分类统计数量
  16. [note]What I’ve learnt from working on startups
  17. 安装和使用iOS的包管理工具CocoaPods
  18. JHipster项目启动后默认的8080主页是空白页面?
  19. Python与Go快速排序
  20. Integer.MIN_VALUE

热门文章

  1. 工作拾记 - 关于easyui模板后台改为vue-element
  2. luogu P2353 背单词
  3. day 11
  4. 重装了服务器,用的是centos/php微信小程序版,centos 命令大全
  5. 第01组 Alpha冲刺(2/6)
  6. jemalloc内存分配原理【转】
  7. SpringBoot激活profiles
  8. vue的跳转方式(打开新页面)及传参
  9. windows如何删除服务
  10. MySQL可传输表空间:将一个表从一个实例拷贝到另一个实例