牧者

大风起兮云飞扬

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

JAVA元数据注释初探《二》

JAVA的元数据功能是JDK1.5才开始支持的,以前都没有,正因为是新支持的,所以有关于它的介绍挺少,用处也不太多,最近发现很多框架都可以把它用来配置一些东西,以代替以前比较复杂的XML配置.

/*   
 * MyType.java   
 *   
 * Created on 2006年12月7日, 下午3:40   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
@Retention(RetentionPolicy.RUNTIME)    
@Target({ElementType.TYPE,ElementType.METHOD})    
public @interface MyType {    
    String authorName();    
    String lastModified();    
    String bugFixes() default "ok";    
}

  

这里我定义了一个我自己的注释类,声明方式和声明接口差不多,只不过在interface前面多了一个@符号. 

注释类也可以用注释类注释,如此下去.

@Retention(RetentionPolicy.RUNTIME)

这句表示它的保存范围是到RUNTIME,也就是运行时,这样在类运行的时候,我们也可以取到有关它的信息.

@Target({ElementType.TYPE,ElementType.METHOD})  

这句表示它的适用对象,它可以用在哪里地方,我这里定义的是它可以用在类的定义和方法的定义上

然后我们看我们是怎么为我们写的类加上注释的

/*   
 * Test1.java   
 *   
 * Created on 2006年12月7日, 下午3:34   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
@MyType(authorName="hadeslee",lastModified="20061207")    
public class Test1 {    
        
    /** Creates a new instance of Test1 */   
    public Test1() {    
    }    
    @Deprecated   
    @MyType(authorName="hadeslee",lastModified="20061207",bugFixes="what")    
    public void doSth(){    
               
    }    
    @MyType(authorName="hadeslee",lastModified="20061207",bugFixes="what")    
    public void doAnother(){    
            
    }    
}    

  加了元数据的类和不加元数据的类差不多,只不过如果你的元数据注释如果是运行时的话,你的类文件可能会比不加元数据大一些,因为它必须把一些注释的信息写入到class文件中去,我们已经注释了我们的类,现在我们来看一下,我们如何去取我们的注释。

/*   
 * GetAnno.java   
 *   
 * Created on 2006年12月7日, 下午3:46   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
import java.lang.reflect.*;    
public class GetAnno {    
        
    /** Creates a new instance of GetAnno */   
    public GetAnno() {    
    }    
    public static void main(String[] args)throws Exception {    
        Test1 t=new Test1();    
        Class c=Test1.class ;    
        Annotation[] as= c.getDeclaredAnnotations();    
        for(Annotation an:as){    
            System.out.println("类Test1的注释"+an);    
        }    
        Method med=c.getDeclaredMethod("doSth");    
        Annotation[] ass=med.getDeclaredAnnotations();    
        for(Annotation an:ass){    
            Class<!--/sp-->extends Annotation> clazz=an.annotationType();    
            Annotation[] ased=clazz.getAnnotations();    
            for(Annotation ad:ased){    
                System.out.println("注释的注释:"+ad);    
            }    
            System.out.println("方法doSth的注释:"+an);    
        }    
    }    
}   

此程序输出如下

类Test1的注释@testAnno.MyType(bugFixes=ok, authorName=hadeslee, lastModified=20061207)
注释的注释:@java.lang.annotation.Documented()
注释的注释:@java.lang.annotation.Retention(value=RUNTIME)
方法doSth的注释:@java.lang.Deprecated()
注释的注释:@java.lang.annotation.Retention(value=RUNTIME)
注释的注释:@java.lang.annotation.Target(value=[TYPE, METHOD])
方法doSth的注释:@testAnno.MyType(bugFixes=what, authorName=hadeslee, lastModified=20061207)

 

posted on 2012-05-13 08:41  牧者.D  阅读(137)  评论(0)    收藏  举报