深层次了解Java:注释(Annotation)自定义注解新手入门

深层次了解Java:注释(Annotation)自定义注解新手入门

注释的界定

Java注释又被称为Java标明,是JDK5.0版本号逐渐适用添加源码的独特英语的语法数据库
Java语言表达中的类、方式 、自变量、主要参数移动和包等都能够被标明。和Javadoc不一样,Java标明能够根据反射面获得标明內容。在c语言编译器转化成类文档时,标明能够被置入到字节码中。Javavm虚拟机能够保存标明內容,在运作时能够获得到标明內容。 自然它也适用自定Java标明。

元注释的源代码部位

元注释分析及案例

@Documented

标示某一种类的注解将根据默认设置javadoc和相近的专用工具来纪录。这类种类应当用以注解种类的注解危害她们的顾客应用注解原素的申明。假如一个种类申明存档的注释,注释它变成注解原素的公共性API的一部分

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Dog {
    public String name() default "wangcai";
    public int age() default 0;
}

@Inherited

此注释是标志性的元注释,表明当今注释能够由子注释来承继

import java.lang.annotation.Inherited;

@Inherited
public @interface testInherited {
    Sring name();
}

@Target

用以注释类、插口(包含注释种类) 或enum申明,用分号隔开分隔来表明修饰符。

重要编码

@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();
}
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
}

@Retention

生命期注释,表明注释保存的時间长度,被叙述的注释在什么范畴内合理

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
     /**注解将被编译程序时丢掉*/
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
     /**编译程序试纪录在class文档中,但VM运作时丢掉*/
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
     /**运作时存有,能够根据反射面获得*/
    RUNTIME
}

自定义注解

界定标识必须应用元注释

  • 应用元注释@Target来标识该注释的功效范畴(室内空间范畴,在什么地方能够应用)
  • 应用元注释@Retention来标识该注释的功效时间段(时间段,在什么时候起功效)
  • 应用元注释@Inherited来标识该注释是不是能够由子注释来承继
  • 应用元注释@Documented来标识该注释是不是必须自动生成文本文档
  • 应用@interface关键字来界定注释
  • 在注释內部界定一些有关的特性,一般是方式 的方式来界定特性

自定义注解案例

  • 新创建自定义注解,这儿以idea为例子,在想新创建注释的部位,鼠标点击->New->Java Class
  • 挑选要想的方法,加上名字
    • 挑选Annotation,新创建一个Food的插口
/**
 * 该注释为一个字段名注释
 * Description:
 *
 * @author: jieya
 * Date: 2021-05-19
 * Time: 18:05
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Food {
    String name() default "";
}
  • 挑选Class,新创建一个
/**
 * 这儿界定了食材的有关注释
 * Description:
 *
 * @author: jieya
 * Date: 2021-05-19
 * Time: 18:11
 */
public class FoodAnnotation {

    // 这儿是独立界定
//    @Target(ElementType.FIELD)
//    @Retention(RetentionPolicy.RUNTIME)
//    public @interface red {
//    }
//
//    @Target(ElementType.FIELD)
//    @Retention(RetentionPolicy.RUNTIME)
//    public @interface green {
//    }

    // 这儿应用枚举类运营界定
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface color {
        /**
         * 色调枚举类型
         *
         */
        public enum Color{RED,GREEN};
        /**
         * 色调特性
         */
        Color color() default color.Color.GREEN;
    }
}
/**
 * 界定了一个食材的实体线目标类
 * Description:
 *
 * @author: jieya
 * Date: 2021-05-19
 * Time: 18:34
 */

@Data
@AllArgsConstructor
public class FoodTest {

    @Food
    @FoodAnnotation.color(color = FoodAnnotation.color.Color.GREEN)
    private String bread;

    /**
     * 饮品算不上食材,也不加注释
     */
    private String drinks;

    @Food
    @FoodAnnotation.color(color = FoodAnnotation.color.Color.RED)
    private String biscuits;

    /**
     * 下列为应用反射面获得注释的內容
     *
     * @param args
     */
    public static void main(String[] args) {
        // 界定一个食材实体线目标
        FoodTest food = new FoodTest("桃李面包", "百世快递", "达利园");
        // 获得类模板
        Class c = food.getClass();
        // 获得全部字段名
        for (Field f : c.getDeclaredFields()) {
            // 分辨该字段名是不是有Food注释
            if (f.isAnnotationPresent(Food.class)) {
                Food annotation = f.getAnnotation(Food.class);
                System.out.println("字段名:["   f.getName()   "],"   "该字段名应用了Food注释");
                // 分辨该字段名是不是有color注释
                if (f.isAnnotationPresent(FoodAnnotation.color.class)) {
                    FoodAnnotation.color.Color color = f.getAnnotation(FoodAnnotation.color.class).color();
                    System.out.println("字段名:["   f.getName()   "],"   "该字段名应用了color注释"   "色调为"   color);
                }
            } else {
                System.out.println("字段名:["   f.getName()   "],"   "该字段名沒有应用注释");
            }
        }
    }
}

FIELD注释是最经常应用的,别的的注释方式 的应用在这儿就没有过多阐释了,全是如出一辙的。

评论(0条)

刀客源码 游客评论