版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、分享一种获取指定包名下所有类旳工具类,代码如下import java.io.File;import java.io.FileFilter;import java.lang.annotation.Annotation;import .JarURLConnection;import .URL;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarFile;public class Class
2、Util / 获取指定包名下旳所有类 public static ListClass getClassList(String packageName, boolean isRecursive) ListClass classList = new ArrayListClass(); try Enumeration urls = Thread.currentThread().getContextClassLoader().getResources(packageName.replaceAll(., /); while (urls.hasMoreElements() URL url = urls.n
3、extElement(); if (url != null) String protocol = url.getProtocol(); if (protocol.equals(file) String packagePath = url.getPath(); addClass(classList, packagePath, packageName, isRecursive); else if (protocol.equals(jar) JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection(); Jar
4、File jarFile = jarURLConnection.getJarFile(); Enumeration jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements() JarEntry jarEntry = jarEntries.nextElement(); String jarEntryName = jarEntry.getName(); if (jarEntryName.endsWith(.class) String className = jarEntryName.substring(0, jarEntr
5、yName.lastIndexOf(.).replaceAll(/, .); if (isRecursive | className.substring(0, className.lastIndexOf(.).equals(packageName) classList.add(Class.forName(className); catch (Exception e) e.printStackTrace(); return classList; / 获取指定包名下旳所有类(可根据注解进行过滤) public static ListClass getClassListByAnnotation(St
6、ring packageName, Class annotationClass) ListClass classList = new ArrayListClass(); try Enumeration urls = Thread.currentThread().getContextClassLoader().getResources(packageName.replaceAll(., /); while (urls.hasMoreElements() URL url = urls.nextElement(); if (url != null) String protocol = url.get
7、Protocol(); if (protocol.equals(file) String packagePath = url.getPath(); addClassByAnnotation(classList, packagePath, packageName, annotationClass); else if (protocol.equals(jar) JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection(); JarFile jarFile = jarURLConnection.getJarFi
8、le(); Enumeration jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements() JarEntry jarEntry = jarEntries.nextElement(); String jarEntryName = jarEntry.getName(); if (jarEntryName.endsWith(.class) String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(.).replaceAll(/, .); C
9、lass cls = Class.forName(className); if (cls.isAnnotationPresent(annotationClass) classList.add(cls); catch (Exception e) e.printStackTrace(); return classList; private static void addClass(ListClass classList, String packagePath, String packageName, boolean isRecursive) try File files = getClassFil
10、es(packagePath); if (files != null) for (File file : files) String fileName = file.getName(); if (file.isFile() String className = getClassName(packageName, fileName); classList.add(Class.forName(className); else if (isRecursive) String subPackagePath = getSubPackagePath(packagePath, fileName); Stri
11、ng subPackageName = getSubPackageName(packageName, fileName); addClass(classList, subPackagePath, subPackageName, isRecursive); catch (Exception e) e.printStackTrace(); private static File getClassFiles(String packagePath) return new File(packagePath).listFiles(new FileFilter() Override public boole
12、an accept(File file) return (file.isFile() & file.getName().endsWith(.class) | file.isDirectory(); ); private static String getClassName(String packageName, String fileName) String className = fileName.substring(0, fileName.lastIndexOf(.); if (mons.lang.StringUtil.isNotEmpty(packageName) className =
13、 packageName + . + className; return className; private static String getSubPackagePath(String packagePath, String filePath) String subPackagePath = filePath; if (mons.lang.StringUtil.isNotEmpty(packagePath) subPackagePath = packagePath + / + subPackagePath; return subPackagePath; private static Str
14、ing getSubPackageName(String packageName, String filePath) String subPackageName = filePath; if (mons.lang.StringUtil.isNotEmpty(packageName) subPackageName = packageName + . + subPackageName; return subPackageName; private static void addClassByAnnotation(ListClass classList, String packagePath, St
15、ring packageName, Class annotationClass) try File files = getClassFiles(packagePath); if (files != null) for (File file : files) String fileName = file.getName(); if (file.isFile() String className = getClassName(packageName, fileName); Class cls = Class.forName(className); if (cls.isAnnotationPresent(annotationClass) classList.add(cls); else String subPacka
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年知识产权侵权赔偿合同协议
- 2026-2030中国大衣行业市场全景调研及投资价值评估咨询报告
- 个性化作文教学素材与范文模板
- 个人车辆过户合同产权变更2026范本
- 2026年教育培训机构招生合同范本及入学协议
- 汽车4S店客户服务流程优化方案
- 某汽车厂装配质量办法
- 小学语文重点知识点详解及练习题汇编
- 腰椎骨折的健康宣教
- 消防水炮安装施工详细方案书
- 长征名著试题及答案
- 超市会员管理制度方案
- 显示器件制造中的工艺参数优化考核试卷
- 首都经济贸易大学《贝叶斯统计》2021-2022学年第一学期期末试卷
- 病理生理学(南华大学)知到智慧树章节答案
- 客房服务员理论知识考试题及答案
- 上海市人教版七年级下册期末生物期末试卷及答案
- NBT 35091-2016 水电工程生态流量计算规范
- JBT 6434-2024 输油齿轮泵(正式版)
- 建筑施工风险分级管控建筑施工安全生产风险分级管控制度
- 《秋夜》鲁迅作品讲练课件
评论
0/150
提交评论