张龙 Annotation学习笔记2

本集主要讲述@Retention及@Target2个注解,顺带提一下@Documented这个注解

 

1.关于@Retention这个注解

Retention翻译成中文是“保留”的意思,RetentionPolicy是“保留策略”。

简要描述:指示注解类型的注解要保留多久。如果注解类型声明中不存在 Retention 注解,则保留策略默认为  RetentionPolicy.CLASS

每一个Retention都要给他一个RetentionType,RetentionType是一个枚举类型(具体可以查看API文档),它有3种取值:SOURCE,CLASS,RUNTIME,区别如下:

(a)SOURCE:表示该注解只会存在于JAVA源文件中,不会编译到class文件里面去,更不会在运行期通过反射的方式获   取到。

(b)CLASS:表示该注解会随着JAVA源代码一起编译到class文件里面去,但不会在运行期通过反射的方式获取到。

(c)RUNTIME:表示该注解会随着JAVA源代码一起编译到class文件里面去,并且会在运行期通过反射的方式获取到。

 

请看一个示例:

首先定义一个注解:

package com.shengsiyuan.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


//注解也可以修饰注解,该注解修饰下面自定义的注解,通过设定
//RetentionPolicy的值为RUNTIME表示该自定义的注解会被编
//译到CLASS文件当中,而且可以在运行期通过反射的方式获取到(具体请查看一遍API文档,很有必要!)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
	String hello() default "shengsiyuan";

	String world();
}

然后定义一个类,用这个Annotation去修饰

package com.shengsiyuan.annotation;

@MyAnnotation(hello = "beijing", world = "shanghai")
public class MyTest
{       
        //一个方法可以被多个注解所修饰。
	@MyAnnotation(hello = "tianjin", world = "shangdi")
	@Deprecated
	@SuppressWarnings("unchecked") 
	public void output()
	{
		System.out.println("output something!");
	}
}

 接着定义一个类,并通过反射相关API去获得自定义注解的相关信息

package com.shengsiyuan.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

//该类拿到修饰MyTest里方法的Annotation
public class MyReflection
{
	public static void main(String[] args) throws Exception
	{
		MyTest myTest = new MyTest();
		
		Class<MyTest> c = MyTest.class;
		
		Method method = c.getMethod("output", new Class[]{});
		
                //能够进入到if语句里面来说明MyAnnotation的RetentionPolicy的值为Runtime(为什么请查API文档!)
		if(method.isAnnotationPresent(MyAnnotation.class))
		{
			method.invoke(myTest, new Object[]{});
			
			MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
			
			String hello = myAnnotation.hello();
			String world = myAnnotation.world();
			
			System.out.println(hello + ", " + world);
		}

		//只会得到Myannotation和Deprecated两个Annotation,因为只有这两个Annotation的RetentionPolicy
                //的值为Runtime,只有RetentionPolicy的值为Runtime才会在运行期通过反射相关API拿到Annotation的相关信息。
		Annotation[] annotations = method.getAnnotations();
		
		for(Annotation annotation : annotations)
		{
			System.out.println(annotation.annotationType().getName());
		}
	}
}

 

2.关于@Target这个注解(建议去读一读API文档,介绍的很详细)

 简要描述:指示注解类型所适用的程序元素的种类。如果注解类型声明中不存在 Target 元注解,则声明的类型可以用在任一程序元素上。

每一个Target都要给他一个ElementType,ElementType是一个枚举类型(具体可以查看API文档),它有8种取值:SOURCE,CLASS,RUNTIME,区别如下:

(a)ANNOTATION_TYPE:表示该注解可以去修饰另外一个注解

(b)COUNSTRUCTOR:表示该注解可以修饰构造方法

(c)FIELD:表示该注解可以修饰成员变量

(d)LOCAL_VARIABLE:表示该注解可以修饰局部变量

(e)METHOD:表示该注解可以修饰普通方法

(f)PACKAGE:表示该注解可以修饰包

(g)PARAMETER:表示该注解可以修饰方法参数

(h)TYPE:表示该注解可以修饰类、接口(包括注解类型)或枚举声明

 

请看示例:

package com.shengsiyuan.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)//表示该自定义注解只能用于修饰方法
public @interface MyTarget
{
	String value();
}

接着定义一个类:

package com.shengsiyuan.annotation;

public class MyTargetTest
{
	@MyTarget("hello")
	public void doSomething()
	{
		System.out.println("hello world");
	}
}

 当把该自定义的注解放到方法上面后编译器不报错时,说明我们的实验是成功的(不需要写main方法进行测试)

 

对以上2个注解的总结:Retention与Target都是注解,Retention与RetentionPolicy搭配,Target与ElementType搭配。

 

3.关于@Documented(了解就行)

不多做描述,请看示例:

package com.shengsiyuan.annotation;

import java.lang.annotation.Documented;

@Documented   //当一个注解被@Documented 修饰后表示被该注解修饰的对象(类或方法或其他)在生成JAVA DOC文档时,该注解会被加到修饰的对象的上面
public @interface DocumentedAnnotation
{
	String hello();
}

 然后用该注解去修饰某个方法

package com.shengsiyuan.annotation;

public class DocumentedTest
{
	@DocumentedAnnotation(hello = "welcome")
	public void method()
	{
		System.out.println("hello world");
	}
}

 当对DocumentedTest所在的包或工程生成JAVA DOC文档的时候,会发现自定义的注解会出现在method方法的上面

 

posted @ 2011-08-04 10:14  focus~wow  阅读(123)  评论(0编辑  收藏  举报