@Target元注解的使用

@Target注解标记另外的注解用于限制此注解可以应用哪种Java元素类型。
先看Java SE 8中@Target是如何声明的:

package java.lang.annotation;

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
}

从源代码的注释中,我们看到java.lang.annotation.ElementType此枚举类声明了有哪些Java元素类型:
java.lang.annotation.ElementType.TYPE:类、接口(包括注解类型)和枚举的声明
java.lang.annotation.ElementType.FIELD:字段声明(包括枚举常量)
java.lang.annotation.ElementType.METHOD:方法声明
java.lang.annotation.ElementType.PARAMETER:参数声明
java.lang.annotation.ElementType.CONSTRUCTOR:构造函数声明
java.lang.annotation.ElementType.LOCAL_VARIABLE:本地变量声明
java.lang.annotation.ElementType.ANNOTATION_TYPE:注解类型声明
java.lang.annotation.ElementType.PACKAGE:包声明
java.lang.annotation.ElementType.TYPE_PARAMETER:类型参数声明,JavaSE8引进,可以应用于类的泛型声明之处
java.lang.annotation.ElementType.TYPE_USE:JavaSE8引进,此类型包括类型声明和类型参数声明,是为了方便设计者进行类型检查,例如,如果使用@Target(ElementType.TYPE_USE)对@NonNull进行标记,则类型检查器可以将@NonNull class C {...} C类的所有变量都视为非null
以下我们看看ElementType.TYPE_PARAMETER和ElementType.TYPE_USE的使用示例,对于其他的ElementType注解元素,看其说明就知道怎么用了:

声明由ElementType.TYPE_PARAMETER标记的@NotEmpty注解:

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * @author 春晨
 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10264624.html
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_PARAMETER)
public @interface NotEmpty {
}

声明由ElementType.TYPE_USE标记的@NotEmpty注解:

 
package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * @author 春晨
 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10264624.html
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface NotNull {
}

以下示例代码说明了两种Target注解元素的使用区别,ElementType.TYPE_USE包含了ElementType.TYPE和ElementType.TYPE_PARAMETER

package org.springmorning.demo.javabase.annotation.meta;

/**
 * @author 春晨
 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10264624.html
 */

import java.util.ArrayList;

//泛型类型声明时,使用TYPE_USE类型,编译通过
class A <@NotNull TT>{}

//泛型类型声明时,使用使用TYPE_PARAMETER类型,编译通过
public class TypeParameterAndTypeUseAnnotation<@NotEmpty T>{

    //使用TYPE_PARAMETER类型,会编译不通过
//    public void test(@NotEmpty T a){
//        new ArrayList<@NotEmpty String>();
//    }
    
    //使用TYPE_USE类型,编译通过
    public void test(@NotNull T a){
        new ArrayList<@NotNull String>();
    }
}

猜你喜欢

转载自www.cnblogs.com/springmorning/p/10264624.html