JDK8—lang.annotation

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.

在这里插入图片描述

(七)元注解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值