zoukankan      html  css  js  c++  java
  • @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的使用。

  • 相关阅读:
    Python抓取网页动态数据——selenium webdriver的使用
    mac上vim插件YouCompleteMe的安装
    shell命令的搜索顺序(hash -r, hash的作用)
    mac gcc framework include
    一个debug了4个多小时的bug, 操作系统真象还原: 试图运行起第6章的print相关函数却没有预期效果
    python中bytes类型与str类型的区别以及python中str类型是怎么存储的
    一行不解的awk代码
    OKR案例——不同类型的OKR实例
    PostgreSQL与MySQL优势比较
    [Win7 x64]Eclipse Indigo 3.7 中文字体偏小解决方案: Consolas 微软雅黑混合字体!
  • 原文地址:https://www.cnblogs.com/springmorning/p/10264624.html
Copyright © 2011-2022 走看看