目录
2.三种常见的注解及其源码(源码后半段为核心,看不懂源码可以先去学习元注解部分)
注解
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é
* @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 @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。