Java annotations summary (the most complete history, there is this one is enough)

What is a comment?

Defined annotations

Official website described as follows:

Java annotations to provide metadata to Java code. As metadata, annotations do not directly affect your code execution, but there are some types of notes can actually be used for this purpose. Java annotations are beginning to add from Java5 to Java.

The above words and then translate as follows:
(1) the role of metadata in development is to do data constraints and standard definition, it can be understood to specification standards code (template code);
template (element (2)

In summary, annotation is a meta-data, it can be understood as a comment, explaining that it provided a way for us to add information formalized in the code, which is used to help us write more efficient code.

Classification annotations

Commonly used annotation can be divided into three categories:

1, Java comes standard annotations
include @ Override, @ Deprecated, @ SuppressWarnings, etc., these annotations using the compiler will be checked.

2, a meta-annotation
metadata annotation annotations are used to define the annotation, including @ Retention, @ Target, @ Inherited , @ Documented, @ Repeatable like.
Yuan notes also comes with the standard Java annotations, except for the modification of notes, rather special.

3, custom annotation
users can define their own needs comment.

Use annotations

The use of Java annotations comes with
Java built-in annotations, is a set of annotations defined in java.lang to Override annotation, for example, use the following method:

@Override         //在需要注解的方法上面@Override即可
protected void onCreate() {

}

Commonly used Java annotations as follows:

1, @ Deprecated - marked content is no longer recommended;
2, @ Override - tagging method only indicates the parent class in this method covers;
. 3, of Documented @ - content may appear in the javadoc marked;
4 , @ Inherited - can only be used to mark "annotation type", it has marked annotation inheritance;
5, @ Retention - can only be used to mark "annotation type", and it is used to specify the annotation of property RetentionPolicy ;
. 6, the Target @ - can only be used to mark "annotation type", and it is used to specify the attribute of annotation ElementType;
. 7, the SuppressWarnings @ - content of the warning generated by the annotation, the compiler will remain silent warnings;
8 , @ interface - used to define an annotation;

Among them, 4,5,6,8 used for custom annotation, remember what the reader focus.

Custom annotation

In Java, we use annotations to customize a @interface annotation, as follows:

public @interface MyTestAnnotation {

}

At this point, we have defined a comment MyTestAnnotation, then we can act we have just the new annotation on a class or method:

@MyTestAnnotation
public class Test {
   @MyTestAnnotation
   public static void testString(){
   }
}

At this point, we have customized a comment, but now this annotation meaningless.

How to make annotations work? This requires the use of meta-annotation.

Commonly used meta-annotation has @Retention, @Target, @Document, @Inherited and @Repeatable five.

@Retention
Retention reservations English meaning, holding means, which indicates the presence of the annotation phase is retained in the source (compile), bytecodes (class loader) or runtime (JVM run).

@Retention notes in enumeration RetentionPolicy use to represent the period of retention comment:

  • @Retention (RetentionPolicy.SOURCE), present only in the source code annotations, not included in the class bytecode files
  • @Retention (RetentionPolicy.CLASS), the default retention policy, annotations exist in the class bytecode file, but the runtime not available
  • @Retention (RetentionPolicy.RUNTIME), annotations exist in the class bytecode files, can be obtained by reflection at runtime

If we are custom annotations, through the previous analysis, we custom annotation if only keep the source code or bytecode files will not play a role, but during operation can get to the notes in order to achieve our aim, so the custom annotation certainly using @Retention (RetentionPolicy.RUNTIME), as follows:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {

}

@Target
Target English meaning is the goal, it is also very easy to understand, use @Target meta-annotation notes indicate the scope of our role is more specific, and can be a class, method, method parameters, variables, etc., is also a class by enumerating ElementType expression type:

  • @Target (ElementType.TYPE) the role of interfaces, classes, enumerations, annotations
  • @Target (ElementType.FIELD) role attribute fields, enumeration constants
  • @Target (ElementType.METHOD) method of action
  • @Target (ElementType.PARAMETER) method of action parameters
  • @Target (ElementType.CONSTRUCTOR) acting constructor
  • @Target (ElementType.LOCAL_VARIABLE) acting local variables
  • @Target (ElementType.ANNOTATION_TYPE) acting on the annotation (@Retention annotation on the use of the property)
  • @Target (ElementType.PACKAGE) applied to the bag
  • @Target (ElementType.TYPE_PARAMETER) acting on the generic type, i.e. generic methods, generic class, generic interface (jdk1.8 added)
  • @Target (ElementType.TYPE_USE) using type may be used for labeling of any type other class (jdk1.8 added)

ElementType.TYPE general type are commonly used, as follows:

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

}

