如何以编程方式在 Java 中获取接口的所有实现的列表? [英] How can I get a list of all the implementations of an interface programmatically in Java?

查看:28
本文介绍了如何以编程方式在 Java 中获取接口的所有实现的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用反射或类似的方法来做吗?

Can I do it with reflection or something like that?

推荐答案

找了好久,好像有不同的方法,总结如下:

I have been searching for a while and there seems to be different approaches, here is a summary:

  1. reflections 库很受欢迎,如果你不这样做的话介意添加依赖项.它看起来像这样:

  1. reflections library is pretty popular if u don't mind adding the dependency. It would look like this:

Reflections reflections = new Reflections("firstdeveloper.examples.reflections");
Set<Class<? extends Pet>> classes = reflections.getSubTypesOf(Pet.class);

  • ServiceLoader(根据 erickson 的回答),它看起来像这样:

  • ServiceLoader (as per erickson answer) and it would look like this:

    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 的所有实现来实现在里面

    Note that for this to work you need to define Petas a ServiceProviderInterface (SPI) and declare its implementations. you do that by creating a file in resources/META-INF/services with the name examples.reflections.Pet and declare all implementations of Pet in it

    examples.reflections.Dog
    examples.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 的文件中声明包级注释.以下是示例内容:

    and you must declare the package-level annotation in a file named package-info.java inside that package. here are sample contents:

    @MyPackageAnnotation(implementationsOfPet = {Dog.class, Cat.class})
    package examples.reflections;
    

    请注意,只有在那时 ClassLoader 已知的包才会通过调用 Package.getPackages() 加载.

    Note that only packages that are known to the ClassLoader at that time will be loaded by a call to Package.getPackages().

    此外,还有其他基于 URLClassLoader 的方法将始终限于已加载的类,除非您进行基于目录的搜索.

    In addition, there are other approaches based on URLClassLoader that will always be limited to classes that have been already loaded, Unless you do a directory-based search.

    这篇关于如何以编程方式在 Java 中获取接口的所有实现的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