在编译时强制执行 java 注释
您好:我想确保在编译时类中存在注释。这可能吗?我意识到注释本身就是类,所以我假设是这样 - 但我只是不确定语法上在哪里以及如何在我的类中强制/实现这样的结构。
Hi : I wanted to make sure that an annotation is present at compile time in a class. Is this possible ? I realize that annoataions are, themselves, classes, so I assume so - but Im just not sure syntactically where and how to enforce/implement such a structure in my classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写注释处理器来在编译时运行任意逻辑。
从注释处理器中,您可以执行诸如检查类是否具有特定结构或成员(如果该类上存在特定注释)的操作。它们非常灵活 - 要详细了解可以使用它们做什么,请查看 API.它们还受到 Eclipse 和 Netbeans 等主要 IDE 的支持。
关于编写注释处理器的介绍是 此处(链接已失效,部分复制此处)。
You can write an annotation processor to run arbitrary logic at compile time.
From an annotation processor, you can do things like check whether a class has a particular structure or member present if a particular annotation exists on that class. They are pretty flexible - for more of an idea of what you can do with them check out the API. They are also supported in major IDEs such as Eclipse and Netbeans.
An introduction to writing an annotation processor is here (link dead, partial copy here).