Java 注释处理(自 Java 6 起)是一个非常好的概念,因为它允许通过 Element
接口(interface)(和其他接口(interface))访问有关类和方法的大量信息。
但遗憾的是,我不得不根据经验发现,非注释类永远不会传递给自定义注释处理器:
warning: No SupportedAnnotationTypes annotation found on
my.TESTProcessor, returning an empty set.
我的发现是真的吗?还是我可以“欺骗”编译器,让我的自定义注释处理器也提供有关未注释 类的信息?
最佳答案
太棒了!
这给了我真正的所有类,而不仅仅是带注释的类:
@SupportedAnnotationTypes("*")
该注释的规范说:
[...] Finally, "*" by itself represents the set of all annotation types,
including the empty set. Note that a processor should not claim "*"
unless it is actually processing all files [...]
经过测试,有效!
关于Java注解处理: Accessing "Element"s of non-annotated classes possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4117526/