Annotation的使用方式及处定义注解
首先扫下盲,元注解就是
Annotation的元注解一共有4个,分别为:
@Target
@Retention
@Documented
@Inherited
1、@Target用来表示声明的Annotation修饰的范围,其ElementType的枚举一共有以下几个
简单地翻译一下吧
ANNOTATION_TYPE 注解类型声明
CONSTRUCTOR 构造方法声明
FIELD 属性声明
LOCAL_VARIABLE 局部变量性声明
METHOD 方法声明
PACKAGE 包声明
PARAMETER 参数声明
TYPE 类、接口(包括注解类型)或枚举型声明
2、@Retention 表明了注解可以保留多长时间,或者说是在哪个阶段,默认的保存留方案是CLASS,其RetentionPolicy的枚举一共有以下几个
简单地翻译一下吧
CLASS 注解被记录在class文件中,但是在运行的时候不会在虚拟机中保留(即编译阶段有效)
RUNTIME 注解被记录在class文件中,在虚拟机运行时会被保留,因此可以被反射机制读取出来(即在运行阶段仍然有效)
SOURCE 在编译的时候被编译器丢弃(即源码阶段有效)
3、@Documented 标注了这个描述的annotation是否可以被文档化。(没什么好说的)
4、@Inherited 表明了注解的类型是自动被继承的。如果一个注解使用了@Inherited这个元注解,则它的子类也会自动添加这个注解(即这个注解是可以被继承的)。
5、jdk1.5后反射的实体都实现了AnnotatedElement的接口,使得读取注解更加地简单,为了节省时间,还是截jdk文档的图吧
6、最后弄一个自己写的使用Annotation并且对其进行处理的例子
StudentGender.java
package org.javatribe.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentGender {
public enum Gender{ MALE, FEMALE};
Gender studentGender() default Gender.MALE;
}
StudentName.java
package org.javatribe.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentName {
String value() default "ICE";
}
Teacher.java
package org.javatribe.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Teacher {
public int id() default 1;
public String name() default "ICE'S TEACHER";
public String introduce() default "I am a teacher!!!";
}
Student.java
package org.javatribe.annotation;
public class Student {
@StudentName
private String name;
@StudentGender
private String gender;
@Teacher
private String teacher;
}
StudentManage.java
package org.javatribe.annotation;
import java.lang.reflect.Field;
/**
* @Description:
*
* @author ICE
*
* @date 2014-6-6 上午3:20:50
*/
public class StudentManage {
public static void getStudent(Class<?> clazz){
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
if(field.isAnnotationPresent(StudentName.class)){
System.out.println("学生姓名为: " + field.getAnnotation(StudentName.class).value());
} else if (field.isAnnotationPresent(StudentGender.class)) {
System.out.println("学生的性别为: " + field.getAnnotation(StudentGender.class).studentGender());
} else if (field.isAnnotationPresent(Teacher.class)) {
Teacher teacher = field.getAnnotation(Teacher.class);
System.out.println("其老师为: " + teacher.name() + "(" + teacher.id() + ")");
System.out.println("老师介绍: " + teacher.introduce());
}
}
}
public static void main(String[] args) {
StudentManage.getStudent(Student.class);
}
}
写完收工,好困,后背有点麻了,碎觉。