在编译时强制执行 Java 注释 [英] Enforcing java annotations at compile time

查看:34
本文介绍了在编译时强制执行 Java 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:我想确保在编译时在类中存在注释.这可能吗 ?我意识到 annoataions 本身就是类,所以我假设是这样 - 但我只是不确定在我的类中在哪里以及如何在语法上强制/实现这样的结构.

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.

推荐答案

您可以编写注释处理器以在编译时运行任意逻辑.

You can write an annotation processor to run arbitrary logic at compile time.

从注解处理器中,您可以执行诸如检查类是否具有特定结构或成员(如果该类上存在特定注解)的操作.它们非常灵活 - 有关您可以用它们做什么的更多想法,请查看 API.Eclipse 和 Netbeans 等主要 IDE 也支持它们.

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.

这篇关于在编译时强制执行 Java 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