我已经搜索了一段时间,似乎有不同的方法,这里是一个摘要:
如果你不介意添加依赖项,则反射库非常受欢迎。它看起来像这样:
Reflections reflections = new Reflections("firstdeveloper.examples.reflections");Set<Class<? extends Pet>> classes = reflections.getSubTypesOf(Pet.class);ServiceLoader(根据埃里克森的回答),看起来像这样:ServiceLoader<Pet> loader = ServiceLoader.load(Pet.class);for (Pet implClass : loader) { System.out.println(implClass.getClass().getSimpleName()); // prints Dog, Cat}请注意,要使其正常工作,你需要定义
Pet为
ServiceProviderInterface(SPI)并声明其实现。你这样做,通过创建一个文件
resources/meta-INF/services的名称
examples.reflections.Pet和声明的所有实现Pet它
examples.reflections.Dogexamples.reflections.Cat
包级注释。这是一个例子:
Package[] packages = Package.getPackages();for (Package p : packages) { MyPackageAnnotation annotation = p.getAnnotation(MyPackageAnnotation.class); if (annotation != null) { Class<?>[] implementations = annotation.implementationsOfPet(); for (Class<?> impl : implementations) { System.out.println(impl.getSimpleName()); } }}和注释定义:
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.PACKAGE)public @interface MyPackageAnnotation { Class<?>[] implementationsOfPet() default {};}并且你必须
package-info.java在该包内名为的文件中声明包级注释。以下是示例内容:
@MyPackageAnnotation(implementationsOfPet = {Dog.class, Cat.class})package examples.reflections;请注意,只有那时
ClassLoader已知的软件包才能通过调用加载
Package.getPackages()。
此外,还有其他基于
URLClassLoader的方法,这些方法将始终限于已加载的类,除非你执行基于目录的搜索。



