Java根据包路径查找指定路径下指定类型的实现类

当前工具方法可以根据指定的包路径如:com.xxx.xxx 去加载出指定接口或者抽象的类的实现类;直接上代码

public static List<CommonProtocolChainAdapter> findProtocolAdapter(String packagePath) {
		//创建一个集合,用于存储所有的类
        List<CommonProtocolChainAdapter> adapters = new ArrayList<>();
        try {
        	//根据包路径获取到所有的resources对象
            Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packagePath.replace(".", "/"));
            while (resources.hasMoreElements()) {
                URL nextElement = resources.nextElement();
                //获取到文件路径
                String file = nextElement.getFile();
                process(file, packagePath, adapters);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return adapters;
    }
public static void process(String classPath, String rootPackageName, List<CommonProtocolChainAdapter> commonProtocolChainAdapters) {
        File file = new File(classPath);
        if (!file.exists()) {
            return;
        }
        //首先判断路径是否是文件路径
        if (file.isDirectory()) {
        //获取文件路径下面的所有子路径
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            //遍历子文件
            for (File childrenFile : files) {
            //如果是路径,获取到遍历文件路径的名称,跟根路径名称进行拼接
                if (childrenFile.isDirectory()) {
                    process(childrenFile.getAbsolutePath(), rootPackageName + "." + childrenFile.getName(), commonProtocolChainAdapters);
                } else {
                //如果不是路径,直接将包路径传入下去。进行递归处理
                    process(childrenFile.getAbsolutePath(), rootPackageName, commonProtocolChainAdapters);
                }
            }
        } else {
        //获取到文件的后缀是否是.class
            String fileName = file.getName();
            boolean endsWith = fileName.endsWith(".class");
            if (endsWith) {
            //截取出class的名称
                fileName = fileName.substring(0, fileName.length() - 6);
                try {
                //装载class
                    Class<?> loadClass = Thread.currentThread().getContextClassLoader().loadClass(rootPackageName + "." + fileName);
                    //判断是否是抽象类以及是接口,如果是当前的实现类,直接创建
                    int modifiers = loadClass.getModifiers();
                    if (org.springframework.util.ClassUtils.isAssignable(CommonProtocolChainAdapter.class, loadClass) && !(Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers))) {
                        Object instance = loadClass.newInstance();
                        commonProtocolChainAdapters.add((CommonProtocolChainAdapter) instance);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值