博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java-Annotation注解
阅读量:7257 次
发布时间:2019-06-29

本文共 8173 字,大约阅读时间需要 27 分钟。



@Override

  1. 是告诉编译器检查这个方法,保证父类要包含一个被该方法重写的方法,否则编译出错。
  2. 只能修饰方法,不能修饰其他程序元素 。

Java 9 增强的@Deprecated

  1. 表示某个程序元素已经过时。
  2. Java 9 为@Deprecated注解增加了如下两个属性

    • ForRemoval:该boolean类型的属性指定该API在将来是否会被删除。
    • Since:该String类型的属性指定该API从哪个版本被标记为过时。
public class DeprecatedS {    //已过时,since是从9版本开始,forRemoval指定该API将来会被删除    @Deprecated(forRemoval=true,since="9")    public void info() {    }    public static void main(String[] args) {        DeprecatedS s = new DeprecatedS();        s.info();    }}

抑制编译器警告:@SuppressWarnings

  1. 指示被该注解修饰的程序元素,以及该程序元素中的所有子元素,取消显示指定的编译器警告。@SuppressWarnings会一直作用于该程序元素的所有子元素。
//关闭整个类里的编译器警告@SuppressWarnings(value="unchecked")public class SuppressWarningsS {    public static void main(String[] args) {        List
list = new ArrayList(); }}

堆污染警告 与 java 9 增强的@SafeVarargs

  1. 当把一个不带泛型的对象赋给一个带泛型的变量时,往往就会发生堆污染
public class SafeVarargsS {    public static void main(String[] args) {    }    //Type safety: Potential heap pollution via varargs parameter list    //类型安全:通过varargs参数列表的潜在堆污染。    public static void ai(List
...list) { List[] list2 = list; List
myList = new ArrayList<>(); myList.add(new Random().nextInt(100)); list2[0] = myList; String string = list[0].get(0); }}
  • 上面程序中的粗体字代码已经发生了堆污染,由于该方法有个形参是List<String>...类型,个数可变的形参相当于数组,但java又不会支持泛型数组,因此程序只能把List<String>...当成List[]处理,这里发生了堆污染。
  • 使用@SafeVarargs
@SafeVarargspublic static void ai(List
...list) {}

Java 8的函数式接口 @FuncationInterface

  1. 如果接口中只有一个抽象方法,可以包含多个默认方法或多个static方法,该接口就是函数式接口。
  2. @FuncationInterface就是用来指定某个接口必须是函数式接口。
@FunctionalInterfacepublic interface Function
{}
  • FuncationInterface只能修饰接口,不能修饰其他元素

JDK的元注解

  1. 在java.lang.annotation包下提供了6个Meta注解(元注解),@Repeatable专门用于定义java 8新增的重复注解。
  2. 使用@Retention :保留,扣留 Policy:政策,方针

    • @Retention只能用于修饰注解定义,用于指定被修饰的注解可以保留多长时间,他包含一个RetentionPolicy类型的value成员变量,所以使用时必须为该value成员变量指定值。
    • Value成员变量的值只能是如下三个

      • RetentionPolicy.CLASS : 编译器将把注解记录在class中,当运行java程序时,JVM不可获得注解信息,这是默认值。
      • RetentionPolicy.REUNTIME:编译器将把注解记录在class文件中,当运行java程序时,JVM也可获取注解信息,程序可以通过反射获得该注解信息。
      • RetentionPolicy.SOURCE:注解只保留在源代码中,编译器直接丢弃这种注解。
    • 使用
