重复注解与类型注解

Java 8对注解处理提供了两点改进:可重复的注解及可用于类型的注解
重复注解:
即允许在同一申明类型(类,属性,或方法)前多次使用同一个类型注解。在java8 以前,同一个程序元素前最多只能有一个相同类型的注解;如果需要在同一个元素前使用多个相同类型的注解,则必须使用注解“容器”。java8新增了重复注解,其使用方式为:

package com.springboot.study.annotations;

import java.lang.annotation.*;

/**
 * @Author: guodong
 * @Date: 2021/12/29 13:48
 * @Version: 1.0
 * @Description:
 */
@Repeatable(MyAnnotations.class)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

    String value() default "java";

}
package com.springboot.study.annotations;

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

/**
 * @Author: guodong
 * @Date: 2021/12/29 13:50
 * @Version: 1.0
 * @Description:
 */
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {

    MyAnnotation[] value();

}
package com.springboot.study.annotations;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * @Author: guodong
 * @Date: 2021/12/29 13:51
 * @Version: 1.0
 * @Description:
 */
public class TestAnnotation {

    @MyAnnotation("hello")
    @MyAnnotation("world")
    public void show() {
    }

    @Test
    public void test1() throws Exception {
        Class<TestAnnotation> aClass = TestAnnotation.class;
        Method show = aClass.getMethod("show");
        MyAnnotation[] annotations = show.getAnnotationsByType(MyAnnotation.class);
        for (MyAnnotation my : annotations) {
            System.out.println(my.value());
        }
    }


}

运行结果如下所示:

hello
world

重复注解只是一种简化写法,这种简化写法是一种假象:多个重复注解其实会被作为“容器”注解的value成员的数组元素处理。
java8新增的类型注解
Java8为ElementType枚举增加了TYPE_PARAMETER、TYPE_USE两个枚举值,从而可以使用@Target(ElementType_TYPE_USE)修饰注解定义,这种注解被称为类型注解,可以用在任何使用到类型的地方。在java8以前,注解只能用在各种程序元素(定义类、定义接口、定义方法、定义成员变量...)上。从java8开始,类型注解可以用在任何使用到类型的地方。
TYPE_PARAMETER:表示该注解能写在类型参数的声明语句中。类型参数声明如:<T>、<TextendsPerson>
TYPE_USE:表示注解可以再任何用到类型的地方使用,比如允许在如下位置使用:

  • 1.创建对象(用new关键字创建)
  • 2.类型转换
  • 3.使用implements实现接口
  • 4.使用throws声明抛出异常
package com.springboot.study.annotations;

import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.List;

/**
 * @Author: guodong
 * @Date: 2021/12/29 14:02
 * @Version: 1.0
 * @Description:
 */
@NotNull
public class TypeAnnotationTest implements Serializable{

    public static void main(@NotNull String [] args)  throws FileNotFoundException{
        Object obj = "fkjava.org";
        //使用强制类型转换时使用
        String str = (@NotNull String) obj;
        //创建对象时使用
        Object win = new (@NotNull) String("疯狂软件");
    }
    
    //泛型中使用
    public void foo(List<@NotNull String> info)  { 
        
    }
}

这种无处不在的注解,可以让编译器执行更严格的代码检查,从而提高程序的健壮性。需要指出的是,上面程序虽然使用了大量@NotNull注解,但是这些注解暂时不会起任何作用——因为没有为这些注解提供处理工具,java8本身并没有提供,要想这些注解发挥作用,需要开发者自己实现,或者使用第三方提供的工具。

posted @ 2021-12-29 13:55  郭慕荣  阅读(176)  评论(0编辑  收藏  举报