How do annotations work internally--转

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How do annotations work internally--转相关的知识,希望对你有一定的参考价值。

原文地址:http://stackoverflow.com/questions/18189980/how-do-annotations-work-internally

The first main distinction between kinds of annotation is whether they‘re used at compile time and then discarded (like @Override) or placed in the compiled class file and available at runtime (like Spring‘s @Component). This is determined by the @Retention policy of the annotation. If you‘re writing your own annotation, you‘d need to decide whether the annotation is helpful at runtime (for autoconfiguration, perhaps) or only at compile time (for checking or code generation).

When compiling code with annotations, the compiler sees the annotation just like it sees other modifiers on source elements, like access modifiers (public/private) or final. When it encounters an annotation, it runs an annotation processor, which is like a plug-in class that says it‘s interested a specific annotation. The annotation processor generally uses the Reflection API to inspect the elements being compiled and may simply run checks on them, modify them, or generate new code to be compiled. @Override is an example of the first; it uses the Reflection API to make sure it can find a match for the method signature in one of the superclasses and uses the Messagerto cause a compile error if it can‘t.

There are a number of tutorials available on writing annotation processors; here‘s a useful one. Look through the methods on the Processor interface for how the compiler invokes an annotation processor; the main operation takes place in the process method, which gets called every time the compiler sees an element that has a matching annotation.

 

以上是关于How do annotations work internally--转的主要内容,如果未能解决你的问题,请参考以下文章

Spring Annotation Processing: How It Works--转

[2016-4-1]OMG美语每日笔记-What would you do to get know your new colleagues at work?

How do I install uudecode?

How do I code well?

Spring Annotation是怎么工作的?

[转]How do I use variables in Oracle SQL Developer?