JAVA元数据注释初探

  JAVA的元数据功能是JDK1.5才开始支持的,以前都没有,正因为是新支持的,所以有关于它的介绍挺少,用处也不太多,最近发现很多框架都可以把它用来配置一些东西,以代替以前比较复杂的XML配置.想像一下,在JAVA代码中直接写入注释来配置,那该是多么好的事情,让我们写习惯了代码和看习惯了代码的人来说,这无疑是一件很爽的事情.
  我们可以使用JAVA内置的注释内型,如果觉得不够用,可以定义自己的注释内型,定义如下
  /*
  *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)
  从这代码里,我们可以看出,取注释其实很简单,就是利用反射机制来取的.不过我们要特别注意到的一点是,我们不但可以取我们定义的类的注释,也可以取注释的注释,我们这里只取到了MyType的注释,其实还可以往下取,在取的过程中,我们可以看到这些元数据注释类都用了哪些注释。