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.

时间: 2024-10-27 09:31:20

How do annotations work internally--转的相关文章

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataPro

1.错误描写叙述 信息: MLog clients using java 1.4+ standard logging. 2014-7-12 19:29:20 com.mchange.v2.c3p0.C3P0Registry banner 信息: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10] 2014-7-12 19:29:21 org.springframework.w

asp.net mvc5 step by step(二)——Data Annotations(data 注释)

   以下使用在Entity Framework Code First Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去

使用Android Annotations开发

使用Android Annotations框架gradle配置1.修改Module下的build.gradle apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android

How LinkedHashSet Works Internally In Java?

LinkedHashSet is an extended version of HashSet. HashSet doesn’t follow any order where as LinkedHashSet maintains insertion order. HashSet uses HashMap object internally to store it’s elements where as LinkedHashSet uses LinkedHashMap object interna

Android Annotations开源框架学习 + eclipse(一)

1.下载 相关jar包 (下载地址:https://repo1.maven.org/maven2/org/androidannotations/androidannotations-bundle/3.3.1/androidannotations-bundle-3.3.1.zip) 2.配置eclipse 在libs下导入androidannotations-api-3.3.1.jar ,在eclipse项目中 新增compile-lib 文件夹,其中放置:androidannotations-3

Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享

原创整理不易,转载请注明出处:Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享 代码下载地址:http://www.zuidaima.com/share/1780237805931520.htm 一.准备 1. Hibernate: hibernate-distribution-3.3.2.GA, hibernate-annotations-3.4.0.GA 2. Spring: spring-fr

Hibernate 注解 (Annotations 三)多对一双向注解

注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来对这些元素进行说明,注释. 接下来我们讲解一下多对一双向注解: 我以部门表和员工表作为示例讲解. 第一步:创建实体类 Dept(部门表) package cn.onetomanydouble.entity; import org.hibernate.annotations.*; import org

会报编译器警告的Xcode 6.3新特性:Nullability Annotations

最近在用Xcode 6.3写代码,一些涉及到对象的代码会报如下编译器警告: 1 Pointer is missing a nullability type specifier (__nonnull or __nullable) 于是google了一下,发现这是Xcode 6.3的一个新特性,即nullability annotations. Nullability Annotations 我们都知道在swift中,可以使用!和?来表示一个对象是optional的还是non-optional,如v

使用Data Annotations进行手动数据验证

Data Annotations是在Asp.Net中用于表单验证的 它通过Attribute直接标记字段的有效性,简单且直观.在非Asp.Net程序中(如控制台程序),我们也可以使用Data Annotations进行手动数据验证的,一个简单的例子如下(需要添加System.ComponentModel.DataAnnotations.dll的引用): using System; using System.Collections.Generic; using System.Linq; using