查找实现特定接口的所有类 [英] Finding all classes implementing a specific interface

查看:134
本文介绍了查找实现特定接口的所有类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序(Quartz调度程序),我们有一个工作类负责实际执行工作,我们需要告诉/传递工作类的名称,同时在Quartz中创建一个触发器调度程序。

I am in the process of developing an application (Quartz scheduler) where we have a job class which is responsible for actually executing the work and we need to tell/pass the name of the job class while creating a trigger in the Quartz scheduler.

我想为所有想要使用API​​的人提供一个扩展点(除了我将作为API的一部分提供的一些通用作业)。我们的想法是创建一个(标记)接口,如果有人想要将它们的类声明为调度程序作业类,他们所要做的就是(声明)实现接口。

I want to provide an extension point to all who want to use the API (beside the some generic jobs which I will provide as part of the API). The idea is to create an (marker) interface and if any one wants to declare their class as scheduler job class, all they have to do is, to (declare to) implement the interface.

我不知道如何找到合同后面的类(通过实现接口),以便我可以向想要在调度程序中安排触发器的用户显示它们。

I am not sure how I can find which classes are following the contract (by implementing the interface) so that I can show them to the user who want to schedule a trigger in the scheduler.

我的要求是不要在运行时加载类,而是要显示实现所需接口的类的用户列表,以便用户可以选择类和类名可以传递给调度。这是Quartz调度程序,它最终将负责创建类的实例。

任何人都可以建议我如何实现上述目标或者是有没有其他更好的方法来实现我想做的事情?

Can any one suggest how I can achieve the above goal or is there any other better way to achieve what I am trying to do?

编辑

我查看了 ServiceLoader 的文档,似乎是实现一个服务必须在META-INF文件夹中创建一个具有实现类名称的文件,这使我认为如果我的API用户需要20个不同的实现,他必须在文件中放入20个条目对我来说似乎为最终用户做了很多额外的工作,因为每个工作类都将被创建用于执行特定的工作,并且可以有100个工作类。

I went through the doc of ServiceLoader and it seems that for implementing a service one has to create a file in the META-INF folder with the name of the implementation class, which leads me to think that if the user of my API wants 20 different implementations, he has to put 20 entries in the file which for me seems a lot of extra work for the end user since each job class will be created for executing a specific job and there can be 100s of job classes.

请更正我,如果我的假设是错误的。

Please correct me if my assumption is wrong.

推荐答案

我有一个类似的需要我想要的地方确保创建的任何实现某个接口的类始终是真正可序列化的。我创建了一个JavaClassFinder,它遍历类路径中的所有目录,并找到可分配给我关心的接口的所有类。以下是代码段:

I had a similar need where I wanted to make sure that any classes created that implemented a certain interface were always truly serializable. I created a JavaClassFinder which walks through all directories in the classpath, and finds all classes assignable to the interface I cared about. Here is a code snippet:

public <T> List<Class<? extends T>> findAllMatchingTypes(Class<T> toFind) {
    foundClasses = new ArrayList<Class<?>>();
    List<Class<? extends T>> returnedClasses = new ArrayList<Class<? extends T>>();
    this.toFind = toFind;
    walkClassPath();
    for (Class<?> clazz : foundClasses) {
        returnedClasses.add((Class<? extends T>) clazz);
    }
    return returnedClasses;
}

如果有帮助,我很乐意与您分享代码。唯一的缺点是这只会处理.class文件 - 我没有添加该功能来解压缩.jars并从那里读取类文件。 (但添加它不是一个大项目。)

I'm happy to share the code with you if it helps. The only draw back is that this will only handle .class files -- I didn't add the feature to unzip .jars and read class files from there. (But it wouldn't be a huge project to add that.)

更新:我检查了上面的源代码,发现了它依赖于我们标准实用程序库中的许多辅助类。为了方便起见,我将所有需要的代码压缩,您可以从 JavaClassFinder.zip 。这将直接在Eclipse中设置,您可以获取所需代码的任何部分。

UPDATE: I checked my source code for the above, and found it depends on a lot of helper classes in our standard utility library. To make it easier, I zipped up all the code needed, which you can download from JavaClassFinder.zip. This will set up directly in Eclipse, and you can take whatever portions of the code you need.

您将在项目中找到一个名为JavaClassFinderTest.java的JUnit3测试,它将向您展示JavaClassFinder类的功能和用法。运行Junit测试所需的唯一外部依赖是Junit。

You will find a JUnit3 test in the project, called JavaClassFinderTest.java, which shows you the features and usage of the JavaClassFinder class. The only external dependency needed to run the Junit test is Junit.

此实用程序的基本用法:

Basic usage of this utility:

    JavaClassFinder classFinder = new JavaClassFinder();
    List<Class<? extends MyTagInterface>> classes = classFinder.findAllMatchingTypes(MyTagInterface.class);

这将为您提供一个List,其中包含类路径中可从MyTagInterface分配的任何类。 (例如)。希望这会有所帮助。

This will give you a List which contains any classes in the classpath which are assignable from the "MyTagInterface.class" (for example). Hope this helps.

这篇关于查找实现特定接口的所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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