annotation
The common interface extended by all annotation types. Note that an
interface that manually extends this one does not define an
annotation type. Also note that this interface does not itself define
an annotation type.
(一)注解的定义
Annotation是Java5开始引入的新特征,注解提供了一种安全的类似注释的机制,用来将任何信息或者元数据(metadata)与程序元素(类,方法,成员变量等)进行关联。为程序的元素(类,方法,成员变量) 加上更直观更明了的说明,这些说明信息是与程序的业务逻辑无关,并且供指定的工具或框架使用。Annotation像一种修饰符一样,应用于包、类型、构造方法、成员变量、参数及本地变量的生命语句中。
(二)注解的原理
注解本质是一个继承了Annotation的特殊接口,其具体实现类是Java运行时生成的动态代理类。而我们通过反射获取注解时,返回的是Java运行时生成的动态代理对象$Proxy1。通过代理对象调用自定义注解(接口)的方法,会最终调用AnnotationInvocationHandler的invoke方法。该方法会从memberValues这个Map中索引出对应的值,而memberValues的来源是Java常量池。
(三)注解的分类
元注解:注解的注解,可以为注解添加信息描述。主要是用于自定义的注解,元注解在JDK中已被定义.。
(四)注解的异常/错误
4.1、AnnotationFormatError
注解格式错误。
4.2、AnnotationTypeMismatchException
注解类型不匹配异常。
4.3、IncompleteAnnotationException
注解信息不完善的异常。
(五)ElementType
The constants of this enumerated type provide a simple classification
of the syntactic locations where annotations may appear in a Java
program. These constants are used in {@link Target
java.lang.annotation.Target} meta-annotations to specify where it is
legal to write annotations of a given type.
(六)RetentionPolicy
Annotation retention policy. The constants of this enumerated type
describe the various policies for retaining annotations. They are
used in conjunction with the {@link Retention} meta-annotation type to
specif how long annotations are to be retained.