读书人

[Google Guava札记](一)annotations

发布时间: 2012-11-15 15:16:13 作者: rapoo

[Google Guava笔记](一)annotations

?????? 最近在看Google Guava库的源码,为什么会看这个库的源码呢?不做解释。

??????? 这个项目包含了一些Google的核心Java1.5+类库,这些类库已经被应用在Google许多基于Java的项目中。包括:

com.google.common.annotationscom.google.common.basecom.google.common.collect com.google.common.io com.google.common.net com.google.common.primitives com.google.common.util.concurren

??????? 我大致是按照包的顺序来看的,因为时间的关系有时也会先看小一点的包,包内的顺序会根据类之间的调用做相应调整。因为网上Guava的中文资料实在太少了,所以看源码的时候根据注释和个人的理解稍微记了下笔记,给大家提供一点参考。目前水平有限,很多地方理解可能不够深入,遇到不足或错误的地方,请各位大牛多多指正!

?

??????? 这个类库比较大,所以将会分几篇日志来写,目前的计划是这样的:

    com.google.common.annotationscom.google.common.base(预算是分为两篇)com.google.common.netcom.google.common.primitivescom.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 @interfaceMyAnnotation {

String value() default"hello world";

}

????????它等价于下面的类:

public class MyAnnotationimplements java.lang.annotation.Annotation {

private String value ="hello world";

public voidsetValue(String value) {

this.value =value;

}

public StringgetValue() {

return value;

}

}

????????注释类的使用方法如下:

@MyAnnotation(value="hello")

?

Methodmethod = AnnotationTest.class.getMethod("doSomething",null);//获取被注释方法

MyAnnotationannotation = 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 enumElementType {

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有OverrideDeprecated SuppressWarnings.
????????Override只用于方法,它指明注释的方法重写父类的方法,如果不是,则编译器报错.
????????Deprecated指明该方法不建议使用
????????SuppressWarnings告诉编译器:我知道我的代码没问题,你不用吓我了,我不怕的^_^
????????这些都是MarkAnnotation,名称本身就包含了要提供的信息,不需要额外提供.

?

1.1Beta:这个注释使得被注释的API受不希望的改变的影响,甚至是删除操作。如果一个API有Beta注释,将不再遵守它所在的类库的约定。

Retention为class,Target为类型、构造函数、域、方法、类型,标记为Documented和GwtCompatible。

是标记注释,不需要给出说明。

1.2GwtCompatible:这个注释要求被注释的method返回类型是与GWT兼容的。用于type时表示希望该类型的所有方法返回类型是与GWT兼容的。常用于指示工厂方法创建的实例类型是GWT可序列化类型。

Retention为class,Target为类型和方法,标记为Documented。

是标记注释,不需要给出说明。

1.3GwtIncompatible:这个注释表示被注释的type、method或field不能用于GWT,即便它们所在的类型被注释为GwtCompatible或者可以在GWT中被访问。在GWT中使用被GwtIncompatible注释的method或field时会引起编译错误或抛出异常。

Retention为class,Target为type、method和field,标记为Documented。

是非标记注释,需要描述被注释对象与GWT不兼容的原因。一般情况下,简单的引用GWT不支持的类型或方法就足够了,例如:@GwtIncompatible Class.isInstance。

1.4VisibleForTesting:这个注释表示被注释的type或member可以被测试。标记为GwtCompatible。是标记注释,不需要给出说明。

?

以上。

?

读书人网 >编程

热点推荐