Annotation简介:
          Annotation提供一种机制,将程序中元素(如类、方法、属性等)和元数据联系起来。这样编译器可以将元数据保存的class文件中。代码分析工具就可以使用这些元数据执行的额外任务。注释采用“at”标记形式 ( @ ),后面是注释名称。
自定义 annotationDebug.java 
 package annotation.test;
package annotation.test;

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


 /**//*
    /**//*
 * annotation类型是一种接口,能够通过java反射API的方式提供对其信息的访问
     * annotation类型是一种接口,能够通过java反射API的方式提供对其信息的访问
 */
     */


 /**//*
    /**//*
 * Documented 产生Java Doc文件,这次可以看到文件中包括了@Debug的信息
     * Documented 产生Java Doc文件,这次可以看到文件中包括了@Debug的信息
 */
     */
 @Documented
@Documented

 /**//*
    /**//*
 * 限定 annotation 使用对象 - Target
     * 限定 annotation 使用对象 - Target 
 * 在定义annotation时,使用java.lang.annotation.Target可以定义annotation使用的时机。
     * 在定义annotation时,使用java.lang.annotation.Target可以定义annotation使用的时机。
 * public enum ElementType {
     * public enum ElementType {
 *   TYPE, // 适用 class, interface, enum
     *   TYPE, // 适用 class, interface, enum
 *   FIELD, // 适用 field
     *   FIELD, // 适用 field
 *   METHOD, // 适用 method
     *   METHOD, // 适用 method
 *   PARAMETER, // 适用 method 上之 parameter
     *   PARAMETER, // 适用 method 上之 parameter
 *   CONSTRUCTOR, // 适用 constructor
     *   CONSTRUCTOR, // 适用 constructor
 *   LOCAL_VARIABLE, // 适用区域变量
     *   LOCAL_VARIABLE, // 适用区域变量
 *   ANNOTATION_TYPE, // 适用 annotation 型态
     *   ANNOTATION_TYPE, // 适用 annotation 型态
 *   PACKAGE // 适用 package
     *   PACKAGE // 适用 package
 *   }
     *   } 
 */
     */

 @Target(
@Target( {ElementType.CONSTRUCTOR, ElementType.METHOD})//指定目标
{ElementType.CONSTRUCTOR, ElementType.METHOD})//指定目标

 /**//*
    /**//*
 * Retention 在你自定义的形态中,指定编译器如何处理自定义的annotation
     * Retention 在你自定义的形态中,指定编译器如何处理自定义的annotation
 * 告知编译器如何处理 annotaion - Retention
     * 告知编译器如何处理 annotaion - Retention 
 * RetentionPolicy
     * RetentionPolicy
 *         SOURCE        编译器处理完Annotation信息后就没事了
     *         SOURCE        编译器处理完Annotation信息后就没事了
 *         CLASS        编译器将Annotation储存于class档中,预设
     *         CLASS        编译器将Annotation储存于class档中,预设
 *         RUNTIME        编译器将Annotation储存于class檔中,可由VM读入
     *         RUNTIME        编译器将Annotation储存于class檔中,可由VM读入
 *                     使用java设计的一个程序代码工具,从VM中读出Annotation信息,已在分析程序中使用。
     *                     使用java设计的一个程序代码工具,从VM中读出Annotation信息,已在分析程序中使用。
 *                     搭配Reflection机制,就可以实现。
     *                     搭配Reflection机制,就可以实现。
 *
     *         
 *         J2SE 5.0中新增了java.lang.reflect.AnnotatedElement这个接口,当中定义有四个方法:
     *         J2SE 5.0中新增了java.lang.reflect.AnnotatedElement这个接口,当中定义有四个方法:
 *                 public Annotation getAnnotation(Class annotationType);
     *                 public Annotation getAnnotation(Class annotationType);
 *                 public Annotation[] getAnnotations();
     *                 public Annotation[] getAnnotations();
 *                 public Annotation[] getDeclaredAnnotations();
     *                 public Annotation[] getDeclaredAnnotations();
 *                 public boolean isAnnotationPresent(Class annotationType);
     *                 public boolean isAnnotationPresent(Class annotationType);
 *         Class、Constructor、Field、Method、Package等类别,都实作了 AnnotatedElement这个接口,
     *         Class、Constructor、Field、Method、Package等类别,都实作了 AnnotatedElement这个接口,
 *         所以您可以从这些类别的实例上,分别取得标示于其上的Annotation与其信息,
     *         所以您可以从这些类别的实例上,分别取得标示于其上的Annotation与其信息,
 *         如果 RetentionPolicy为RUNTIME的话。
     *         如果 RetentionPolicy为RUNTIME的话。

 */
     */
 
     
 @Retention(RetentionPolicy.RUNTIME)//设置保持性
@Retention(RetentionPolicy.RUNTIME)//设置保持性

 /**//*
    /**//*
 * @Inherited 子类是否继承父类的 annotation
     * @Inherited 子类是否继承父类的 annotation 
 */
     */
 @Inherited
@Inherited

 public @interface Debug
