Original http://www.cnblogs.com/coolgun/p/3802535.html
Java annotations
That is, the comment, Baidu explained: Also called meta-data. A description of the code level. Personal understanding: is a comment that the content can be understood by the code, which is generally a class.
Meta data
Also called meta-annotations, is placed in front of a defined annotation class, is a restriction on annotations. Only two: @Retention and @Target (I only found these two).
@Retention: Used to describe the life cycle of the annotation class. It has the following three parameters:
Retentionpolicy.source: Annotations remain in the source file only
Retentionpolicy.class: Annotations remain in the CLASS file and discarded when loaded into the JVM virtual machine
Retentionpolicy.runtime: Annotations remain during a program run, at which point all annotations defined on a class can be obtained through reflection.
@Target: Used to indicate that the annotation can be declared before those elements.
Elementtype.type: Note that the note can only be declared before a class.
Elementtype.field: Note that the note can only be declared in front of a field in a class.
Elementtype.method: Indicates that the annotation can only be declared before a method of a class.
Elementtype.parameter: Note that the note can only be declared before a method parameter.
Elementtype.constructor: Note that the annotation can only be declared before the constructor of a class.
Elementtype.local_variable: Note that the note can only be declared before a local variable.
Elementtype.annotation_type: Note that the note can only be declared before an annotation type.
Elementtype.package: Note that the note can only be declared before a package name.
Definition of annotations:
The definition of the annotation uses the keyword @interface, and @rentention (ARG) or @target (args) in the line above, as in the following example:
@Rentention (Retentionpolicy.runtime)
Public @interface Annotation01 {
Defining a public final static property
.....
public abstract methods.
......
}
Common basic built-in annotations
@Override When we want to rewrite a method, add @override to the method, and when the name of our method goes wrong, the compiler will error. Defined as follows:
@Retention (Retentionpolicy.source)
Public @interface Override
@Deprecated used to indicate that a property or method of a class is obsolete and is not intended to be used by others, and is decorated with @deprecated on properties and methods. Defined as follows:
@Retention (Retentionpolicy.source)
Public @interface Deprecated
@SuppressWarnings used to suppress warnings from the program. Defined as follows:
@Retention (Retentionpolicy.source)
Public @interface suppresswarnings
Use of annotations
Define annotations--note--get annotations;
The obtained annotations are obtained through the Getannotation or Getannotations method of the modified object.
Java Annotations @retention @interface meta-data