Java 注释处理:可以访问非注释类的“元素”吗?

jav*_*top 3 java annotations javac annotation-processing

Java 注释处理(自 Java 6 起)是一个非常好的概念,因为它允许通过Element接口(和其他接口)访问有关类和方法的大量信息。

但遗憾的是,我不得不凭经验发现,非注释类永远不会传递给自定义注释处理器:

warning: No SupportedAnnotationTypes annotation found on
    my.TESTProcessor, returning an empty set.
Run Code Online (Sandbox Code Playgroud)

我的发现是真的吗?或者我可以“欺骗”编译器以提供有关非注释类的自定义注释处理器信息吗?

jav*_*top 5

伟大的!

这给了我真正的所有类,而不仅仅是带注释的类:

@SupportedAnnotationTypes("*")
Run Code Online (Sandbox Code Playgroud)

该注释的规范说:

[...] 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 [...] 
Run Code Online (Sandbox Code Playgroud)

经测试,有效