本文共 1731 字,大约阅读时间需要 5 分钟。
java.lang包有很多类及子包,因为中的类少,简单,所以就先看了下这个包。
看源码有两大工具:API 和JLS,另外看源码一定要看注释,这一点非常非常重要。
API:
JLS:
我用的是jdk1.8的两个文档。
Annotation的第一行注释就是The common interface extended by all annotation types,所有注释类型扩展的公共接口。
那么什么是annotation type?
在JLS 9.6中,描述了这个定义。annotation type其实是一种接口类型,只不过为了区分与普通接口的不同,所以使用了@+interface的方式来表示。
原文注释是这样的:
我们平常的写法都是@interface,其实@和interface是两个关键词,可以用逗号隔开使用,java开发规范中也讲了这一点,只不过因为风格问题,所以一般要求都是连起来写。
原文注释:
我们自定义注解使用到的元注解也基本是这三个。
@Documented :
如果用Documented注释类型声明,则其注释将成为已注释元素的公共API的一部分。在我们自定义注解的时候,其实这个注解用不用,并不会影响我们正常的功能,起码我测试的时候是没有问题的。
@
用来指定注释的保留策略。具体的值是由RetentionPolicy的Enum定义的,总共有三种策略:
Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.(默认值) |
Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively. |
Annotations are to be discarded by the compiler. |
@Target
用来指定当前注解类可以用在哪些对象上面,具体的值是由的Enum定义的,
Annotation type declaration |
Constructor declaration |
Field declaration (includes enum constants) |
Local variable declaration |
Method declaration |
Package declaration |
Formal parameter declaration |
Class, interface (including annotation type), or enum declaration |
Type parameter declaration |
Use of a type |
以上介绍了注解类的定义,但是定义完之后,注解类具体怎么实现,这个后面的文章再说,先干活了,,
转载地址:http://gmcdi.baihongyu.com/