@Documented
the Document means that documents in English. Its role is to annotation elements can be included in the Javadoc go.

@Inherited
Inherited English meaning is inherited, but the succession and inheritance we usually understand very much the same, is a @Inherited annotated notes a modification of the parent class, subclass if he has not been modified other comments, it subclasses also inherits the parent class notes.

@Repeatable
Repeatable English meaning is repeatable. As the name suggests this modified instructions annotated meta-annotation can act simultaneously an object several times, but each time the role annotation and can represent different meanings.

Annotated source code analysis
we @Override notes, for example, to analyze the source code, I would like to see a general category, as ctrl-click @Override to enter its source code, as follows:

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

We see a general comment @Override annotation is defined by @interface comments, and we know that, when used @interface defined annotation, meaning it implements the interface java.lang.annotation.Annotation that the comment is a Annotation

Note: When defining Annotation, @ interface is necessary, it is different and we usually implemented method implementation of the interface. Annotation interface implementation details to complete by the compiler. @Interface defined by the comment, the comment can not inherit other comments or interface.

Let's analyze the source code Annotation class, as follows:

public interface Annotation {
    boolean equals(Object var1);

    int hashCode();

    String toString();

    Class<? extends Annotation> annotationType();
}

Through the above source, we know annotation itself is a sub-interface Annotation interface, that fact can have properties and methods of annotation, but the interface properties are static final, for the notes, it does not make sense, and we define the interface the method is equivalent to the annotation properties, it corresponds to the front of the property only to say why annotation member variables, in fact, he is the way the interface, which is why there will be brackets member variables, we can give is different from the interface in parentheses comment member variable.

Java annotations architecture

According to the source code analysis, we have come to Java annotations (Annotation) architecture as follows:

Java annotations summary (the most complete history, there is this one is enough)

0 comment is an interface class, interface class inherits from Annotation

1, 1 and 1 RetentionPolicy Annotation association
can be understood as: 1 per Annotation objects will have unique properties RetentionPolicy;

2, 1 and 1 ~ n th Annotation ElementType association
can be understood as follows: For each 1 Annotation objects, attributes may have several ElementType;

3, Annotation realize there are a lot of categories, including: Deprecated, Documented, Inherited, Override, and so on.
Annotation of each class and the associated implement RetentionPolicy and 1 and 1 ~ n th ElementType association.

The role of annotations

Before said use annotations, we first introduced XML and annotations difference:

  • Note: metadata in a distributed, tightly bound with the source code.
    xml: is a centralized metadata, and source code is free to bind
    this part of the configuration items used for the development of Java background, we know that a few years ago multi-server configuration items stored in an xml file, and then spring 2.5 start annotation-based configuration, in order to achieve the function instead of the configuration file.

Annotated uses a lot, just a simple example above, the total starting up, annotation has the following four major functions:

1, generate a document, via metadata javadoc generation identification code.

2, the compiler checks the metadata identified by the code so the compiler at compile time checks to verify.

3, the dynamic compilation process, the dynamic metadata is processed by the compile-time identification code, such as dynamically generated code.

4, dynamic process, processing dynamic metadata identification code run by the runtime, for example using injection reflection Example

Guess you like

Origin blog.51cto.com/14230003/2440990