您的位置 首页 java

java元注解

元注解

@Target

@Target说明了Annotation所修饰的对象范围:即注解的作用域,用于说明注解的使用范围(即注解可以用在什么地方,比如类的注解,方法注解,成员变量注解等等)。该注解如果没有出现,那么自定义注解默认可以应用于程序中的任何元素。

该注解类型是在 java.lang.annotation.Element TYPE 枚举中 定义的:

 public enum ElementType {
    // 类、接口(包括注解类型)或枚举声明
    TYPE,
    // 字段声明(包括枚举常量)
    FIELD,
    // 方法声明
    METHOD,
    //  形参 声明
    PARAMETER,
    // 构造函数声明
     CONSTRUCTOR ,
    // 局部变量声明
    LOCAL_VARIABLE,
    // 注解类型声明
    ANNOTATION_TYPE,
    // 包声明
     PACKAGE ,
    /**
     * 类型参数声明
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
    /**
     * 类型使用
     *
     * @since 1.8
     */
    TYPE_USE
}  

使用样例

 package reflex;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface Demo {
    String name() default "";
}  

@Retention

@Retention定义了该Annotation被保留的时间长短:

@Retention的取值是在RetentionPoicy这个枚举中规定的

 public enum RetentionPolicy {
    // 编译器将丢弃注释。
    SOURCE,

    // 注释将由编译器记录在类文件中,但在运行时不需要由 VM 保留。 这是默认行为。
    CLASS,

    // 注解将被编译器记录在类文件中,并在运行时由 VM 保留,因此它们可以被反射读取
     RUNTIME 
}  

使用样例

 package reflex;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Demo {
    String name() default "";
}  

@Documented

@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如 javadoc 此类的工具文档化。Documented是一个标记注解,没有成员。

源码:

 package java.lang.annotation;

/**
 * Indicates that annotations with a type are to be documented by javadoc
 * and similar tools by default.  This type should be used to annotate the
 * declarations of types whose annotations affect the use of annotated
 * elements by their clients.  If a type declaration is annotated with
 * Documented, its annotations become part of the public API
 * of the annotated elements.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

------------
表示默认情况下,带有类型的注释将由 javadoc 和类似工具记录。 
此类型应用于注释类型声明,这些类型的注释会影响其客户端对带
注释元素的使用。 
如果使用 Documented 对类型声明进行注释,则其注释将成为带注释元素
的公共 API 的一部分  

使用样例

 package reflex;

import java.lang.annotation.*;
@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Demo {
    String name() default "";
}  

@Inherited

 package java.lang.annotation;

/**
 * Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.3 @Inherited
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

-------------
表示自动继承注解类型。 如果注解类型声明中存在 Inherited 元注解,
并且用户在类声明上查询注解类型,而类声明没有针对该类型的注解,
则将自动查询该类的超类以获取该注解类型。 将重复此过程,直到
找到此类型的注释,或到达类层次结构(对象)的顶部。 
如果没有超类具有此类型的注释,则查询将指示所讨论的类没有此类注释。
请注意,如果注释类型用于注释除类以外的任何内容,则此元注释类型无效。 
还要注意,这个元注释只会导致从超类继承注释; 已实现接口上的注释无效。  

文章来源:智云一二三科技

文章标题:java元注解

文章地址:https://www.zhihuclub.com/181686.shtml

关于作者: 智云科技

热门文章

网站地图