java自定义注解

【第一部分】

首先了解一下java1.5起默认的三个annotation类型:

@override:只能用在方法上,用来告诉人们这个方法是改写的父类的

@Deprecated:建议别人不要使用旧的api的时候使用的,编译的时候会产生警告信息,可以设定在程序的所有元素上。

@SuppressWarnings:这一类型可以暂时把一些警告信息消除。

【第二部分】

先讲一下怎么自己设计一个annotation,最好的就是读以下jdk自带的annotation源文件

1、源文件Documented.class

/*
 * @(#)Documented.java	1.6 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang.annotation;

/**
 * Indicates that annotations with a type are to be documented by javadoc
 * and similar tools by default.  This type should be used to annotate the 
 * declarations of types whose annotations affect the use of annotated
 * elements by their clients.  If a type declaration is annotated with
 * Documented, its annotations become part of the public API
 * of the annotated elements.
 *
 * @author  Joshua Bloch
 * @version 1.6, 11/17/05
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Documented:作用是在生成javadoc文档的时候将该Annotation也写入到文档中。

2、Target.class

/*
 * @(#)Target.java	1.6 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang.annotation;

/**
 * Indicates the kinds of program element to which an annotation type
 * is applicable.  If a Target meta-annotation is not present on an
 * annotation type declaration, the declared type may be used on any
 * program element.  If such a meta-annotation is present, the compiler
 * will enforce the specified usage restriction.
 *
 * For example, this meta-annotation indicates that the declared type is
 * itself a meta-annotation type.  It can only be used on annotation type
 * declarations:
 * <pre>
 *    @Target(ElementType.ANNOTATION_TYPE)
 *    public @interface MetaAnnotationType {
 *        ... 
 *    }
 * </pre>
 * This meta-annotation indicates that the declared type is intended solely
 * for use as a member type in complex annotation type declarations.  It
 * cannot be used to annotate anything directly:
 * <pre>
 *    @Target({}) 
 *    public @interface MemberType {
 *        ...
 *    }
 * </pre>
 * It is a compile-time error for a single ElementType constant to
 * appear more than once in a Target annotation.  For example, the
 * following meta-annotation is illegal:
 * <pre>
 *    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
 *    public @interface Bogus {
 *        ...
 *    }
 * </pre>
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

@Target 指示注释类型所适用的程序元素的种类。如果不指定则可以用在任一程序元素上。取值为枚举的数据类型java.lang.annotation.ElementType,枚举项含义为:

   ANNOTATION_TYPE  注释类型声明

   CONSTRUCTOR  构造方法声明

   FIELD  字段声明(包括枚举常量)

   LOCAL_VARIABLE 局部变量声明

   METHOD  方法声明

   PACKAGE  包声明

   PARAMETER  参数声明

   TYPE  类、接口(包括注释类型)或枚举声明

3、Retention.class

/*
 * @(#)Retention.java	1.6 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang.annotation;

/**
 * Indicates how long annotations with the annotated type are to
 * be retained.  If no Retention annotation is present on
 * an annotation type declaration, the retention policy defaults to
 * <tt>RetentionPolicy.CLASS</tt>.
 *
 * <p>A Target meta-annotation has effect only if the meta-annotated
 * type is use directly for annotation.  It has no effect if the meta-annotated
 * type is used as a member type in another annotation type.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}

@Retention 指示注释类型的注释要保留多久。默认为 RetentionPolicy.CLASS。取值为枚举:

java.lang.annotation.RetentionPolicy:

   CLASS  编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释。

   RUNTIME  编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。

   SOURCE  编译器要丢弃的注释。

【第三部分】

下面我们自己来写一个注解类使用

1、Description

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Documented//作用是在生成javadoc文档的时候将该Annotation也写入到文档中。
@Retention(RetentionPolicy.RUNTIME)//指示注释类型的注释要保留多久。默认为 RetentionPolicy.CLASS。取值为枚举
/**
 * 注解中定义的属性如果名称为 value, 此属性在使用时可以省写属性名
 */
public @interface Description {
	String value() ;
}

2、Name

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
/**
 * 使用了默认值
 */
public @interface Name {
	String work() default "JAVA";
	String community() default "BLOG";
}

3、hzu_Opensource类

@Description("吴海旭的社区测试")
public class hzu_Opensource {
	@Name(work="sales",community="ITeye")
	public String getName(){
		return null ;
	}
	@Name(community="csdn",work="IT")
	public String getName2(){
		return "csdn" ;
	}
	@Name
	public String getName3(){
		return null ;
	}
}

4、测试类TestAnnotation

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;


public class TestAnnotation {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		 String CLASS_NAME = "hzu_Opensource" ;
		 Class test = Class.forName(CLASS_NAME) ;
		 
		 boolean flag = test.isAnnotationPresent(Description.class) ;
		 if(flag){
			 Description des = (Description)test.getAnnotation(Description.class) ;
			 System.out.println("描述: " + des.value());
			 System.out.println("---------------");
		 }
		 Method[] method = test.getMethods() ;
		 Set<Method> set = new HashSet<Method>() ;
		 for(int i=0;i<method.length;i++){
			 boolean otherFlag = method[i].isAnnotationPresent(Name.class) ;
			 if(otherFlag) set.add(method[i]) ;
		 }
		 for(Method m:set){
			 Name name = m.getAnnotation(Name.class) ;
			 System.out.println(name.work());
			 System.out.println("社区:"  + name.community());
		 }
	}
}

结果为:

描述: 吴海旭的社区测试
---------------
JAVA
社区:BLOG
IT
社区:csdn
sales
社区:ITeye


java自定义注解,布布扣,bubuko.com

java自定义注解

上一篇:C++windows内核编程笔记day13 进程、线程与信号量


下一篇:C++windows内核编程笔记day11 win32静态库和动态库的使用