zoukankan      html  css  js  c++  java
  • java元注解 @Target注解用法

    @Target:

       @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

      作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

     取值(ElementType)有

    public enum ElementType {
        /**用于描述类、接口(包括注解类型) 或enum声明 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
    

      

    ElementType.TYPE_PARAMETER(Type parameter declaration) 用来标注类型参数, 栗子如下:
    @Target(ElementType.TYPE_PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TypeParameterAnnotation {
        
    }
    
    // 如下是该注解的使用例子
    public class TypeParameterClass<@TypeParameterAnnotation T> {
        public <@TypeParameterAnnotation U> T foo(T t) {
            return null;
        }    
    }
    

      ElementType.TYPE_USE(Use of a type) 能标注任何类型名称,包括上面这个(ElementType.TYPE_PARAMETER的),栗子如下:

    public class TestTypeUse {
    
        @Target(ElementType.TYPE_USE)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface TypeUseAnnotation {
            
        }
        
        public static @TypeUseAnnotation class TypeUseClass<@TypeUseAnnotation T> extends @TypeUseAnnotation Object {
            public void foo(@TypeUseAnnotation T t) throws @TypeUseAnnotation Exception {
                
            }
        }
        
        // 如下注解的使用都是合法的
        @SuppressWarnings({ "rawtypes", "unused", "resource" })
        public static void main(String[] args) throws Exception {
            TypeUseClass<@TypeUseAnnotation String> typeUseClass = new @TypeUseAnnotation TypeUseClass<>();
            typeUseClass.foo("");
            List<@TypeUseAnnotation Comparable> list1 = new ArrayList<>();
            List<? extends Comparable> list2 = new ArrayList<@TypeUseAnnotation Comparable>();
            @TypeUseAnnotation String text = (@TypeUseAnnotation String)new Object();
            java.util. @TypeUseAnnotation Scanner console = new java.util.@TypeUseAnnotation Scanner(System.in);
        }
    }
    

      

  • 相关阅读:
    Centos7 系统更改apache默认网站目录(解决You don't have permission to access / on this server问题)
    1-18-2 LVM管理和ssm存储管理器使用&磁盘配额 (二)
    1-3 RHEL7操作系统的安装
    1-18-1 LVM管理和ssm存储管理器使用&磁盘配额(一)
    【转载】CentOS7下使用LVM给系统硬盘扩容
    自动备份Mysql数据库脚本
    docker安装nginx并配置通过https访问
    搭建本地yum源和局域网yum源
    【转载】使用Docker Hub官方gcc:latest镜像编译C/C++程序以及缩小镜像的方法
    【转载】如何使用docker部署c/c++程序
  • 原文地址:https://www.cnblogs.com/unknows/p/10261539.html
Copyright © 2011-2022 走看看