问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/8981540/enforcing-java-annotations-at-compile-time