Annotation介绍@interface

来源:互联网 发布:素描绘画软件 编辑:程序博客网 时间:2024/05/04 06:20

 Annotation是继承自Java.lang.annotation.Annotation的类,用于向程序分析工具或虚拟机提供package class field methed 等方面的信息,它和其他类没什么区别,除了使用方式. 
     Annotation的一般形式是 :

Java代码 复制代码
  1. public @interface MyAnnotation {   
  2.     String value() default "hahaha";   
  3. }   
[java] view plain copy
  1. public @interface MyAnnotation {  
  2.     String value() default "hahaha";  
  3. }   

    
     我认为和它等价的java类为:

Java代码 复制代码
  1. public class MyAnnotation extends java.lang.annotation.Annotation{   
  2.      private String value = "hahaha";   
  3.      public void setValue(String value){   
  4.           this.value = value;   
  5.      }   
  6.      public String getValue(){   
  7.           return value;   
  8.       }   
  9. }  
[java] view plain copy
  1. public class MyAnnotation extends java.lang.annotation.Annotation{  
  2.      private String value = "hahaha";  
  3.      public void setValue(String value){  
  4.           this.value = value;  
  5.      }  
  6.      public String getValue(){  
  7.           return value;  
  8.       }  
  9. }  


     Annotation的使用方法和Bean的比较:

Java代码 复制代码
  1. @MyAnnotation(value="hello")     //对应Bean的set方法   
  2.   
  3. Method method = AnnotationTest.class.getMethod("doSomthing"null);   //取得被注释的方法,AnnotationTest.class为该方法所在的类   
  4. MyRetention mr = method.getAnnotation(MyRetention.class); //取得注释对象   
  5. String value = mr.value();    //取得value的值,对应Bean的get方法.  
[java] view plain copy
  1. @MyAnnotation(value="hello")     //对应Bean的set方法  
  2.   
  3. Method method = AnnotationTest.class.getMethod("doSomthing"null);   //取得被注释的方法,AnnotationTest.class为该方法所在的类  
  4. MyRetention mr = method.getAnnotation(MyRetention.class); //取得注释对象  
  5. String value = mr.value();    //取得value的值,对应Bean的get方法.  

     @interface实际上是继承了java.lang.annotation.Annotation,所以定义annotation时不能继承其他annotation或interface. 
    java.lang.annotation.Retention告诉编译器如何对待 Annotation,使用Retention时,需要提供java.lang.annotation.RetentionPolicy的枚举值.

Java代码 复制代码
  1. public enum RetentionPolicy {   
  2.     SOURCE, // 编译器处理完Annotation后不存储在class中   
  3.     CLASS, // 编译器把Annotation存储在class中,这是默认值   
  4.     RUNTIME // 编译器把Annotation存储在class中,可以由虚拟机读取,反射需要   
  5. }   
[java] view plain copy
  1. public enum RetentionPolicy {  
  2.     SOURCE, // 编译器处理完Annotation后不存储在class中  
  3.     CLASS, // 编译器把Annotation存储在class中,这是默认值  
  4.     RUNTIME // 编译器把Annotation存储在class中,可以由虚拟机读取,反射需要  
  5. }   


    java.lang.annotation.Target告诉编译器Annotation使用在哪些地方,使用需要指定java.lang.annotation.ElementType的枚举值.

Java代码 复制代码
  1. public enum ElementType {   
  2.     TYPE, // 指定适用点为 class, interface, enum   
  3.     FIELD, // 指定适用点为 field   
  4.     METHOD, // 指定适用点为 method   
  5.     PARAMETER, // 指定适用点为 method 的 parameter   
  6.     CONSTRUCTOR, // 指定适用点为 constructor   
  7.     LOCAL_VARIABLE, // 指定使用点为 局部变量   
  8.     ANNOTATION_TYPE, //指定适用点为 annotation 类型   
  9.     PACKAGE // 指定适用点为 package   
  10. }   
[java] view plain copy
  1. public enum ElementType {  
  2.     TYPE, // 指定适用点为 class, interface, enum  
  3.     FIELD, // 指定适用点为 field  
  4.     METHOD, // 指定适用点为 method  
  5.     PARAMETER, // 指定适用点为 method 的 parameter  
  6.     CONSTRUCTOR, // 指定适用点为 constructor  
  7.     LOCAL_VARIABLE, // 指定使用点为 局部变量  
  8.     ANNOTATION_TYPE, //指定适用点为 annotation 类型  
  9.     PACKAGE // 指定适用点为 package  
  10. }   


    java.lang.annotation.Documented用于指定该Annotation是否可以写入javadoc中. 
    java.lang.annotation.Inherited用于指定该Annotation用于父类时是否能够被子类继承. 

Java代码 复制代码
  1. import java.lang.annotation.ElementType;   
  2. import java.lang.annotation.Retention;   
  3. import java.lang.annotation.RetentionPolicy;   
  4. import java.lang.annotation.Target;   
  5.   
  6. @Documented  //这个Annotation可以被写入javadoc   
  7. @Inherited       //这个Annotation 可以被继承   
  8. @Target({ElementType.CONSTRUCTOR,ElementType.METHOD}) //表示这个Annotation只能用于注释 构造子和方法   
  9. @Retention(RetentionPolicy.CLASS) //表示这个Annotation存入class但vm不读取   
  10. public @interface MyAnnotation {   
  11.     String value() default "hahaha";   
  12. }  
[java] view plain copy
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. @Documented  //这个Annotation可以被写入javadoc  
  7. @Inherited       //这个Annotation 可以被继承  
  8. @Target({ElementType.CONSTRUCTOR,ElementType.METHOD}) //表示这个Annotation只能用于注释 构造子和方法  
  9. @Retention(RetentionPolicy.CLASS) //表示这个Annotation存入class但vm不读取  
  10. public @interface MyAnnotation {  
  11.     String value() default "hahaha";  
  12. }  


    java.lang.reflect.AnnotatedElement接口提供了四个方法来访问Annotation

Java代码 复制代码
  1. public Annotation getAnnotation(Class annotationType);   
  2. public Annotation[] getAnnotations();   
  3. public Annotation[] getDeclaredAnnotations();   
  4. public boolean isAnnotationPresent(Class annotationType);  
[java] view plain copy
  1. public Annotation getAnnotation(Class annotationType);  
  2. public Annotation[] getAnnotations();  
  3. public Annotation[] getDeclaredAnnotations();  
  4. public boolean isAnnotationPresent(Class annotationType);  


Class、Constructor、Field、Method、Package等都实现了该接口,可以通过这些方法访问Annotation信息,前提是要访问的Annotation指定Retention为RUNTIME. 

    Java内置的annotation有Override Deprecated SuppressWarnings. 
    Override只用于方法,它指明注释的方法重写父类的方法,如果不是,则编译器报错. 
    Deprecated指明该方法不建议使用 
    SuppressWarnings告诉编译器:我知道我的代码没问题,你不用吓我了,我不怕的^_^ 
    这些都是Mark Annotation,名称本身就包含了要提供的信息,不需要额外提供. 
转自:http://www.javaeye.com/topic/171412

0 0
原创粉丝点击