Can you find all classes in a package using reflection?

前端 未结 27 2269
不知归路
不知归路 2020-11-21 05:24

Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)

27条回答
  •  盖世英雄少女心
    2020-11-21 05:58

    If you're in Spring-land you can use PathMatchingResourcePatternResolver;

      PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
      Resource[] resources = resolver.getResources("classpath*:some/package/name/*.class");
    
        Arrays.asList(resources).forEach(r->{
            ...
        });
    

提交回复
热议问题