天天看點

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  

 * 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:  

 *    @target({})   

 *    public @interface membertype {  

 *        ...  

 * 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:  

 *    @target({elementtype.field, elementtype.method, elementtype.field})  

 *    public @interface bogus {  

public @interface target {  

    elementtype[] value();  

   annotation_type  注釋類型聲明

   constructor  構造方法聲明

   field  字段聲明(包括枚舉常量)

   local_variable 局部變量聲明

   method  方法聲明

   package  包聲明

   parameter  參數聲明

   type  類、接口(包括注釋類型)或枚舉聲明

3、retention.class

 * @(#)retention.java   1.6 05/11/17  

 * 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.  

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

@target(elementtype.method)  

 * 使用了預設值  

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(){  

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  

上一篇: md5