最近在看Google Guava库的源码,为什么会看这个库的源码呢?不做解释。
这个项目包含了一些Google的核心Java1.5+类库,这些类库已经被应用在Google许多基于Java的项目中。包括:
- com.google.common.annotations
- com.google.common.base
- com.google.common.collect
- com.google.common.io
- com.google.common.net
- com.google.common.primitives
- com.google.common.util.concurren
我大致是按照包的顺序来看的,因为时间的关系有时也会先看小一点的包,包内的顺序会根据类之间的调用做相应调整。因为网上Guava的中文资料实在太少了,所以看源码的时候根据注释和个人的理解稍微记了下笔记,给大家提供一点参考。目前水平有限,很多地方理解可能不够深入,遇到不足或错误的地方,请各位大牛多多指正!
这个类库比较大,所以将会分几篇日志来写,目前的计划是这样的:
- com.google.common.annotations
- com.google.common.base(预算是分为两篇)
- com.google.common.net
- com.google.common.primitives
- com.google.common.collect(预算是分为两篇,不够就再加,这个包实在是太大了...以前是一个独立的项目Google Collections,后来加到Guava中了,只用了一个包,坑爹啊)
- com.google.common.io(预算是分为两篇)
- com.google.common.util.concurrent(预算是分为两篇)
现在开始正文,annotations:提供了常用的注释类型。
以下是java中annotations的相关知识,从网上找到的,引自:http://www.iteye.com/topic/171412,感谢lingzantia。
注释类是实现
java.lang.annotation.Annotation接口的类,用于向程序分析工具或虚拟机提供注释信息,它和一般的类在定义和使用方式上有所不同。
注释类的一般形式是:
public @interface
MyAnnotation {
String value() default
"hello world";
}
它等价于下面的类:
public class MyAnnotation
implements java.lang.annotation.Annotation {
private String value =
"hello world";
public void
setValue(String value) {
this.value =
value;
}
public String
getValue() {
return value;
}
}
注释类的使用方法如下:
@MyAnnotation(value="hello")
Method
method = AnnotationTest.class.getMethod("doSomething",
null);//
获取被注释方法
MyAnnotation
annotation = method.getAnnotation(MyAnnotation .class);//
获取注释对象
String value =
annotation.value();
@interface
表示该类是注释类,定义注释类时不能再继承或实现其它类。
java.lang.annocation
包中常用的
class
有:接口
Annotation
、枚举类型
ElementType
和
RetentionPolicy
、注释类
Documented
、
Inherited
、
Retention
和
Target
。
public enum RetentionPolicy {
SOURCE, //编译器处理完
Annotation后不存储在
class中
CLASS,
//编译器把
Annotation存储在
class中,这是默认值
RUNTIME
//编译器把
Annotation存储在
class中,可以由虚拟机读取,反射需要
}
public enum
ElementType {
TYPE, //指定适用点为
class,
interface, enum
FIELD, //指定适用点为
field
METHOD, //指定适用点为
method
PARAMETER, //指定适用点为
method的
parameter
CONSTRUCTOR, //指定适用点为
constructor
LOCAL_VARIABLE, //指定使用点为局部变量
ANNOTATION_TYPE, //指定适用点为
annotation类型
PACKAGE //指定适用点为
package
}
java.lang.annotation.Documented用于指定该
Annotation是否可以写入
javadoc中
.
java.lang.annotation.Inherited用于指定该
Annotation用于父类时是否能够被子类继承
.
示例如下:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
//这个
Annotation可以被写入
javadoc
@Inherited //这个
Annotation可以被继承
//表示这个
Annotation只能用于注释构造子和方法
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})
@Retention(RetentionPolicy.CLASS) //表示这个
Annotation存入
class但
vm不读取
public @interface MyAnnotation {
String value() default "hahaha";
}
java.lang.reflect.AnnotatedElement接口提供了以下四个方法来访问注释:
public Annotation getAnnotation(Class annotationType);
public Annotation[] getAnnotations();
public Annotation[] getDeclaredAnnotations();
public boolean isAnnotationPresent(Class annotationType);
Class、
Constructor、
Field、
Method、
Package等都实现了该接口
,可以通过这些方法访问
Annotation信息
,前提是要访问的
Annotation指定
Retention为
RUNTIME.
Java内置的
annotation有
Override
Deprecated SuppressWarnings.
Override只用于方法
,它指明注释的方法重写父类的方法
,如果不是
,则编译器报错
.
Deprecated指明该方法不建议使用
SuppressWarnings告诉编译器
:我知道我的代码没问题
,你不用吓我了
,我不怕的
^_^
这些都是
Mark
Annotation,名称本身就包含了要提供的信息
,不需要额外提供
.
1.1 Beta:这个注释使得被注释的
API受不希望的改变的影响,甚至是删除操作。
如果一个
API有
Beta注释,将不再遵守它所在的类库的约定。
Retention为
class,
Target为类型、构造函数、域、方法、类型,标记为
Documented和
GwtCompatible。
是标记注释,不需要给出说明。
1.2 GwtCompatible:这个注释要求被注释的
method返回类型是与
GWT兼容的。用于
type时表示希望该类型的所有方法返回类型是与
GWT兼容的。常用于指示工厂方法创建的实例类型是
GWT可序列化类型。
Retention为
class,
Target为类型和方法,标记为
Documented。
是标记注释,不需要给出说明。
1.3 GwtIncompatible:这个注释表示被注释的
type、
method或
field不能用于
GWT,即便它们所在的类型被注释为
GwtCompatible或者可以在
GWT中被访问。在
GWT中使用被
GwtIncompatible注释的
method或
field时会引起编译错误或抛出异常。
Retention为
class,
Target为
type、
method和
field,标记为
Documented。
是非标记注释,需要描述被注释对象与
GWT不兼容的原因。一般情况下,简单的引用
GWT不支持的类型或方法就足够了,例如:
@GwtIncompatible
Class.isInstance。
1.4 VisibleForTesting:这个注释表示被注释的
type或
member可以被测试。标记为
GwtCompatible。是标记注释,不需要给出说明。
以上。
分享到:
相关推荐
├─Google Guava 第20讲-实战:手动实现一个EventBus-01快速搭建程序结构.wmv ├─Google Guava 第21讲-实战:手动实现一个EventBus-02快速实现程序功能.wmv ├─Google Guava 第22讲-实战:手动实现一个EventBus...
使用google guava 实现定时缓存功能
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
Getting Started with Google Guava, guava的技术参考文档
Google Guava 官方教程 - 详情:https://blog.csdn.net/Dream_Weave/article/details/86252213
Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法...
Google Guava 官方教程 - v1.1
google guava框架,包括Getting Started with Google Guava、google cache、base、io等介绍
Google Guava 官方教程 v1.1 2018-07-22 https://github.com/tianbian224/GuavaLearning/blob/master/Google%20Guava%20%E5%AE%98%E6%96%B9%E6%95%99%E7%A8%8B%20-%20v1.1.pdf
Getting Started with Google Guava code
此文档为Guava中最流行和最强大的功能,提供更具可读性和解释性的说明
Getting Started with Google Guava.pdf -Bill Bejeck
标签:google、guava、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
Google Guava 官方教程
com.google.guava_1.6.0.jar.zip com.google.guava_1.6.0.jar.zip
Google Guava 官方教程,电子书,epub格式,可用在手机端阅读
19的guava包 希望对大家有帮助
谷歌guava的jar包和源码,很值得收藏。直接通过exlipse的build path,导入jar包和源码,可供开发和学习
标签:google、guava、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库。 目前主要包含: com.google.common.annotations com.google.common.base ...