博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1、Annotation
阅读量:4036 次
发布时间:2019-05-24

本文共 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的方式来表示。

原文注释是这样的:

An
annotation type declaration
specifies a new
annotation type
, a special kind
of interface type. To distinguish an annotation type declaration from a normal
interface declaration, the keyword
interface
is preceded by an at-sign (
@
).

 

我们平常的写法都是@interface,其实@和interface是两个关键词,可以用逗号隔开使用,java开发规范中也讲了这一点,只不过因为风格问题,所以一般要求都是连起来写。

原文注释:

Note that the at-sign (
@
) and the keyword
interface
are distinct tokens. It is possible to
separate them with whitespace, but this is discouraged as a matter of style.
 
 
在annotation中,定义了一些meta-annotation元注解。最常用的就是

我们自定义注解使用到的元注解也基本是这三个。

@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/

你可能感兴趣的文章
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>