关于获取路径下所有class文件

关于获取路径下所有class文件

(最终目的:获取包下所有的类的全类名)

通过类加载器的getResource方法获取指定目录下的resource

Enumeration<URL> resources = classLoader.getResources(pathName);//pathName = "com/xxx/xxx"
1、遍历resources获取url
 while (resources.hasMoreElements()) {
     URL url = resources.nextElement();
 }
2、如果是本地环境 也就是说 url.getProtocol() = File
2.1 通过当前路径获取过滤出 class文件 和 目录
 /**
     * 获取路径下所有的 .class 文件 和  文件夹
     *
     * @param filePath  文件目录
     * @param recursive 是否查询子文件夹
     * @return 文件
     */
    public static File[] findFiles(String filePath, boolean recursive) {
        //过滤出 class文件 和 目录
        return new File(filePath).listFiles(file ->
                (file.isFile() && file.getName().endsWith(POINT + CLASS)) ||
                        (file.isDirectory() && recursive));
    }
2.2 目录则进行递归 ,不是目录则为class文件直接保存全类名
public static void findFullClassName(...){
    //遍历过滤好的文件
    for (File file : findFiles(filePath,recursive)) {
            //如果文件是目录
            if (file.isDirectory()) {
                ....
                //调用当前方法
                findFullClassName(file.getPath())  //路径为文件路径
            }else{
                //如果是class文件 则去除 后缀 .class
                String className = file.getName().substring(0, file.getName().lastIndexOf(POINT));
                //添加包名到list
            }
    }
}
	
3、不是本地环境,也就是将项目打成jar包后(需要引起注意)
url = 第二步的url
//获取Jar资源
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarURLConnection.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
    JarEntry jarEntry = entries.nextElement();
    String jarEntryName = jarEntry.getName();
    //包含.class 和 com/xxx/xxx 留下
    if (jarEntryName.contains(pathName) && jarEntryName.contains(POINT + CLASS)) {
        //如果是class文件 则去除 后缀 .class
        String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(POINT));
        //添加包名
        fullClassName.add(className.replaceAll(BACKSLASH,POINT));
    }
}

通过Spring工具获取Resource

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metaReader = new CachingMetadataReaderFactory(resolver.getResourceLoader());
String packagePath =
    ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + packageName.replace(".", "/") + "/**/*.class";
Resource[] resources = resolver.getResources(packagePath);
for (Resource r : resources) {
    MetadataReader reader = metaReader.getMetadataReader(r);
    fullClassName.add(reader.getClassMetadata().getClassName());
}
return fullClassName;

总结

1、Spring如果也是jar包运行环境的话,和步骤三是一致的。

2、Spring如果是本地运行环境的话,是通过找出每一个file文件listFiles方法找到目录及目录以下的文件。

3、作为学习,自己写一遍还是不错的。作为使用,推荐使用Spring封装好的获取指定路径资源方法。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值