我怎样才能找到具有特定方法的注释类路径中的所有类? [英] How can I find all classes on the classpath that have a specific method annotation?
问题描述
我想实现一个intialization机制是注释基于Java编写的。具体来说,我有一个注释,我定义的:
I want to implement an intialization mechanism that is annotation-based in Java. Specifically, I have an annotation I've defined:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Initialization {
/**
* If the eager initialization flag is set to <code>true</code> then the
* initialized class will be initialized the first time it is created.
* Otherwise, it will be initialized the first time it is used.
*
* @return <code>true</code> if the initialization method should be called
* eagerly
*/
boolean eager() default false;
}
此外,我有一个接口:
public interface SomeKindOfBasicInterface {}
我想找到我的classpath中 SomeKindOfBasicInterface
类,它具有一个在 @Initialization
标注的每一个执行方法。我期待在Spring的 MetaDataReader
工具,它看起来像最好的办法推迟加载其他 SomeKindOfBasicInterface
的实现,同时我在做这个......但我不知道如何做搜索就像我描述。任何提示?
I want to find every implementation of the SomeKindOfBasicInterface
class on my classpath that has the @Initialization
annotation on a method. I'm looking at Spring's MetaDataReader
tools, which look like the best way to defer loading the other SomeKindOfBasicInterface
implementations while I'm doing this... but I'm not sure how to do a search like I'm describing. Any tips?
推荐答案
您可以使用思考,这是一个Java运行时元分析工具。我用它来获得一个给定类型的所有亚型,但它可以处理你的情况也是如此。
You could use Reflections, which is a Java runtime metadata analysis tool. I've used it to get all subtypes of a given type, but it can handle your case as well.
这篇关于我怎样才能找到具有特定方法的注释类路径中的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!