类字节码文件扫描工具,可以扫描jar包
/** * @description :类字节码文件扫描工具,可以扫描jar包 */ public class Utils { //从包路径下扫描 public static Set<Class> getClasses(String packagePath) { Set<Class> res = new HashSet<>(); String path = packagePath.replace(".", "/"); URL url = Thread.currentThread().getContextClassLoader().getResource(path); if (url == null) { System.out.println(packagePath + " is not exit"); return res; } String protocol = url.getProtocol(); if ("jar".equalsIgnoreCase(protocol)) { try { res.addAll(getJarClasses(url, packagePath)); } catch (IOException e) { e.printStackTrace(); return res; } } else if ("file".equalsIgnoreCase(protocol)) { res.addAll(getFileClasses(url, packagePath)); } return res; } //获取file路径下的class文件 private static Set<Class> getFileClasses(URL url, String packagePath) { Set<Class> res = new HashSet<>(); String filePath = url.getFile(); File dir = new File(filePath); String[] list = dir.list(); if (list == null) return res; for (String classPath : list) { if (classPath.endsWith(".class")) { classPath = classPath.replace(".class", ""); try { Class<?> aClass = Class.forName(packagePath + "." + classPath); res.add(aClass); } catch (ClassNotFoundException e) { e.printStackTrace(); } } else { res.addAll(getClasses(packagePath + "." + classPath)); } } return res; } //使用JarURLConnection类获取路径下的所有类 private static Set<Class> getJarClasses(URL url, String packagePath) throws IOException { Set<Class> res = new HashSet<>(); JarURLConnection conn = (JarURLConnection) url.openConnection(); if (conn != null) { JarFile jarFile = conn.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String name = jarEntry.getName(); if (name.contains(".class") && name.replaceAll("/", ".").startsWith(packagePath)) { String className = name.substring(0, name.lastIndexOf(".")).replace("/", "."); try { Class clazz = Class.forName(className); res.add(clazz); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } return res; } }
本文作者:zydjjcpdszylddpll
本文链接:https://www.cnblogs.com/jyfs/p/15103260.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步