java注解原理

1.什么是java注解

java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制,可以理解为为某个东西(类,方法,字段。。。)打个标记的记号,等要使用这个注解时,可以通过反射获取标注里面的内容。

2.注解原理

在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容。

3.java内置注解

1.@Override 重写
2.@Deprecated 过期警告
3.@SuppressWarnings 忽略警告
等等

4.元注解和自定义注解

像程序员如何使用注解呢,肯定是自定义一个注解,那这个自定义注解编译器怎么认识呢,所以java提供一些元注解来解释下自定义注解是什么。
举个例子 自定义一个JobInfo注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Inherited 
public @interface JobInfo {
    String jobName() default "";
}

元注解: @Retention 表示这个注解的作用域 ,告知编译器这个JobInfo注解,在代码什么级别保存该注解信息。在实际开发中,我们一般都写RUNTIME,当然根据自己需求而定;枚举有
1.SOURCE:源代码时保留
2.CLASS:class文件中保留
3.RUNTIME:运行时保留
作用范围—》 RUNTIME>CLASS>SOURCE

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,

    /**
     * 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
}

元注解:@Documented 作用文档 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档

元注解:@Target 标记这个注解应该是使用在哪种 Java 成员上面

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
}

@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)——包

元注解@Inherited 表示继承,比如类A上打了@JobInfo注解,那么类B继承了类A,那么类B也可以拿到类A上的@JobInfo注解信息

其他元注解

@SafeVarargs - Java 7 开始支持,忽略任何使用参数为泛型变量的方法或构造函数调用产生的警告。

@FunctionalInterface - Java 8 开始支持,标识一个匿名函数或函数式接口。

@Repeatable - Java 8 开始支持,标识某注解可以在同一个声明上使用多次。

5.总结

程序员要使用自定义注解,必须使用元注解标识一些信息,告知编译器这个自定义注解的一些信息,这样编译器才知道怎么去校验和保留该自定义注解信息到字节码中

6.TODO

1.注解的使用(打标记)
2.注解的获取(获取注解标记的信息,拿到这些信息做其他事情。。。)通常我们说一个注解的作用时,说的是获取这个注解打标记的内容然后干了什么事情,这个作用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值