查找实现接口的Java类 [英] Find Java classes implementing an interface

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

问题描述

前一段时间,我遇到了一段代码,它使用了一些标准的Java功能来定位实现给定接口的类。我知道函数隐藏在一些非逻辑位置,但它们可以用于其他类,因为包含的名称是隐含的。那时我不需要它,所以我忘了它,但现在我做了,我似乎无法再找到这些功能。在哪里可以找到这些函数?

Some time ago, I came across a piece of code, that used some piece of standard Java functionality to locate the classes that implemented a given interface. I know the functions were hidden in some non-logical place, but they could be used for other classes as the package name implied. Back then I did not need it, so I forgot about it, but now I do, and I can't seem to find the functions again. Where can these functions be found?

编辑:我不是在寻找任何IDE函数或任何东西,而是可以在Java应用程序中执行的东西。

I'm not looking for any IDE functions or anything, but rather something that can be executed within the Java application.

推荐答案

前一段时间,我整理了一个包,用于做你想做的事情,等等。 (我需要它用于我正在编写的实用程序)。它使用 ASM 库。你可以使用反射,但ASM结果表现更好。

Awhile ago, I put together a package for doing what you want, and more. (I needed it for a utility I was writing). It uses the ASM library. You can use reflection, but ASM turned out to perform better.

我将我的包放在我网站上的开源库中。该库位于: http://software.clapper.org/javautil/ 。您想从使用 ClassFinder 课程开始。

I put my package in an open source library I have on my web site. The library is here: http://software.clapper.org/javautil/. You want to start with the with ClassFinder class.

我为它编写的实用程序是一个RSS阅读器,我仍然每天都在使用它,因此代码确实会被运用。我使用ClassFinder来支持RSS阅读器中的插件API;在启动时,它在几个目录树中查找包含实现特定接口的类的jar和类文件。它比你预期的要快得多。

The utility I wrote it for is an RSS reader that I still use every day, so the code does tend to get exercised. I use ClassFinder to support a plug-in API in the RSS reader; on startup, it looks in a couple directory trees for jars and class files containing classes that implement a certain interface. It's a lot faster than you might expect.

该库是BSD许可的,因此你可以安全地将它与你的代码捆绑在一起。来源可用。

The library is BSD-licensed, so you can safely bundle it with your code. Source is available.

如果这对您有用,请自己帮助。

If that's useful to you, help yourself.

更新:如果您正在使用Scala,您可能会发现此库对Scala更友好。

Update: If you're using Scala, you might find this library to be more Scala-friendly.

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

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