Java:注解和元注解

目录

注解

  1.简介

  2.三种常见的注解及其源码(源码后半段为核心,看不懂源码可以先去学习元注解部分)

        1.@Override

        2.@Deprecated

        3.@SuppressWarnings()

元注解(了解即可,用于看懂源代码)

  1.简介

  2.常见的元注解

        1.@Retention

        2.@Target

        3.@Documented


注解

  1.简介

  • 注解(Annotation),也被称为元数据(Metadata);
  • 注解不影响程序执行逻辑,但注解可以被编译或运行,相当于嵌入在代码中的注释信息;
  • 以@开头,可以写在类,方法,属性等的上头表示修饰;
  • @interface 表示注解类

  2.三种常见的注解及其源码(源码后半段为核心,看不懂源码可以先去学习元注解部分)

        1.@Override

                说明:用且仅能用于检查方法是否构成重写,若否则报编译错误。

package java.lang;

import java.lang.annotation.*;

/**
 * Indicates that a method declaration is intended to override a
 * method declaration in a supertype. If a method is annotated with
 * this annotation type compilers are required to generate an error
 * message unless at least one of the following conditions hold:
 *
 * <ul><li>
 * The method does override or implement a method declared in a
 * supertype.
 * </li><li>
 * The method has a signature that is override-equivalent to that of
 * any public method declared in {@linkplain Object}.
 * </li></ul>
 *
 * @author  Peter von der Ah&eacute;
 * @author  Joshua Bloch
 * @jls 9.6.1.4 @Override
 * @since 1.5
 */


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

        2.@Deprecated

                说明:可用于构造器,属性,局部变量,方法,包,参数和类上,表示该被修饰者已经过时,不推荐使用(但仍可以使用)。@Deprecated 往往用于新旧版本的兼容和过度。

package java.lang;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
 * A program element annotated &#64;Deprecated is one that programmers
 * are discouraged from using, typically because it is dangerous,
 * or because a better alternative exists.  Compilers warn when a
 * deprecated program element is used or overridden in non-deprecated code.
 *
 * @author  Neal Gafter
 * @since 1.5
 * @jls 9.6.3.6 @Deprecated
 */

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

        3.@SuppressWarnings()

                说明:可用于类,字段,方法,参数,构造器和局部变量上来抑制( )中所指定类型的警告。(例如:@SuppressWarnings("all")用于抑制所有警告)

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
 * Indicates that the named compiler warnings should be suppressed in the
 * annotated element (and in all program elements contained in the annotated
 * element).  Note that the set of warnings suppressed in a given element is
 * a superset of the warnings suppressed in all containing elements.  For
 * example, if you annotate a class to suppress one warning and annotate a
 * method to suppress another, both warnings will be suppressed in the method.
 *
 * <p>As a matter of style, programmers should always use this annotation
 * on the most deeply nested element where it is effective.  If you want to
 * suppress a warning in a particular method, you should annotate that
 * method rather than its class.
 *
 * @author Josh Bloch
 * @since 1.5
 * @jls 4.8 Raw Types
 * @jls 4.12.2 Variables of Reference Type
 * @jls 5.1.9 Unchecked Conversion
 * @jls 5.5.2 Checked Casts and Unchecked Casts
 * @jls 9.6.3.5 @SuppressWarnings
 */


@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}

元注解(了解即可,用于看懂源代码)

  1.简介

  •   修饰注解的注解,成为元注解(Meta annotation).

  2.常见的元注解

        1.@Retention

                说明:@Retention只能修饰注解对象的存活寿命,@Rentention包含-个RetentionPolicy类型的成员变量,使用@Rentention时必须为该value成员变量指定值。

                @Retention的三类值:

                        1. RetentionPolicy.SOURCE:编译器使用后,直接丢弃该注释。

                        2.RetentionPolicy.CLASS:(默认值),编译器将把注释记录在class文件中.当运行Java程序时,JVM不会保留注解。

                        3.RetentionPolicy.RUNTIME:编译器将把注释记录在class文件中.当运行Java程序时,JVM会保留注释.程序可以通过反射获取该注释。

        2.@Target

                说明:@Target用于修饰注解类的注解范围

        3.@Documented

                说明:@Documented:用于表明被@Documented修饰的该注解类的注解对象会被javadoc工具提取成文档,即在生成java文档时,可以看到该注释。

                提醒:定义为Documented的注解必须设置Retention值为RUNTIME。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YHanJG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值