转自:http://blog.csdn.net/x_yp/article/details/6229516
自定义注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER}) //用于字段,方法,参数
@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中
public @interface Field_Method_Parameter_Annotation {
Class<?> type() default void.class; //定义一个具有默认值的Class型成员
String describ(); //定义一个没有默认值的String成员
}
使用注解:
public class AnnotationTest {
// 注释字段
@Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class)
int id;
// 注释字段
@Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class)
String name;
public AnnotationTest() {
}
@Field_Method_Parameter_Annotation(describ = "获得编号", type = int.class)
public int getId() {
return id;
}
@Field_Method_Parameter_Annotation(describ = "设置编号")
public void setId(
// 注释参数
@Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id) {
this.id = id;
}
@Field_Method_Parameter_Annotation(describ = "获得姓名", type = String.class)
public String getName() {
return name;
}
@Field_Method_Parameter_Annotation(describ = "设置姓名")
public void setName(
@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
this.name = name;
}
@Field_Method_Parameter_Annotation(describ = "设置编号和姓名")
public void setIdAndName(
// 注释参数
@Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id,
@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
this.id = id;
this.name = name;
}
@Field_Method_Parameter_Annotation(describ = "设置编号和姓名")
public void setIdAndName2(
// 注释参数 @Field_Method_Parameter_Annotation(describ = "设置编号", type =
// int.class) int id,
String name) {
this.id = id;
this.name = name;
}
}
处理注解:
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println("********字段的Annotation*************");
Field[] declaredFields = AnnotationTest.class.getDeclaredFields(); //获得所有的字段
for(int i=0;i<declaredFields.length;i++){
Field field = declaredFields[i];
System.out.print("字段" + field.getName() + "的Annotation:"); //获得字段描述
//查看是否具有指定类型的注释:
if(field.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){
Field_Method_Parameter_Annotation fa = field.getAnnotation(Field_Method_Parameter_Annotation.class);
System.out.print(" " + fa.describ()); //获得字段描述
System.out.println(" " + fa.type()); //获得字段类型
}
}
System.out.println("********方法的Annotation********");
Method [] methods = AnnotationTest.class.getDeclaredMethods(); //获得所有的方法
for(int i=0;i<methods.length;i++){
Method method = methods[i];
//查看是否指定注释:
System.out.println("方法" + method.getName() + "的Annotation:");
if(method.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){
Field_Method_Parameter_Annotation ma = method.getAnnotation(Field_Method_Parameter_Annotation.class);
System.out.print(" " + ma.describ()); //获得方法描述
System.out.println(" " + ma.type()); //获得方法类型
}
System.out.println("方法" + method.getName() + "的参数的Annotation");
//获得所有参数注解
Annotation[][]parameterAnnotations = method.getParameterAnnotations();
if (parameterAnnotations.length == 0) {
System.out.println("方法没有参数");
}
for(int j = 0;j < parameterAnnotations.length;j++){
int length = parameterAnnotations[j].length;
if(length==0){
System.out.println("没有添加Annotation参数");
}else{
for(int k=0;k<length;k++){
//获得指定的注释:
Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation)parameterAnnotations[j][k];
System.out.print(" " + pa.describ()); //获得参数描述
System.out.println(" " + pa.type()); //获得参数类型
}
}
}
}
}
}