Annotation是继承自Java .lang.annotation.Annotation 的类,用于向程序分析工具或虚拟机提供package class field methed 等方面的信息,它和其他类没什么区别,除了使用方式. Annotation的一般形式是 :
public @interface MyAnnotation { String value() default "hahaha" ; }
public @interface MyAnnotation { String value() default "hahaha" ; }
我认为和它等价的java类为:
public class MyAnnotation extends java.lang.annotation.Annotation{ private String value = "hahaha" ; public void setValue(String value){ this .value = value; } public String getValue(){ return value; } }
public class MyAnnotation extends java.lang.annotation.Annotation{ private String value = "hahaha" ; public void setValue(String value){ this .value = value; } public String getValue(){ return value; } }
Annotation的使用方法和Bean的比较:
@MyAnnotation (value= "hello" ) Method method = AnnotationTest. class .getMethod( "doSomthing" , null ); MyRetention mr = method.getAnnotation(MyRetention. class ); String value = mr.value();
@MyAnnotation (value= "hello" ) Method method = AnnotationTest.class .getMethod( "doSomthing" , null ); MyRetention mr = method.getAnnotation(MyRetention.class ); String value = mr.value();
@interface实际上是继承了java.lang.annotation.Annotation,所以定义annotation时不能继承其他annotation或interface. java.lang.annotation.Retention告诉编译器如何对待 Annotation,使用Retention时,需要提供java.lang.annotation.RetentionPolicy的枚举值.
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME }
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME }
. java.lang.annotation.Target告诉编译器Annotation使用在哪些地方,使用需要指定java.lang.annotation.ElementType的枚举值.
public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE }
public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE }
java.lang.annotation.Documented用于指定该Annotation是否可以写入javadoc中. java.lang.annotation.Inherited用于指定该Annotation用于父类时是否能够被子类继承. 如
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Inherited @Target ({ElementType.CONSTRUCTOR,ElementType.METHOD}) @Retention (RetentionPolicy.CLASS) public @interface MyAnnotation { String value() default "hahaha" ; }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Inherited @Target ({ElementType.CONSTRUCTOR,ElementType.METHOD}) @Retention (RetentionPolicy.CLASS) public @interface MyAnnotation { String value() default "hahaha" ; }
java.lang.reflect.AnnotatedElement接口提供了四个方法来访问Annotation
public Annotation getAnnotation(Class annotationType); public Annotation[] getAnnotations(); public Annotation[] getDeclaredAnnotations(); public boolean isAnnotationPresent(Class annotationType);
public Annotation getAnnotation(Class annotationType); public Annotation[] getAnnotations(); public Annotation[] getDeclaredAnnotations(); 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