参考资料:https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html

普通注解’只能用来注解’代码’,而’元注解’只能用来注解 ‘普通注解’。

@Target:限制注解的元素种类。

不加元注解@Target的情况下,注解可以修饰各种元素,比如可以修饰类,可以修饰变量,可以修饰方法等,但是如果要限制注解的元素种类,则需要加入@Target。

源码:

package java.lang.annotation;

/**
* Indicates the contexts in which an annotation type is applicable. The
* declaration contexts and type contexts in which an annotation type may be
* applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
* constants of {@link ElementType java.lang.annotation.ElementType}.
*
* <p>If an {@code @Target} meta-annotation is not present on an annotation type
* {@code T} , then an annotation of type {@code T} may be written as a
* modifier for any declaration except a type parameter declaration.
*
* <p>If an {@code @Target} meta-annotation is present, the compiler will enforce
* the usage restrictions indicated by {@code ElementType}
* enum constants, in line with JLS 9.7.4.
*
* <p>For example, this {@code @Target} meta-annotation indicates that the
* declared type is itself a meta-annotation type. It can only be used on
* annotation type declarations:
* <pre>
* @Target(ElementType.ANNOTATION_TYPE)
* public @interface MetaAnnotationType {
* ...
* }
* </pre>
*
* <p>This {@code @Target} meta-annotation indicates that the declared type is
* intended solely for use as a member type in complex annotation type
* declarations. It cannot be used to annotate anything directly:
* <pre>
* @Target({})
* public @interface MemberType {
* ...
* }
* </pre>
*
* <p>It is a compile-time error for a single {@code ElementType} constant to
* appear more than once in an {@code @Target} annotation. For example, the
* following {@code @Target} meta-annotation is illegal:
* <pre>
* @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
* public @interface Bogus {
* ...
* }
* </pre>
*
* @since 1.5
* @jls 9.6.4.1 @Target
* @jls 9.7.4 Where Annotations May Appear
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}

指定注释类型适用的上下文。即:用于设定注解的使用范围(被描述的注解可以用在什么地方)。

在JLS 9.6.4.1中规定了注解类型可能适用的声明上下文和类型上下文,并且在源码中用java.lang.annotation.ElementType的枚举常量表示。即:Target用ElementType来指定注解可使用范围的枚举集合。

如果注解类型T中不存在@Target元注释,则声明的注解类型T可被用于除了类型参数声明之外的任何声明的修饰符。即,如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上 。

如果存在@Target元注释,则编译器将强制执行由ElementType枚举常量指定的使用限制。

例如:这个@Target元注释表明声明的类型本身就是一个元注释类型。它只能用于注解类型声明:

    @Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType {
...
}

这个@Target元注释表明,声明的类型仅用作复杂注释类型声明中的成员类型。它不能用来直接注解任何东西。

    @Target({})
public @interface MemberType {
...
}

单个ElementType常量在@Target注释中出现多次是编译时错误,例如,下面的元注释是非法的:

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
public @interface Bogus {
...
}

ElementType[]:

  返回注释类型可用的各种元素数组。

  取值有:

    TYPE:类、接口(包括注解类型) 或enum声明。

    FIELD:字段声明(包括enum常量).

    METHOD:方法声明。

    PARAMTER:形式参数声明。

    CONSTRUCTOR:构造函数声明。

    LOCAL_VARIABLE:局部变量声明。

    ANNOTATION_TYPE:注解类型声明。

    PACKAGE:包声明

    TYPE_PARAMETER:参数类型声明

    TYPE_USE:使用一个类型,详见:java.lang.annotation.ElementType

元注解——java.lang.annotation.Target(1.8)的更多相关文章

  1. 注解 java.lang.annotation.Inherited 介绍

    在Spring Boot中大量使用了@Inherited注解.我们来了解一下这个注解的用法,注解的源码: package java.lang.annotation; /** * Indicates t ...

  2. java.lang和java.lang.annotation中实现Annotation的类小结

    加了注解,等于打上了某种标记,没加,则等于没有某种标记,以后,其他程序可以用反射来了解你的类上面有无何种标记,看你有什么标记,就去干相应的事.标记可以加在类,方法,字段,包上,方法的参数上. (1)  ...

  3. API 注解 & Java API annotation

    API 注解 & Java API annotation 注解 annotation

  4. java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action

    原因:请求的URL地址不完整,没有找到host. 排查解决:发现HTTP请求的URL少加了项目名,导致URL地址不完整.

  5. 解决java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.robolectric.annotation.Config.application()

    Deleting the .gradle folder worked for me too. Odd. Guessing some high level caching going on somewh ...

  6. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句

    这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点:            1.反射            主要用于提取注解信息            2.自定义异常  主要是为了 ...

  7. 【注解】Annotation Target ElementType

    背景知识 Annotate.Annotation:注释.注解.批注.注 在java中,注解作为程序的元数据嵌入到程序当中,元数据标签的存在并不影响程序代码的编译和执行. 所谓Annotation就是提 ...

  8. Java中的元注解

    注解为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据. 通过使用注解,我们可以将这些元数据保存在Java源代码中,并利用annotation API为自己的 ...

  9. Java元注解,简单案例

    [注解] 程序中有 注释 和注解 * 注释:给开发人员. * 注解:给计算机看的. 注解使用:学习框架支持注解开发. [JDK提供的注解] @Override :描述方法的重写. @SuppressW ...

随机推荐

  1. spark优化设置

    ->>>配置参数优化 SparkConf sc = new SparkConf().setAppName("com.sp.test.GroupTop3").set ...

  2. you-get模块

    You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 转载https://www.cnblogs.com/wangchuanyan ...

  3. jQuery 学习笔记(5)(事件绑定与解绑、事件冒泡与事件默认行为、事件的自动触发、自定义事件、事件命名空间、事件委托、移入移出事件)

    1.事件绑定: .eventName(fn) //编码效率略高,但部分事件jQuery没有实现 .on(eventName, fn) //编码效率略低,所有事件均可以添加 注意点:可以同时添加多个相同 ...

  4. 阻塞队列(BlockingQueue)

    阻塞队列是 java.util.concurrent 包提供的一个类,该类提供了多线程中通过队列实现安全高效的数据处理的功能. 所谓阻塞队列,是在普通队列基础上实现了阻塞线程的功能: 队列为空时,获取 ...

  5. 3、LwIP协议栈规范翻译——概述

    3.概述 像许多其他TCP/IP实现一样,分层协议设计已经成为设计lwIP实现的指南.每个协议实现为自己的模块,其中几个功能充当每个协议的入口点.尽管协议层是单独实现的,一些层却不全是,正如前面所叙述 ...

  6. MVC中code first方式开发,数据库的生成与更新

    在使用EF的实际编程中我们经常遇到这样的问题:发现实体结构需要新增加一个字段,或者减少一个字段,急需把实体结构修改,并让数据库更新这种修改.在用Model First或者Database First的 ...

  7. 关于maven-resources-plugin配置的隐藏的坑

    昨天发现一个问题, 一个第三方证书的文件存放于resources文件夹下,在本地环境使用该证书进行加密调用第三方接口,没有任何问题,但是发布到测试环境和生产环境(linux)报错,提示证书工厂无法初始 ...

  8. 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置

    首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...

  9. Nginx、Tomcat配置https

    一.Nginx.Tomcat配置https 前提就是已经得到了CA机构颁发的证书 一.合并证书 1.假设证书文件如下 秘钥文件server.key,证书CACertificate-INTERMEDIA ...

  10. 使用Sqlserver事务发布实现数据同步(zhuanqian)

    事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进的.这里以sqlserver2008的事务发布功能为例,对发布 ...