java自定义注解的使用

如:

@Target(ElementType.FIELD)  //@Target(value = {ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckNum {
		//错误消息
		String message() default "非数字格式";
	}

1、target :译文 目标、对象

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

ElementType 取值:

public enum ElementType {
    /** 用于描述类、接口(包括注解类型) 或enum声明  Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** 用于描述域 字段声明(包括枚举常量)Field declaration (includes enum constants) */
    FIELD,

    /** 描述方法 Method declaration */
    METHOD,

    /**形式参数声明  Formal parameter declaration */
    PARAMETER,

    /** 构造器声明 Constructor declaration */
    CONSTRUCTOR,

    /** 局部变量 Local variable declaration */
    LOCAL_VARIABLE,

    /** 批注类型 Annotation type declaration */
    ANNOTATION_TYPE,

    /** 描述包  Package declaration */
    PACKAGE,

    /**
     * 类型参数 Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *标注任何类型
     * @since 1.8
     */
    TYPE_USE
}

2、Retention:译文 保留

@Retention注解,翻译为持久力、保持力。即用来修饰自定义注解的生命力,生命周期。
注解的生命周期有三个阶段:1、Java源文件阶段;2、编译到class文件阶段;3、运行期阶段。同样使用了RetentionPolicy枚举类型定义了三个阶段:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}


public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * (注解将被编译器忽略掉)
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * (注解将被编译器记录在class文件中,但在运行时不会被虚拟机保留,这是一个默认的行为)
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * (注解将被编译器记录在class文件中,而且在运行时会被虚拟机保留,因此它们能通过反射被读取到)
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

1、 如果一个注解被定义为RetentionPolicy.SOURCE,则它将被限定在Java源文件中,那么这个注解即不会参与编译也不会在运行期起任何作用,这个注解就和一个注释是一样的效果,只能被阅读Java文件的人看到;
2、如果一个注解被定义为RetentionPolicy.CLASS,则它将被编译到Class文件中,那么编译器可以在编译时根据注解做一些处理动作,但是运行时JVM(Java虚拟机)会忽略它,我们在运行期也不能读取到;
3、如果一个注解被定义为RetentionPolicy.RUNTIME,那么这个注解可以在运行期的加载阶段被加载到Class对象中。那么在程序运行阶段,我们可以通过反射得到这个注解,并通过判断是否有这个注解或这个注解中属性的值,从而执行不同的程序代码段。我们实际开发中的自定义注解几乎都是使用的RetentionPolicy.RUNTIME;
4、在默认的情况下,自定义注解是使用的RetentionPolicy.CLASS。

3、@Documented注解
是被用来指定自定义注解是否能随着被定义的java文件生成到JavaDoc文档当中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值