java注解

559 阅读4分钟

「这是我参与11月更文挑战的第13天,活动详情查看:2021最后一次更文挑战」。

简单入门

现在大家开发过程中,经常会用到注解。 比如@Controller 等等,但是有时候也会碰到自定义注解,在开发中公司的记录日志就用到了自定义注解。身为渣渣猿还是有必要学习下自定义注解的。

这篇我们先写一个简单的注解列子,不会立马介绍各种什么元注解。从例子中感受下注解的作用

定义个注解

package com.kevin.annotation;

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

@Retention(RetentionPolicy.RUNTIME)
public @interface Kevin {
    String name() default "kevin";
}

解析并测试这个注解

package com.kevin;

import com.kevin.annotation.Kevin;

@Kevin
public class Test {

  public static void showKevin(Class c) {
      System.out.println(c.getName());
      boolean isExist = c.isAnnotationPresent(Kevin.class);

      if (isExist) {
          Kevin kevin = (Kevin) c.getAnnotation(Kevin.class);
          System.out.println(kevin.name());
      }
  }

  public static void main(String[] args) {
      Test.showKevin(Test.class);
  }
}

运行结果


com.kevin.Test
kevin

Process finished with exit code 0

总结

上面几句代码,我们已经实现了一个简单的自定义注解,是不是很简单。 大家不要吧注解想想的太复杂,其实任何东西大规模的应用肯定是易用易懂的。

简单介绍

整体图示

image.png

内置注解

@Override 重写覆盖

这个注解大家应该经常用到,主要在子类重写父类的方法,比如toString()方法

package com.kevin.demo;

public class Demo1 {

    @Override
    public String toString(){
        return "demo1";
    }
}

@Deprecated 过时

@Deprecated可以修饰的范围很广,包括类、方法、字段、参数等,它表示对应的代码已经过时了,程序员不应该使用它,不过,它是一种警告,而不是强制性的。

package com.kevin.demo;

public class Demo1 {

    @Deprecated
    public void goHome(){
        System.out.println("过时的方法");
    }
}

idea中调用这些方法,编译器也会显示删除线并警告

@SuppressWarning 压制Java的编译警告

@SuppressWarnings表示压制Java的编译警告,它有一个必填参数,表示压制哪种类型的警告.

关键字用途
allto suppress all warnings
boxingto suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto suppress warnings relative to deprecation
fallthroughto suppress warnings relative to missing breaks in switch statements
finallyto suppress warnings relative to finally block that don¡¯t return
hidingto suppress warnings relative to locals that hide variable
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
nlsto suppress warnings relative to non-nls string literals
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accessto suppress warnings relative to incorrect static access
synthetic-accessto suppress warnings relative to unoptimized access from inner classes
uncheckedto suppress warnings relative to unchecked operations
unqualified-field-accessto suppress warnings relative to field access unqualified
unusedto suppress warnings relative to unused code

上面的方法,我们就可以增加

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.goHome();
    }

元注解

元注解:注解的注解,即java为注解开发特准备的注解。

我们以上面讲到的java内置注解@Override为例,学习下java元注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

@Target

@Target表示注解的目标,@Override的目标是方法(ElementType.METHOD),ElementType是一个枚举,其他可选值有:

  • TYPE:表示类、接口(包括注解),或者枚举声明
  • FIELD:字段,包括枚举常量
  • METHOD:方法
  • PARAMETER:方法中的参数
  • CONSTRUCTOR:构造方法
  • LOCAL_VARIABLE:本地变量
  • ANNOTATION_TYPE:注解类型
  • PACKAGE:包

目标可以有多个,用{}表示,比如@SuppressWarnings@Target就有多个,定义为:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

如果没有声明@Target,默认为适用于所有类型。我们上篇文章的demo就没有声明@Target

@Retention

@Retention表示注解信息保留到什么时候,取值只能有一个,类型为RetentionPolicy,它是一个枚举,有三个取值:

  • SOURCE:只在源代码中保留,编译器将代码编译为字节码文件后就会丢掉
  • CLASS:保留到字节码文件中,但Java虚拟机将class文件加载到内存时不一定会在内存中保留
  • RUNTIME:一直保留到运行时

如果没有声明@Retention,默认为CLASS

@Override@SuppressWarnings都是给编译器用的,所以@Retention都是RetentionPolicy.SOURCE

@Documented

用于指定javadoc生成API文档时显示该注解信息。Documented是一个标记注解,没有成员。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited

@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。

看个栗子

public class Demo1 {

        @Inherited
        @Retention(RetentionPolicy.RUNTIME)
        static @interface Test {
        }

        @Test
        static class Base {
        }

        static class Child extends Base {
        }

        public static void main(String[] args) {
            System.out.println(Child.class.isAnnotationPresent(Test.class));
        }

}

main方法检查Child类是否有Test注解,输出为true,这是因为Test有注解@Inherited,如果去掉,输出就变成false