JAVA_基础注解的概念

什么是注解

Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加 载, 运行时被读取, 并执行相应的处理。通过使用 Annotation, 程序员 可以在不改变原有逻辑的情况下, 在源文件中嵌入一些补充信息。代 码分析工具、开发工具和部署工具可以通过这些补充信息进行验证 或者进行部署。
Annotation 可以像修饰符一样被使用, 可用于修饰包,类, 构造器, 方 法, 成员变量, 参数, 局部变量的声明, 这些信息被保存在 Annotation 的 name=value 对中。

1)使用 Annotation 时要在其前面增加 @ 符号, 并把该 Annotation 当成 一个修饰符使用。用于修饰它支持的程序元素
示例一:生成文档相关的注解
@author 标明开发该类模块的作者,多个作者之间使用,分割 。
@version 标明该类模块的版本 。
@see 参考转向,也就是相关主题 。
@since 从哪个版本开始增加的 。
@param 对方法中某参数的说明,如果没有参数就不能写 。
@return 对方法返回值的说明,如果方法的返回值类型是void就不能写 。
@exception 对方法可能抛出的异常进行说明 ,如果方法没有用throws显式抛出的异常就不能写 其中。
@param @return@exception 这三个标记都是只用于方法的。
@param的格式要求:@param 形参名 形参类型 形参说明 。
@return 的格式要求:@return 返回值类型 返回值说明 。
@exception的格式要求:@exception 异常类型 异常说明 。
@param@exception可以并列多个。

示例二:在编译时进行格式检查(JDK内置的三个基本注解)

@Override: 限定重写父类方法, 该注解只能用于方法 。
@Deprecated: 用于表示所修饰的元素(类, 方法等)已过时。通常是因为 所修饰的结构危险或存在更好的选择。
@SuppressWarnings: 抑制编译器警告。

自定义注解

① 注解声明为:@interface
② 内部定义成员,通常使用value表示。
③ 可以指定成员的默认值,使用defaulat定义。
④ 如果自定义注解没有成员,表明是一个标识作用。

如果注解有成员,在使用注解时,需要指明成员的值。
定义注解必须配上注解的信息处理流程(使用反射)才有意义。
自定义注解通过都会指明两个元注解:Retention Target

public @interface MyAnnotation {
    String value() default "hello";
}

@MyAnnotation(value = "holle")
class Person{
    @MyAnnotation()
    public void show(){
        ....
    }
    ....
}

JDK 提供的元注解

元注解:对现有的注解进行解释说明的注解

  • JDK 的元 Annotation 用于修饰其他 Annotation 定义。
  • JDK5.0提供了4个标准的meta-annotation类型,分别是:
    Retention :指定所修饰的Annotation的生命周期:SOURCE\CLASS(默认行为)\RUNTIME只能声明为RUNTIME生命周期的注解,才能通过反射获取。
    Target :用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于 修饰哪些程序元素。 @Target 也包含一个名为 value 的成员变量。
    Documented :表示所别修饰的注解在被Javadoc解析时,保留下来。
    Inherited:被它修饰的 Annotation 将具有继承性。如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注解。

通过反射获取注解信息

JDK8 中注解的新特性:可重复注解、类型注解

  • 可重复注解

    ① 在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class
    MyAnnotationTargetRetention等元注解与MyAnnotations相同。

//JDK8之前的写法
@MyAnnotations({@MyAnnotation(value = "hi"),@MyAnnotation(value = "hello")})
class Person{
    ....
}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE,FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {
    String value() default "hello";
}
public @interface MyAnnotations {
    MyAnnotation[] value();
}
//JDK8的写法
@MyAnnotation(value = "hi")
@MyAnnotation(value = "hello")
class Person{
    ....
}
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE,FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {
    String value() default "hello";
}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE,FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotations {
    MyAnnotation[] value();
}

类型注解

JDK1.8之后,关于元注解@Target的参数类型ElementType枚举值多了两个TYPE_PARAMETER,TYPE_USE

ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语 句中(如:泛型声明)。
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。

@Target(TYPE_PARAMETER)
public @interface MyAnnotation {
    String value() default "hello";
}
class Generic<@MyAnnotation(value = "我是TYPE_PARAMETER`注解") T>{
}
@Target({TYPE_USE})
public @interface MyAnnotation {
    String value() default "hello";
}
class Generic<T>{
    public void show() throws @MyAnnotation RuntimeException {
        ArrayList<@MyAnnotation(value = "我是TYPE_USE注解") String> list = new ArrayList<>();
        int num = (@MyAnnotation int) 10L;
    }
}
posted @ 2021-01-04 09:45  TuoYingtao  阅读(70)  评论(0编辑  收藏  举报