首页 > 编程语言 > 详细

Java中的自定义注解

时间:2021-04-23 10:51:25      阅读:21      评论:0      收藏:0      [点我收藏+]

本文思路参考于用大白话聊聊JavaSE -- 自定义注解入门

0.背景

在写代码时,我们通常会加上一些注释,告诉开发人员这段代码的含义.eg:

    /**
     * @Description: 输入的Integer必须大于0
     * @Author: Yiang37
     * @Date: 2021/04/22 23:58:27
     * @Version: 1.0
     */
    public int checkIn(Integer integer) {
        return integer > 0 ? integer : -1;
    }
   //...

写下的注释,让阅读代码的人了解到了这段代码所做的事情.

当然,注解是写给人看的,那么有没有一种方式,可以让程序知道这段代码想干什么事情呢?

注解,就是写给程序看的一种注释.

1.注解的创建

// 没错.就是@interface
public @interface Annotation01 {
    //...
}

1.1.1 元注解

元注解理解为描述注解的注解,元数据理解为描述数据的数据,元类理解为描述类的类

在JDK中提供了4个标准的用来对注解类型进行注解的注解类(元注解),他们分别是:

所以,在Java中,除了有限的几个固定的"描述注解的注解"以外,所有的注解都是自定义注解。

@Target

@Retention

@Documented

@Inherited

2.注解要解决的问题

  • 注解要用在哪里
  • 注解在什么时候起作用
  • 注解里包含哪些东西

2.1 注解要用在哪里 @Target()

eg: @Target(ElementType.METHOD) : 注解要用在方法上.

2.1.1 按住ctrl+p,可以看到期望输出一个ElementType类型的数组

技术分享图片

2.2.2 点击@Target进入后,可以看到@Target本身就是一个注解

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

2.2.3 点击去ElementType,可以看到具体的枚举值.

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 //1.8加入
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8 //1.8加入
     */
    TYPE_USE
}

2.2.4 枚举值说明

   @Target(ElementType.TYPE)           // 接口、类、枚举、注解

  @Target(ElementType.FIELD)          // 字段、枚举的常量

  @Target(ElementType.METHOD)         // 方法

  @Target(ElementType.PARAMETER)      // 方法参数

  @Target(ElementType.CONSTRUCTOR)    // 构造函数

  @Target(ElementType.LOCAL_VARIABLE)  // 局部变量

  @Target(ElementType.ANNOTATION_TYPE) // 注解

  @Target(ElementType.PACKAGE)         // 包

2.2 注解在什么时候起作用 @Retention

eg: @Retention(RetentionPolicy.RUNTIME) : 我们希望在程序运行的时候,注解发挥作用.就是说,当你的程序跑起来了,电脑才开始阅读这些注解.

2.2.1 枚举值

@Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含

@Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

@Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

2.3 注解里包含哪些东西

eg:注解中包含一个String和int

public @interface Annotation01 {

    String oneStr() default "this is one str";

    int onData();
}

2.4 创建一个完整的注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Annotation01 {

    String oneStr() default "this is one str";

    int onData();
}

3.注解的启用

@Annotation01(oneStr = "abc",onData = 1)
public class AnnotationTest01 {


}

4.注解的运用

上文中,用上注解后,我们好像什么也没做.

只是知道AnnotationTest01这个类上对应着一个@Annotation01的注解,里面有着oneStr = "abc",onData = 1两个字段.

//待完善....

Java中的自定义注解

原文:https://www.cnblogs.com/yang37/p/14692138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!