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

发布于 2024-10-01 00:29:43 字数 337 浏览 9 评论 0原文

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

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

warning: No SupportedAnnotationTypes annotation found on
    my.TESTProcessor, returning an empty set.

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

Java annotation processing (since Java 6) is a very good concept, because it allows to access lots of information about classes and methods through the Element interface (and others).

But sadly, I had to find out empirically, that non-annotated classes are never passed to a custom annotation processor:

warning: No SupportedAnnotationTypes annotation found on
    my.TESTProcessor, returning an empty set.

Are my findings true? Or can I "trick" the compiler to give my custom annotation processor information about non-annotated classes as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2024-10-08 00:29:43

伟大的!

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

@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 [...] 

经过测试,有效

Great!

This gives me really all classes, not just annotated ones:

@SupportedAnnotationTypes("*")

The spec of that anotation says:

[...] 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 [...] 

Tested, works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文