@Retention(value = RetentionPolicy.RUNTIME)public @interface My1 {}@Retention(RetentionPolicy.RUNTIME)public @interface My1 {}
  1. 使用@Target

    • 只能修饰注解定义,它用于指定被修饰的注解能用于修饰那些程序单元。
    • @Target元注解也包含一个value成员变量
    • 成员变量如下

      • ElementType.ANNOTATION_TYPE:指定该策略的注解只能修饰注解。
      • ElementType.CONSTRUCTOR:指定该策略的注解只能修饰构造器。
      • ElementType.FIELD:指定该策略的注解只能修饰成员变量。
      • ElementType.LOCAL_VARIABLE:指定该策略的注解只能修饰局部变量。
      • ElementType.METHOD:指定该策略的注解只能修饰方法。
      • ElementType.PACKAGE:指定该策略的注解只能修饰包定义。
      • ElementType.PARAMETER:指定该策略的注解只能修饰参数。
      • ElementType.TYPE:指定该策略的注解只能修饰类,接口,注解类型,枚举定义。
    • 使用
@Target(value = ElementType.ANNOTATION_TYPE)public @interface My1 {}
  1. 使用@Document

    • @Document用于指定被该元注解修饰的注解类将被javadoc工具提取城文档,如果定义注解类时使用了@Document,则所有使用该注解修饰的程序元素的API文档中将会包含该注解说明。
  2. 使用@Inherited : 可继承的

    • 指定被他修饰的注解将具有继承性。
@Inheritedpublic @interface My1 {}
  • 上面程序中代码表明@My1具有继承性,如果某个类使用@My1修饰,则该类的子类将自动使用@My1修饰。
  • 检查其子类是否默认使用@My1修饰。
