Retention提供了java.lang.annotation.RetentionPolicy的列举
为实现获取Class、Constructor、Field、Method等类别annotation,需要将@Retention设置为RetentionPolicy.RUNTIME,并通过反射技术获取到相关的annotation
如
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, // 处理完Annotation信息不存档,如SuppressWarnings
CLASS, // 将Annotation储存于class档中
RUNTIME // 将Annotation储存于class档中,可由JVM获取
}
为实现获取Class、Constructor、Field、Method等类别annotation,需要将@Retention设置为RetentionPolicy.RUNTIME,并通过反射技术获取到相关的annotation
如
package test;
import java.lang.annotation.*;
@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成员
String name() default ""; //字段属性名称
}