JDK注解(内置和自定义)

JDK注解(内置和自定义)

1.内置

  1. @Override:可以确保重写的方法,的确存在与父类、接口中,可以有效避免单词拼错的情况
  2. @Deprecated:给用于提示,该方法由于安全,性能问题等,已经不推荐使用。此外在版本升级时,如果要计划删除一些方法,也通常会在前一个版本中加上@Deprecated,然后再在后续版本中删除
    3.@SuppressWarnings(value = “unchecked”)
    //压制警告(虽然可以使用SuppressWarning压制警告,但不建议使用。)//忽略对泛型等的检查操作。
    其中的一些属性和值:
  • value值:unchecked,deprecation(忽略一些过期的API),

  • unused(是否未被使用),

  • fallthrough(switch是否一致往下执行,而没有break),

  • path(忽略对类路径不存在的检查),

  • serialversionUID(忽略一个类可以序列化,但却没有序列化的警告),

  • all,全部忽略

2.自定义
引入元注解:即修饰注解的注解
常用的几个元注解:

(1)@Target:限制注解,使用位置(ElementType.枚举)
限制注解能够使用在属性、方法、类中。如果一个注解没有@Target描述,则可以修饰任何类型的元素;如果有@Target修饰,该注解只能用于被@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来进行约束
ElementType:


public enum ElementType {
    /** 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
}

ElementType就是用来约束使用的地方,和时间

@Rentention源码:


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

@Document
javadoc:java帮助文档。ABC.java->帮助文档
默认情况下,Javadoc不包含注解的解释,如果现在javadoc文档中也包含对注解的说明,则需要使用@Document标注

Document源码:

package java.lang.annotation;

/**
 * Indicates that annotations with a type are to be documented by javadoc
 * and similar tools by default.  This type should be used to annotate the
 * declarations of types whose annotations affect the use of annotated
 * elements by their clients.  If a type declaration is annotated with
 * Documented, its annotations become part of the public API
 * of the annotated elements.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited:继承

源码:

package java.lang.annotation;

/**
 * Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.3 @Inherited
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

使用实例:

package Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.METHOD,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value() default "张三";
    int age() default 24;
}

package Annotation;

import java.lang.annotation.Annotation;

public class Test {
    @MyAnnotation(value = "ls",age = 21)
    @Deprecated    //不推荐使用注解
    public static void test() throws Exception {
        Annotation[] declaredAnnotations =  Class.forName("Annotation.Test").getMethod("test").getDeclaredAnnotations();
        for(Annotation d:declaredAnnotations){
            if(d instanceof MyAnnotation){
                System.out.println(((MyAnnotation) d).age());
                System.out.println(((MyAnnotation) d).value());
            }else{
                System.out.println("Deprecated");   //不符合条件不推荐
            }
        }
    }

    public static void main(String[] args) throws Exception {
        test();
    }
}

结果:
在这里插入图片描述
修改代码:修改生命周期,将RetentionPolicy.RUNTIME修改为下图:
在这里插入图片描述
结果变为:
在这里插入图片描述
其他的几个元注解调试也类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值