一.相关简介
Annotation主要用于类、构造方法、方法、参数等的声明之中,其功能并不会影响程序的运行,但是会对警告等辅助工具产生影响。且只有JDK 1.5以后的版本呢才具有该项功能。
二.定义Annotation类型
定义Annotation:有3种方法:
1.类中不包括任何参数
<span style="font-size:18px;">public @interface NoMemberAnnotation{</span>
<span style="font-size:18px;">}</span>
2.类中包含一个成员的Annotation类型
<span style="font-size:18px;">public @interface NoMemberAnnotation{</span>
<span style="font-size:18px;">String value();</span>
<span style="font-size:18px;">}</span>
3.类中包含两个成员的Annotation类型
<span style="font-size:18px;">public @interface NoMemberAnnotation{</span>
<span style="font-size:18px;">String value();</span>
<span style="font-size:18px;">Class type();</span>
<span style="font-size:18px;">}</span>
一般情况下定义Annotation类则会使用这三种方法,并且还可对类中的成员设置默认值:
<span style="font-size:18px;">public @interface NoMemberAnnotation{</span>
<span style="font-size:18px;">String describe() default"默认值";</span>
<span style="font-size:18px;">Class type() default void.class</span>
<span style="font-size:18px;">}</span>
在定义Annotation是,可使用@Target来设置其元素的种类,如果没有设置则默认该类适用于所有程序元素。通常使用ElementType中的枚举常量(关于枚举常量请自行了解),一般有如下几种:
ANNOTATION_TYPE :表示用于Annotation类型。
TYPE :表示用于类、接口和枚举,以及Annotation类型。
CONSTRUCTOR:表示用于构造方法。
FIELD:表示用于成员变量和枚举常量。
METHOD:表示用于方法。
PARAMETER:用于具体的类方法。
LOCAL_VARIABLE:用于局部变量。
再则,使用@Retention可以设置Annotation类的有效范围,并且也是使用枚举常量,它们属于枚举类RetentionPolicy,如下:
SOURCE:表示该注释只能存在在源代码中,不能编译到类文件中(即.class文件),其有效范围最小。
CLASS:表示该注释可以编译到类文件中,但程序运行是不能加载到JVM中。
RUNTIME:该注释既可以编译到类文件中也能在运行时将Annotation类加载到JVM中。
Ok了,Annotation的定义方面差不多就这些,下面我们看看Annotation的具体用法。
三.Annotation类的具体用法
首先定义一个用来注释构造方法的Annotation类@Constructor_Annotation,有效范围为加载Annotation到JVM中,代码如下:
<p><span style="font-size:18px;">import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;</span></p><p><span style="font-size:18px;">@Target({ElementType.CONSTRUCTOR})//用于构造方法
@Retention(RetentionPolicy.RUNTIME)//表示在程序运行时将Annotation类加载到JVM中
public @interface Constructor_Annotation {
String value() default"默认构造方法";//定义一个具有默认值的String成员
}
</span></p>
然后定义一个注释成员变量、方法和参数的Annotation类 F_M_P_Annotation,有效范围为程序运行时加载Annotation到JVM中。代码如下:
<p>
<span style="font-size:18px;">import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;</span></p><p><span style="font-size:18px;"> </span></p><p><span style="font-size:18px;">@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)//表示在程序运行时将Annotation类加载到JVM中
public @interface F_M_P_Annotation{
String decribe();//定义一个没默认值的String型成员
Class type() default void.class;//定义一个具有默认值的Class型成员
}
</span></p>
最后编写 一个类TestAnnotation,运用前面定义的两个Annotation类,代码如下:
<p><span style="font-size:18px;">import java.beans.ConstructorProperties;</span></p><p><span style="font-size:18px;">public class TestAnnotation {
@F_M_P_Annotation(decribe = "编号", type = int.class)
int id;// 要注释的字段
@F_M_P_Annotation(decribe = "姓名", type = String.class)
String name;</span></p><p><span style="font-size:18px;"> @Constructor_Annotation()
// 采用默认的方法注释本类的构造方法
public TestAnnotation(//注释构造方法中的参数从而注释该构造方法
@F_M_P_Annotation(decribe = "编号", type = int.class) int id,
@F_M_P_Annotation(decribe = "姓名", type = String.class) String name) {
this.id = id;
this.name = name;
}
@F_M_P_Annotation(decribe="获得编号",type=int.class)//注释类中的方法
public int getId(){
return id;
}
@F_M_P_Annotation(decribe="获得编号")//此时成员Type采用默认值注释方法
public void setId(
@F_M_P_Annotation(decribe = "编号", type = int.class) int id){
this.id=id;
}
@F_M_P_Annotation(decribe = "姓名", type = String.class)
public String getName(){
return name;
}
}
</span></p>
这样便对类TestAnnotation类中的成员变量、构造方法、方法进行了注释。
四.相关拓展
当在定义Annotation是将@Retention设置为RetentionPolicy.RUNTIME时,那么在程序运行时就可通过反射获取相关的Annotation的信息,如获得成员变量、构造方法等的注释信息。(注:Java中的反射机制可访问已经加载到JVM中的JAVA对象的描述,关于反射的知识我便不赘述了。)
在AccessibleObject中定义了3个关于Annotation的方法:
其中isAnnotationPresent(Class<? extends Annotation>annotationClass)方法用来查看是否添加了指定类型的Annotation,若是则返回True,否则返回False。
方法getAnotation(Class<T>annotationClass)用来获得指定类型的Annotation。如果存在则返回该对象,否则返回Null。
方法getAnnotation()则用来返回所有的Annotation,该方法返回一个Annotation对象。
</pre><p> </p><pre class="java" name="code">