好记性不如铅笔头

java, think_in_java, 编程

《Java编程思想》读书笔记:annotation

今天重新学习了下Java中的【 annotation 】,这里备份备份下代码:

github:

https://github.com/cstriker1407/think_in_java 】

CONTENTS

几种常见的自定义的annotation:

@Target(ElementType.TYPE)//这个标注应用于类
@Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
@Documented//将此注解包含在javadoc中
@Inherited//本注解可以被继承
@interface FirstDesc
{
	public String value();//value比较特殊,它在被指定参数的时候可以不用显示的写出来
	
}

@Target(ElementType.TYPE)//这个标注应用于类
@Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
@Documented//将此注解包含在javadoc中
@Inherited//本注解可以被继承
@interface SecondDesc
{
	public String des();
}

@Target(ElementType.TYPE)//这个标注应用于类
@Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
@Documented//将此注解包含在javadoc中
@interface ThirdDesc
{
	public int id();
	public String des();
}

@Target(ElementType.TYPE)//这个标注应用于类
@Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
@Documented//将此注解包含在javadoc中
@interface FourthDesc
{
	public int id();
	public String des() default "This is Fourth Desc" ;
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MethodDesc
{
	public int id();
	public String des() default "This ia a method desc";
}

 说明:

从代码中可以看出,【 firstdesc 】和【 seconddesc 】可以被继承,【 firstdesc 】的【 value 】属性可以在使用注解时省略,【 fourthdesc 】有默认值。

使用方式:

	@FirstDesc("This is a first description")
	@SecondDesc(des="This is a second description")
	@ThirdDesc(id=3,des="This is a third description")
	@FourthDesc(id=4)
	static class FirstDescClass
	{
	}
	
	/* 
	 *  FourthDescClassB 继承了FirstDescClass,也继承了 FirstDescClass 的各种注解,但是FirstDescClass的各种注解里面
	 *  只有 @FirstDesc @SecondDesc设置了@Inherited属性,因此只有这两个是可以继承的。
	 */
	@FirstDesc(value="This is FourthDescClassB a first description")
	@FourthDesc(id=4,des="this FourthDescClassB is fourth desc class B")
	static class FourthDescClassB extends FirstDescClass
	{
	}
public class AnnotationTest
{
	@MethodDesc(id=0,des="this is test method")
	public static void test()
	{
        }
}

 说明:

类【 FourthDescClassB 】继承自【 FirstDescClass 】,也就会继承【 FirstDescClass 】的各种注解,但是只有【 firstdesc 】和【 seconddesc 】允许注解。

测试方法:

public class AnnotationTest
{
	@MethodDesc(id=0,des="this is test method")
	public static void test()
	{
		Annotation[] annArrs = FirstDescClass.class.getAnnotations();
		for (Annotation annotation : annArrs)
		{
			System.out.println(annotation.toString());
		}
		System.out.println("==");
		FourthDesc fourthDesc = FourthDescClassB.class.getAnnotation(FourthDesc.class);
		System.out.println("fourthDesc:id=" + fourthDesc.id() + " des:" + fourthDesc.des());
		System.out.println(fourthDesc.toString());
		System.out.println("==");
		annArrs = FourthDescClassB.class.getAnnotations();
		for (Annotation annotation : annArrs)
		{
			System.out.println(annotation.toString());
		}
		
		System.out.println("==");
		Method testM = null;
		try
		{
			testM = AnnotationTest.class.getMethod("test", null);
			annArrs = testM.getAnnotations();
			for (Annotation annotation : annArrs)
			{
				System.out.println(annotation.toString());
			}
		}
		catch (Exception e)
		{
		}
       }
}

结果输出:

@yeah.cstriker1407.think_in_java.Annotation.FirstDesc(value=This is a first description)
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=This is Fourth Desc, id=4)
@yeah.cstriker1407.think_in_java.Annotation.SecondDesc(des=This is a second description)
@yeah.cstriker1407.think_in_java.Annotation.ThirdDesc(id=3, des=This is a third description)
==
fourthDesc:id=4 des:this FourthDescClassB is fourth desc class B
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=this FourthDescClassB is fourth desc class B, id=4)
==
@yeah.cstriker1407.think_in_java.Annotation.FirstDesc(value=This is FourthDescClassB a first description)
@yeah.cstriker1407.think_in_java.Annotation.FourthDesc(des=this FourthDescClassB is fourth desc class B, id=4)
@yeah.cstriker1407.think_in_java.Annotation.SecondDesc(des=This is a second description)
==
@yeah.cstriker1407.think_in_java.Annotation.MethodDesc(des=this is test method, id=0)

 

发表评论

4 × 1 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据