【发布时间】:2012-04-20 06:47:10
【问题描述】:
我已经并遵循了 Internet 上的几个注释处理工具 (APT) 指南(例如 1 和 2),并设法让它在编译器/构建时运行,甚至在 Eclipse 中运行。
有没有一种方法可以在运行时使用 APT 来使用我的注释获取类型(类)列表。
我写了类似的东西:
@SupportedAnnotationTypes("com.domain.MyAnnotation")
public class MyAbstractProcessor extends AbstractProcessor {
public static Map<Element, MyAnnotation> patches = new HashMap<Element, MyAnnotation>();
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) {
// Get all classes that has the annotation
Set<? extends Element> classElements = roundEnvironment.getElementsAnnotatedWith(MyAnnotation.class);
// For each class that has the annotation
for (final Element classElement : classElements) {
patches.put(classElement, annotation);
因此 MyAbstractProcessor.patches 将使用注释填充类列表。一个崇高的想法,除了这个 APT 在构建时执行而不是运行时的缺陷。
甚至可以在运行时使用 APT 吗?
还是我使用了错误的框架来获得我想要的东西?
【问题讨论】:
标签: java annotations apt