@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

注意:如果一个注解没有指定@Target注解,则此注解可以用于除了TYPE_PARAMETER和TYPE_USE以外的任何地方。

以下我们看看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 @NotEmpty T test(@NotEmpty T a){
//        new ArrayList<@NotEmpty String>();
//            return a;
//    }

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

 

 下节继续

    下节将给大家讲解元注解@Retention的使用。

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/springmorning/p/10264624.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值