@My1class A{}public class My1_Test extends A {    public static void main(String[] args) {        System.out.println(My1_Test.class.isAnnotationPresent(My1.class));//true    }}

自定义注解

  1. 定义新的注解使用@Interface关键字定义一个新的注解类型与定义一个接口非常像,如下
public @interface My1 {}
  1. 定义了该注解之后,就可以在程序的任何地方使用该注解。
  2. 默认情况下,注解可用于修饰任何程序元素,包括类,接口,方法等。
  3. 注解还可以带成员变量,成员变量在注解定义中以无形参的方法形式来声明,其方法名和返回值定义了该成员变量的名字和类型。
public @interface My1 {    String name();    int id();}
  • 一旦在注解里定义了成员变量后,使用该注解时就应该为他的成员变量指定值。
@My1(id=1,name="rrr")class A{}(6)    也可以在定义注解的成员变量时为其指定初始值,指定默认值default。public @interface My1 {    String name() default "ccc";    int id() default 123;}
  1. 成员变量指定了值,则默认值就不会起作用。
  2. 根据注解是否可以包含成员变量,可以把注解分为

    • 标记注解:没有定义成员变量的注解类型被称为标记。这种注解仅利用自身的存在与否来提供信息,如@Test、
    • 元数据注解:包括成员变量的注解,因为他们可以接受更多的元数据,所以也被称为元数据注解。

提取注解信息

  1. 使用注解修饰了类,方法,成员变量等之后,这些注解不会自己生效,必须由开发者提供相应的工具来提取并处理注解信息。
  2. AnnotatedElement接口,该接口代表程序中可以接受注解的程序元素,该接口主要有如下个实现类:

    • Class:类定义
    • Constructor:构造器定义
    • Field:类的成员变量定义
    • Method:类的方法定义
    • Package:类的包定义
  3. 只有当定义注解时使用了@Retention(RetentionPolicy.RUNTIME)修饰,该注解才会在运行时可见,JVM才会在装载class文件时读取保存在class文件中的注解信息。
  4. 获取My2类的方法上的Annotation
public class My2 {    @My1(id =123,name ="dsds")    public void info() {    }}public class GetMy2Annotation{    public static void main(String[] args) throws Exception{        Class
forName = Class.forName("annotations.My2");//加载类 Method method = forName.getMethod("info");//得到方法 Annotation[] annotations = method.getAnnotations();//得到方法上所有注解 //@annotations.My1(name="dsds", id=123) for (Annotation annotation : annotations) { System.out.println(annotation); } }}
  1. 访问注解中的元数据
public class GetMy2Annotation{    public static void main(String[] args) throws Exception{        Class
forName = Class.forName("annotations.My2");//加载类 Method method = forName.getMethod("info");//得到方法 Annotation[] annotations = method.getAnnotations();//得到方法上所有注解 /* * 123 * dsds * */ for (Annotation annotation : annotations) { if (annotation instanceof My1) { System.out.println(((My1)annotation).id()); System.out.println(((My1)annotation).name()); } } }}
  1. 利用注解模拟@Test的JUnit效果
public class RunTest {    public static void main(String[] args) throws Exception {        Class
forName = Class.forName("annotations.RunTest_JUnit");//加载类 Method[] methods = forName.getDeclaredMethods();//获得本类所有方法 int success = 0;//成功方法 int fail = 0;//失败方法 for (Method method : methods) { System.out.println(method); System.out.println(method.isAnnotationPresent(Testable.class)); if (method.isAnnotationPresent(Testable.class)) { try { //抑制private访问修饰符 method.setAccessible(true); method.invoke( null); success++; } catch (Exception e) { fail++; } } } System.out.println("成功方法有:" + success +"个 失败的有:"+fail+"个"); }}class RunTest_JUnit{ @Testable private static void t() { System.out.println("=========================t"); } private static void t1() { } @Testable private static void t2() { System.out.println("=========================t2"); } @Testable private static void r2() { System.out.println("=========================r2"); throw new RuntimeException("出错误啦"); }}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Testable{}Output:private static void annotations.RunTest_JUnit.t1()falseprivate static void annotations.RunTest_JUnit.t()true=========================tprivate static void annotations.RunTest_JUnit.r2()true=========================r2private static void annotations.RunTest_JUnit.t2()true=========================t2成功方法有:2个 失败的有:1个
  • 上面的@Testable用于标记那些方法是可测试的,该注解可以作为JUnit测试框架的补充,在JUnit框架中他要求测试用例的测试方法必须以test开头。

Java 8 新增的重复注解

@Retention(RetentionPolicy.RUNTIME)@Target(value=ElementType.TYPE)@Repeatable(value=FkTags.class)//只能可以包容它的容器类public @interface FkTag {    String name() default "王自强";    int id();}@Retention(RetentionPolicy.RUNTIME)@Target(value=ElementType.TYPE)public @interface FkTags {    FkTag[] value();}//@FkTags({@FkTag(id=1,name="dsd"),@FkTag(id=213)})@FkTag(id=123)@FkTag(id=1233)public class Test_FkTage {    public static void main(String[] args) {        Class
cl = Test_FkTage.class;//获取类 //这个方法可以获得多个重复注解,而getDeclaredAnnotation只能获取一个 FkTag[] annotationsByType = cl.getAnnotationsByType(FkTag.class); for (FkTag fkTag : annotationsByType) { System.out.println(fkTag.id()+" "+ fkTag.name()); } FkTags annotation = cl.getAnnotation(FkTags.class); System.out.println(annotation); }}Output123 王自强1233 王自强@fkAnnotation.FkTags(value={@fkAnnotation.FkTag(name="王自强", id=123), @fkAnnotation.FkTag(name="王自强", id=1233)})
  • 如上的重复注解只是一种简便的写法,运用@Repeatable注解来制定他的容器注解类即可。
  • 容器注解类注解的保留期必须比他所包含的注解的保留期更长,否则编译器报错。

Java 8新增的类型注解

编写自定义注解时未写@Inherited的运行结果 编写自定义注解时写了@Inherited的运行结果
子类的类上能否继承到父类的类上的注解?
子类方法,实现了父类上的抽象方法,这个方法能否继承到注解?
子类方法,实现了父类上的方法,这个方法能否继承到注解?
子类方法,覆盖了父类上的方法,这个方法能否继承到注解?

转载地址:http://wdvdm.baihongyu.com/

你可能感兴趣的文章