public @interface Debug  {
{
 //定义了value()方法,编译器在编译时会自动帮您产生一个value的变量成员,接着在使用Debug Annotation时要指定值
    //定义了value()方法,编译器在编译时会自动帮您产生一个value的变量成员,接着在使用Debug Annotation时要指定值
 //注释类型的数据成员被设置成使用有限信息进行工作
    //注释类型的数据成员被设置成使用有限信息进行工作
 //定义数据成员不需要分别定义访问和修改的方法,只需定义一个方法,以数据成员的名称命名它,数据类型应该是该方法返回值的类型.
    //定义数据成员不需要分别定义访问和修改的方法,只需定义一个方法,以数据成员的名称命名它,数据类型应该是该方法返回值的类型.
 String value()default "windfree";//默认值的类型必需与成员变量的申明类型一置
    String value()default "windfree";//默认值的类型必需与成员变量的申明类型一置
 String name();
    String name();
 }
}


在程序中使用自定义的annotation
   TestDebug.java
 package annotation.test;
package annotation.test;


 public class TestDebug
public class TestDebug  {
{
 @Debug(
    @Debug(
 name = "zgliu"
               name = "zgliu"
 )
            )

 public void doSomething()
     public void doSomething()  {
{        
 }
        }
 }
}

使用java中反射API读取class中的元数据的信息
      DebugTool.java:
 package annotation.test;
package annotation.test;

 import java.lang.annotation.Annotation;
import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
import java.lang.reflect.Method;


 public class DebugTool
public class DebugTool  {
{

 public static void main(String []args)throws NoSuchMethodException
    public static void main(String []args)throws NoSuchMethodException  {
{
 Class<TestDebug> c=TestDebug.class;
        Class<TestDebug> c=TestDebug.class;
 Method method = c.getMethod("doSomething");
        Method method = c.getMethod("doSomething");

 if(method.isAnnotationPresent(Debug.class))
        if(method.isAnnotationPresent(Debug.class))  {
{
 System.out.println("@Debug is found.");
            System.out.println("@Debug is found.");
 Debug debug = method.getAnnotation(Debug.class);
            Debug debug = method.getAnnotation(Debug.class);
 System.out.println("\tvalue = " + debug.value());
            System.out.println("\tvalue = " + debug.value());
 System.out.println("\tname = " + debug.name());
            System.out.println("\tname = " + debug.name());
 }
        }

 else
        else  {
{
 System.out.println("@Debug is not found.");
            System.out.println("@Debug is not found.");
 }
        }

 Annotation[] annotations = method.getAnnotations();
        Annotation[] annotations = method.getAnnotations();

 for(Annotation annotation : annotations)
        for(Annotation annotation : annotations)  {
{
 System.out.println(
            System.out.println(
 annotation.annotationType().getName());
                    annotation.annotationType().getName());
 }
        }
 }
    }
 }
}
Java标准Annotation
@Deprecated 相当于Javadoc的@deprecated,被@Deprecated标注的对象class, method等被注明为不推荐使用。主要用于javac等编译工具。
@Override 注明对象method重载了父类的方法。javac等编译工具编译时会根据此Annotation判断重载方法是否正确。
@SuppressWarnings 告诉javac等编译器忽略所指定的特定的警告信息。
@Target 被定义的annotation可以附加在那些对象上。
@Retention annotation的作用期间。 
定义一个annotation
 import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
 
 
 @Retention(RetentionPolicy.RUNTIME)
@Retention(RetentionPolicy.RUNTIME)

 public @interface MyAnnotation
public @interface MyAnnotation  {①定义一个注释
{①定义一个注释
 int init();
int init();
 }
}

 import java.lang.annotation.Annotation;
import java.lang.annotation.Annotation;

 public class TestAnnotation
public class TestAnnotation  {
{
 @MyAnnotation(init = 2)②使用注释
  @MyAnnotation(init = 2)②使用注释

 public void print()
  public void print() {
{
 System.out.println(TestAnnotation.class.getName());
     System.out.println(TestAnnotation.class.getName());
 }
  }

 public static void main(String[] args) throws Exception
  public static void main(String[] args) throws Exception {
{
 TestAnnotation ta = new TestAnnotation();
     TestAnnotation ta = new TestAnnotation();
 Annotation[] annotations = ta.getClass().getMethod("print").getAnnotations();③
     Annotation[] annotations = ta.getClass().getMethod("print").getAnnotations();③

 for (Annotation annotation : annotations)
     for (Annotation annotation : annotations)   {
{              
 System.out.println("MyAnnotation.init : " +
         System.out.println("MyAnnotation.init : " + 
 ((MyAnnotation)annotation).init());④打印出init的值
       ((MyAnnotation)annotation).init());④打印出init的值
 }
        } 
 }
  }
 }
}

我们定义了一个名为MyAnnotation的注释而这个注释中只有一个类型为int名为init的属性,我们在②处使用了我们刚刚定义的注释并且为init赋值为2,在③处我们通过反射机制获得print方法上定义的所有注释然后通过迭代将其值init打印至控制台。
最终在控制台输出以下信息:
MyAnnotation.init : 2